在python中,newStr+“:”+otherStr变成newStr+“\n”+“:”+oth

2024-04-25 08:14:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望标题不是太混乱,但你会明白我的意思有点。同时,一些背景故事--我正在开发一个函数,它生成随机用户名和密码,并将它们作为username:password写入另一个程序的文本文件,该程序收集username:password行,如下所示:

string = line.split(":")
username = string[0]
pwd = string[1]

为什么这很重要?当我运行我的函数时:

代码:

# To generate users and passwords for the password file:
"""
Usage: count-- how many accounts to generate
file-- where to dump the accounts
method-- dict is where it loops through words 
and chooses random ones as users and passwords,
and brute (not implemented yet) is where it chooses
random characters and strings them together as users
and passwords.
users-- if you want any filled in users, put them in here.
passes-- if you want any filled in passes, put them in here.
"""
def genAccts(count, file, method="dict", users=[], passes=[]):
    try:
        f = open(file, "w")
        if method == "dict":
            dictionary = "Dictionary.txt"#input("[*] Dictionary file: ")
            d = open(dictionary, "r")
            words = d.readlines()
            d.close()
            accts = []
            for b in range(0, count):
                global user
                global pwd
                user = random.choice(words)
                pwd = random.choice(words)
                if b < len(users)-1:
                    user = users[b]
                if b < len(passes)-1:
                    pwd = passes[b]
                acct = [user, pwd]
                accts.append(acct)
            print("[+] Successfully generated",count,"accounts")
            for acct in accts:
                combined = acct[0]+":"+acct[1]
                print(combined)
                f.write(combined)
            f.close()
            print("[+] Successfully wrote",count,"accounts in",file+"!")
    except Exception as error:
        return str(error)

genAccts(50, "brute.txt")

在我的密码文件brute.txt中,我得到如下输出

quainter
:slightest
litany
:purples
reciprocal
:already
delicate
:four

所以我想知道为什么在用户名后面加一个\n?你知道吗


Tags: andinifcountpwdusernamerandompassword
3条回答

您可以通过替换:

words = d.readlines()

使用:

words = [x.strip() for x in d.readlines()]
words = d.readlines()

上面的函数返回一个列表,其中包含作为一个项的每一行。每个单词的结尾都将包含\n字符。因此,要获得所需的输出,必须修剪username的空白字符。你知道吗

user = random.choice(words).strip()

上面的线会解决你的问题!你知道吗

使用此选项:

def genAccts(count, file, method="dict", users=[], passes=[]):
    try:
        f = open(file, "w")
        if method == "dict":
            dictionary = "Dictionary.txt"#input("[*] Dictionary file: ")
            d = open(dictionary, "r")
            words = d.readlines().strip()
            d.close()
            accts = []
            for b in range(0, count):
                global user
                global pwd
                user = random.choice(words)
                pwd = random.choice(words)
                if b < len(users)-1:
                    user = users[b]
                if b < len(passes)-1:
                    pwd = passes[b]
                acct = [user, pwd]
                accts.append(acct)
            print("[+] Successfully generated",count,"accounts")
            for acct in accts:
                combined = acct[0]+":"+acct[1]
                print(combined)
                f.write(combined)
            f.close()
            print("[+] Successfully wrote",count,"accounts in",file+"!")
    except Exception as error:
        return str(error)

genAccts(50, "brute.txt")

相关问题 更多 >