我对python和线程很陌生。我正在尝试编写一个程序,它使用线程和队列来使用caesar密码加密txt文件。当我以独占方式使用加密函数时,它本身工作得很好,但是当我在程序中使用它时,我会得到一个错误。错误从这一行开始:
for c in plaintext:
下面是整个代码:
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()
如有任何帮助,请提前告知。
您正在传递Queue.get[函数]给caesar,而不是调用Queue.get()的值。
加上“,”,你就没事了。:)
在代码中,您使用的
q1.get
和q2.get
是函数对象。而是用括号来调用它:它将从Queue中获取值。
根据^{} document :
相关问题 更多 >
编程相关推荐