Neb*_*ran 1 python multithreading typeerror caesar-cipher
我对python和线程很陌生。我试图编写一个使用线程和队列的程序,以便使用凯撒密码对txt文件进行加密。当我单独使用加密功能时,加密功能本身可以很好地工作,但是在程序中使用它时出现错误。错误从此行开始:
for c in plaintext:
Run Code Online (Sandbox Code Playgroud)
这是整个代码:
import threading
import sys
import Queue
#argumanlarin alinmasi
if len(sys.argv)!=4:
print("Duzgun giriniz: '<filename>.py s n l'")
sys.exit(0)
else:
s=int(sys.argv[1])
n=int(sys.argv[2])
l=int(sys.argv[3])
#Global
index = 0
#caesar sifreleme
#kuyruk deklarasyonu
q1 = Queue.Queue(n)
q2 = Queue.Queue(2000)
lock = threading.Lock()
#Threadler
threads=[]
#dosyayi okuyarak stringe cevirme
myfile=open('metin.txt','r')
data=myfile.read()
def caesar(plaintext, key):
L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ", range(26)))
I2L = dict(zip(range(26), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
ciphertext = ""
for c in plaintext:
if c.isalpha():
ciphertext += I2L[(L2I[c] + key) % 26]
else:
ciphertext += c
return ciphertext
#Thread tanimlamasi
class WorkingThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
lock.acquire()
q2.put(caesar(q1.get, s))
lock.release()
for i in range(0,n):
current_thread = WorkingThread()
current_thread.start()
threads.append(current_thread)
output_file=open("crypted"+ "_"+ str(s)+"_"+str(n)+"_"+str(l)+".txt", "w")
for i in range(0,len(data),l):
while not q1.full:
q1.put(data[index:index+l])
index+=l
while not q2.empty:
output_file.write(q2.get)
for i in range(0,n):
threads[i].join()
output_file.close()
myfile.close()
Run Code Online (Sandbox Code Playgroud)
希望有任何帮助,在此先感谢。
在您的代码中,您正在使用q1.get和q2.get函数对象。而是用括号来称呼它:
q1.get()
Run Code Online (Sandbox Code Playgroud)
这将从Queue中获取值。
从队列中删除并返回一个项目。如果可选的args块为true,并且超时为None(默认值),则在必要时进行阻塞,直到有可用项为止。
| 归档时间: |
|
| 查看次数: |
10927 次 |
| 最近记录: |