Python Socket接收大量数据 [英] Python Socket Receive Large Amount of Data
问题描述
当我尝试接收大量数据时,它会被切断,我必须按 Enter 才能获取其余数据.起初我能够增加一点,但它仍然不会收到全部.正如你所看到的,我已经增加了 conn.recv() 的缓冲区,但它仍然没有得到所有的数据.它在某个点切断它.我必须在我的 raw_input 上按 Enter 才能接收其余的数据.无论如何我可以一次获取所有数据吗?这是代码.
When I try to receive larger amounts of data it gets cut off and I have to press enter to get the rest of the data. At first I was able to increase it a little bit but it still won't receive all of it. As you can see I have increased the buffer on the conn.recv() but it still doesn't get all of the data. It cuts it off at a certain point. I have to press enter on my raw_input in order to receive the rest of the data. Is there anyway I can get all of the data at once? Here's the code.
port = 7777
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', port))
sock.listen(1)
print ("Listening on port: "+str(port))
while 1:
conn, sock_addr = sock.accept()
print "accepted connection from", sock_addr
while 1:
command = raw_input('shell> ')
conn.send(command)
data = conn.recv(8000)
if not data: break
print data,
conn.close()
推荐答案
TCP/IP 是一种基于流的协议,而不是一种基于消息的协议.不能保证一个对等方的每次 send()
调用都会导致另一个对等方接收到发送的确切数据的单个 recv()
调用——它可能会收到数据片-meal,由于数据包碎片,分成多个 recv()
调用.
TCP/IP is a stream-based protocol, not a message-based protocol. There's no guarantee that every send()
call by one peer results in a single recv()
call by the other peer receiving the exact data sent—it might receive the data piece-meal, split across multiple recv()
calls, due to packet fragmentation.
您需要在 TCP 之上定义自己的基于消息的协议,以区分消息边界.然后,要阅读一条消息,您可以继续调用 recv()
,直到您阅读完整条消息或发生错误为止.
You need to define your own message-based protocol on top of TCP in order to differentiate message boundaries. Then, to read a message, you continue to call recv()
until you've read an entire message or an error occurs.
发送消息的一种简单方法是在每条消息前加上其长度.然后要读取消息,首先读取长度,然后读取那么多字节.您可以这样做:
One simple way of sending a message is to prefix each message with its length. Then to read a message, you first read the length, then you read that many bytes. Here's how you might do that:
def send_msg(sock, msg):
# Prefix each message with a 4-byte length (network byte order)
msg = struct.pack('>I', len(msg)) + msg
sock.sendall(msg)
def recv_msg(sock):
# Read message length and unpack it into an integer
raw_msglen = recvall(sock, 4)
if not raw_msglen:
return None
msglen = struct.unpack('>I', raw_msglen)[0]
# Read the message data
return recvall(sock, msglen)
def recvall(sock, n):
# Helper function to recv n bytes or return None if EOF is hit
data = bytearray()
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data.extend(packet)
return data
然后就可以使用send_msg
和recv_msg
函数来发送和接收整个消息,并且它们不会在网络上拆分或合并数据包时出现任何问题水平.
Then you can use the send_msg
and recv_msg
functions to send and receive whole messages, and they won't have any problems with packets being split or coalesced on the network level.
这篇关于Python Socket接收大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!