我想从服务器向客户端发送大块字节,然后将这些块保存在列表中。 我用20个线程发送这个东西,20个线程用于接收。 在每个块中,我在开始处插入两个字节表示块ID的编号,该块ID应该是客户端在列表的ehach索引中知道的指示符,该块将被放置。
客户端:
global chunks
chunks = ['' for _ in xrange(WORKERS)] # That list will hold the chunks before writing it to file.
threads_list = []
for _ in xrange(0, WORKERS):
worker = threading.Thread(target=job, kwargs={"server_tunnel": server_tunnel})
worker.start()
threads_list.append(worker)
for is_end in threads_list:
is_end.join() # Stop the all program till the all file has been sent
客户端职位职能(线程):
def job(server_tunnel):
download = ''
global chunks
while not download == "DIE THREAD!":
curr_bytes = server_tunnel.recv(SPEED) # 7B 8192 4096
chunks[int(curr_bytes[0:2])] += curr_bytes[2:]
服务器:
# Making a list of chunks and send the chunks by workers
chunk = curr_file_size / WORKERS
chunks = []
with open(path, 'rb') as curr_file:
for _ in xrange(0, WORKERS):
chunks.append(curr_file.read(chunk))
chunks[WORKERS - 1] += curr_file.read(curr_file_size % WORKERS)
threads_list = []
for i in xrange(0, WORKERS):
worker = threading.Thread(target=self.job, kwargs={"curr_file_size": curr_file_size,
"curr_file": curr_file,
"client_socket": client_socket,
"chunk": chunks[i],
"worker": i})
服务器作业(线程):
def job(self, curr_file_size, curr_file, client_socket, chunk, worker):
sent = 0
length_chunk = len(chunk)
while sent < length_chunk:
try:
client_socket.sendall(str(worker).zfill(2) + chunk[sent:sent + SPEED]) # 7a ,8192 4096
sent += SPEED
except socket.error:
print 'One link died. E2'
self.clients.remove(self.chosen)
client_socket.close()
self.chosen = ''
client_socket.send("DIE THREAD!")
现在.. 尝试将文件从服务器发送到客户端时出现此错误:
chunks [int(curr_bytes [0:2])] + = curr_bytes [2:] ValueError:无效 对于具有基数10的int()的文字:'{\ xa2'
chunks [int(curr_bytes [0:2])] + = curr_bytes [2:] ValueError:无效 int()的文字为基数10:'DI'等..
有什么建议吗?