客户端服务器上的Python TCP文件传输
我有下面一段代码,根据客户机发送到服务器的信息将文件发送到客户机。客户端将发送特定名称的应用程序请求。服务器将获取文件并将其发送到客户端。问题是服务器发送以下消息:客户端服务器上的Python TCP文件传输,python,python-2.7,tcp,Python,Python 2.7,Tcp,我有下面一段代码,根据客户机发送到服务器的信息将文件发送到客户机。客户端将发送特定名称的应用程序请求。服务器将获取文件并将其发送到客户端。问题是服务器发送以下消息: Not a heartbeat No valid Message App request Done Done Sending Files 但即使服务器发送完文件,客户端也在监听文件 给出了服务器代码。给出了客户端代码 我在代码中做错了什么?您可以通过在发送文件内容之前发送文件的总长度来实现这一点。 首先接收长度并读取该长度 下面是
Not a heartbeat
No valid Message
App request Done
Done Sending Files
但即使服务器发送完文件,客户端也在监听文件
给出了服务器代码。给出了客户端代码
我在代码中做错了什么?您可以通过在发送文件内容之前发送文件的总长度来实现这一点。 首先接收长度并读取该长度 下面是有效的代码片段 在服务器端(server.py) 在客户端:
while True:
print "Sending App Request"
s.sendall("{'app':'windows_process_monitor', 'platform':'windows'}")
print "Receiving Files"
first_flag = True
with open("windows_process_monitor.dll", "wb") as dd:
app_len = 0
remaining_part_len = 0
while(True):
data = s.recv(1024)
print data
if not data:
break
if first_flag:
app_name, app_len_str, first_part_content = data.split("#", 2)
app_len += int(app_len_str)
remaining_part_len += app_len
first_received_len = len(first_part_content)
dd.write(first_part_content)
first_flag = False
if app_len <= first_received_len:
print "all received"
break
remaining_part_len -= first_received_len
else:
dd.write(data)
remaining_part_len -= 1024
if remaining_part_len <= 0:
break
print "inside while"
print "Received ", repr(data)
break
为True时:
打印“发送应用程序请求”
s、 sendall(“{'app':'windows\进程\监视器','platform':'windows'}”)
打印“接收文件”
第一个标志=真
以open(“windows\u process\u monitor.dll”、“wb”)作为dd:
app_len=0
剩余部分长度=0
虽然(正确):
数据=s.recv(1024)
打印数据
如果没有数据:
打破
如果第一个_标志:
app_name,app_len_str,第一部分内容=data.split(“#”,2)
app_len+=int(app_len_str)
剩余部分长度+=应用长度
第一个收到的长度=长度(第一部分内容)
dd.write(第一部分内容)
第一个标志=False
如果app_len I不擅长网络内容,但我认为您必须首先发送文件的大小,并使客户端在收到这么多字节后停止接收。客户端如何知道它已完成接收文件?记住socket.recv()是一个阻塞调用。必须删除无效消息返回类型中的{success:false}。
while True:
print "Sending App Request"
s.sendall("{'app':'windows_process_monitor', 'platform':'windows'}")
print "Receiving Files"
first_flag = True
with open("windows_process_monitor.dll", "wb") as dd:
app_len = 0
remaining_part_len = 0
while(True):
data = s.recv(1024)
print data
if not data:
break
if first_flag:
app_name, app_len_str, first_part_content = data.split("#", 2)
app_len += int(app_len_str)
remaining_part_len += app_len
first_received_len = len(first_part_content)
dd.write(first_part_content)
first_flag = False
if app_len <= first_received_len:
print "all received"
break
remaining_part_len -= first_received_len
else:
dd.write(data)
remaining_part_len -= 1024
if remaining_part_len <= 0:
break
print "inside while"
print "Received ", repr(data)
break