亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python中的文件迭代問題

python中的文件迭代問題

浮云間 2023-06-20 10:35:47
我有一個任務,其中有兩個文件,即“user.txt”、“pwd.txt”,現在我想基本上遍歷這些文件的每個值,即“John”和“pwd.txt”中的所有值,然后是“user.txt”中的另一個值,所有值都是“pwd.txt”。這里我想實現線程。這是我的代碼,import threadingf1 = open("user.txt", "r")f2 = open("pwd.txt", "r")threads = []def brute():    for letter in f1.readlines():        print "[+]First Value: {}".format(letter)        for second_letter in f2.readlines():            print "[+]Second Value: {}".format(second_letter)                    threads.append(threading.Thread(target=runner, args=(letter,second_letter,)))for thread in threads:    thread.start()for thread in threads:    thread.join()    def runner(word1,word2):    print("[+]I am just a worker class: {0}:{1}".format(word1,word2))輸出來了[+]First Value: john[+]Second Value: test1234[+]Second Value: socool![+]Second Value: abcde1234[+]Second Value: password1[+]Second Value: Passw0rd[+]Second Value: adminRocks[+]First Value: dean[+]First Value: harry[+]First Value: sam[+]First Value: joseph[+]First Value: rick[+]I am just a worker class: john:test1234[+]I am just a worker class: john:socool![+]I am just a worker class: john:abcde1234[+]I am just a worker class: john:password1[+]I am just a worker class: john:Passw0rd[+]I am just a worker class: john:adminRocks[Finished in 0.3s]我不確定如何在此處使用密碼文件中的所有密碼打印所有用戶值。非常感謝任何幫助。
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

根據您的帖子,目標是交叉加入用戶和密碼文件。為此,在 pwd 文件中加載一次,然后處理每個用戶(每個線程一個)。


請注意,我使用 Python 3.8 對此進行了測試。


試試這個代碼:


import threading


# create files for testing

user = "user1\nuser2\nuser3\nuser4\nuser5\nuser6\nuser7\nuser8\nuser9"

pwd = "pwd1\npwd2\npwd3\npwd4\npwd5\npwd6\npwd7\npwd8\npwd9"

with open("user.txt",'w') as f: f.write(user)

with open("pwd.txt",'w') as f: f.write(pwd)



##### main script #####


# load all pwds

f2 = open("pwd.txt", "r")

lstpwd = f2.readlines()


f1 = open("user.txt", "r")

threads = []

def brute():

    for letter in f1.readlines():

        print ("\n[+]First Value: {}".format(letter.strip()))

        for second_letter in lstpwd:

            print ("[++]Second Value: {}".format(second_letter.strip()))

            threads.append(threading.Thread(target=runner, args=(letter,second_letter,)))


    for thread in threads:

        thread.start()


    for thread in threads:

        thread.join()

    

def runner(word1,word2):

    print("[+]I am just a worker class: {0}:{1}".format(word1.strip(),word2.strip()))

    

brute()

輸出


[+]First Value: user1

[++]Second Value: pwd1

[++]Second Value: pwd2

[++]Second Value: pwd3

.......    

[+]First Value: user2

[++]Second Value: pwd1

[++]Second Value: pwd2

[++]Second Value: pwd3

.......

[+]I am just a worker class: user1:pwd1

[+]I am just a worker class: user1:pwd2

[+]I am just a worker class: user1:pwd3

.......

[+]I am just a worker class: user2:pwd1

[+]I am just a worker class: user2:pwd2

[+]I am just a worker class: user2:pwd3

.......


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號