早上好,
我有一个简单的问题。
我需要一个允许客户端将XML发送到服务器的套接字。我尝试发送一个简单的字符串b'simple string just for example'
,并且一切正常,但是当我尝试发送XML(二进制)时,服务器无法将其记录下来。我将粘贴代码
客户
import socket
socket = socket.socket()
socket.connect(("localhost", 54610))
file = open(r"c:\shared\68118.xml", "rb")
stream = file.read(65536)
while stream:
socket.send(stream)
stream = file.read(65536) # tried with 1024, 2048
socket.close()
服务器
import socket
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('', 54610)
socket.bind(server_address)
socket.listen(1)
while True:
inbound_stream, address = socket.accept()
print(address)
counter = 1
file = open(r'c:\shared\68118_' + str(counter) + '.xml', 'wb') # binary
counter += 1
while(True):
stream = inbound_stream.recv(65536)
while stream:
file.write(stream)
stream = inbound_stream.recv(65536) # tried with 1024, 2048
file.close()
inbound_stream.close()
socket.close() # TODO NOT REACHABLE
输出文件为68118_1.xml
0Kb,里面没有任何内容
我做错了什么?
提前谢谢
答案 0 :(得分:0)
我刚刚从循环中删除了stream = file.read(65536) # tried with 1024, 2048
,一切正常