Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-接收多个文件_Python_File_Sockets_Server_Client - Fatal编程技术网

Python-接收多个文件

Python-接收多个文件,python,file,sockets,server,client,Python,File,Sockets,Server,Client,我正在学习socket编程和Python,我需要创建一个从客户机接收多个文件的服务器。 当我收到其中一个文件时,我收到以下错误: ValueError: invalid literal for int() with base 2: '<!DOCTYPE html PUBLIC "-//W3C//D' server.py while True: size = clientsocket.recv(16) if not size: break

我正在学习socket编程和Python,我需要创建一个从客户机接收多个文件的服务器。 当我收到其中一个文件时,我收到以下错误:

ValueError: invalid literal for int() with base 2: '<!DOCTYPE html PUBLIC "-//W3C//D'
server.py

while True:
       size = clientsocket.recv(16)
       if not size:
           break
       size = int(size)
       filename = clientsocket.recv(size)
       filesize = clientsocket.recv(32)
       filesize = int(filesize,2)
       file_to_write = open("/home/giorgio/Scrivania/SERVER/Download/"+'new_'+filename, 'wb')
       num_files += 1
       chunksize = 1024
       while filesize > 0:
           if filesize < chunksize:
               chunksize = filesize
           data = clientsocket.recv(chunksize)
           file_to_write.write(data)
           filesize -= len(data)

       file_to_write.close()
       print 'File received successfully'


serversock.close()
为True时:
大小=clientsocket.recv(16)
如果不是尺寸:
打破
大小=整数(大小)
filename=clientsocket.recv(大小)
filesize=clientsocket.recv(32)
filesize=int(filesize,2)
file_to_write=open(“/home/giorgio/Scrivania/SERVER/Download/”+“new”+文件名“wb”)
num_文件+=1
chunksize=1024
当文件大小>0时:
如果filesize
为True时:
大小=clientsocket.recv(16)
如果不是尺寸:
打破
大小=整数(大小)
filename=clientsocket.recv(大小)
filesize=clientsocket.recv(32)
#filesize=int(filesize,2)#这不是必需的,这会导致错误,因此请删除它。
file_to_write=open(“/home/giorgio/Scrivania/SERVER/Download/”+“new”+文件名“wb”)
num_文件+=1
chunksize=1024
而文件大小=“”:#更新为空字符串比较
如果filesize

我为您添加了注释以查看可能的修复。

filesize=int(filesize,2)您为什么需要这个?近似原因:第5行:在服务器
size=int(size)
中,应该是
size=int(size,2)
Secondary:Q:如果您调用
recv(16)
将返回多少字节()。答:不一定是16,可以是1到16之间的任何数字(如果对等方死亡,则为0)。TCP不保证保留消息边界。第三:与发送(缓冲区为X)相同。
:不保证发送X字节。使用
sendall
@gilmhamilton如何模拟“recvall()”?有时,这个问题再次出现。我如何才能确保收到正确的字节数?感谢您的回复,但这不起作用。我有这个错误:filesize-=len(数据)类型错误:不支持的操作数类型-=:'str'和'int'这是一个错误,它是固定的,很抱歉that@zatip还有其他问题吗?为什么你又重新提出这个问题了?是的。。。似乎已经解决了上述问题。。但在同一网络中的两台不同PC上尝试脚本时,同样的问题再次出现。chunksize=1024时,它可以在两台pc上运行,4096否,而在我的pc上运行4096 works的脚本。。我认为这是随机的,我相信这可能是由于套接字超时,否则我看不出答案有任何错误
while True:
       size = clientsocket.recv(16)
       if not size:
           break
       size = int(size)
       filename = clientsocket.recv(size)
       filesize = clientsocket.recv(32)
       filesize = int(filesize,2)
       file_to_write = open("/home/giorgio/Scrivania/SERVER/Download/"+'new_'+filename, 'wb')
       num_files += 1
       chunksize = 1024
       while filesize > 0:
           if filesize < chunksize:
               chunksize = filesize
           data = clientsocket.recv(chunksize)
           file_to_write.write(data)
           filesize -= len(data)

       file_to_write.close()
       print 'File received successfully'


serversock.close()
while True:
       size = clientsocket.recv(16)
       if not size:
           break
       size = int(size)
       filename = clientsocket.recv(size)
       filesize = clientsocket.recv(32)
       #filesize = int(filesize,2) #This is not required and this is causing the error so remove it.
       file_to_write = open("/home/giorgio/Scrivania/SERVER/Download/"+'new_'+filename, 'wb')
       num_files += 1
       chunksize = 1024
       while filesize !="": #update to empty string comparison 
           if filesize < chunksize:
               chunksize = filesize
           data = clientsocket.recv(chunksize)
           file_to_write.write(data)
           filesize = len(data)

       file_to_write.close()
       print 'File received successfully'


serversock.close()