无法向Python发送大文件
我正在尝试为一个类实现一个简单的epollweb服务器。在我尝试发送一个大文件之前,一切都很顺利。我注意到它发送了大约2/3的数据,然后只是坐在那里什么也不做。以下是我如何确保发送所有数据的方法:无法向Python发送大文件,python,sockets,send,server,Python,Sockets,Send,Server,我正在尝试为一个类实现一个简单的epollweb服务器。在我尝试发送一个大文件之前,一切都很顺利。我注意到它发送了大约2/3的数据,然后只是坐在那里什么也不做。以下是我如何确保发送所有数据的方法: def sendResponse(self,fd,response): if response: totalsent = 0 MSGLEN = len(response) while totalsent < MSGLEN:
def sendResponse(self,fd,response):
if response:
totalsent = 0
MSGLEN = len(response)
while totalsent < MSGLEN:
sent = self.clients[fd].send(response[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
else:
self.poller.unregister(fd)
self.clients[fd].close()
del self.clients[fd]
def sendResponse(self、fd、response):
如果回答:
totalsent=0
MSGLEN=len(响应)
当totalsent
我有没有提到过这是中小型文件的作品。我只注意到它在尝试发送1.7 Mbs或更大的文件时会中断。如果可以,请跳过重新发明轮子和使用ZeroMQ消息层的阶段 这样,您的代码可能会忘记所有低级别的问题,而您的开发时间和精力都集中在您的问题域问题上。ZeroMQ提供了一个概念和一组现成的工具,用于添加(几乎)线性可伸缩性、资源池、故障恢复能力、更高层抽象协议等 对于最初的一块蛋糕,尝一尝从 花一周的时间阅读Pieters Hintjen的书“Code Connected,Vol.1”,将向您介绍分布式处理的新世界,您已经准备好随时可以使用大师的专有技术 你得到了什么? 极快的速度,无论是几个字节还是几个GB的BLOB,低延迟等等,都来自这样一个简单的代码:
def sendResponse( self, aZmqConnectionToCounterparty, response ):
if response:
try:
aZmqConnectionToCounterparty.send( response, zmq.NOBLOCK )
except ZMQerror:
''' handle ZMQerror '''
except ValueError:
''' handle ValueError '''
except TypeError:
''' handle TypeError '''
...
文件内容如下:
.send( data, flags = 0, copy = True, track = False )
Send a message on this socket.
This queues the message to be sent by the IO thread at a later time.
Parameters:
-----------
data: object, str, Frame The content of the message.
flags: int Any supported flag: NOBLOCK, SNDMORE.
copy: bool Should the message be sent in a copying or non-copying manner.
track: bool Should the message be tracked for notification
that ZMQ has finished with it? ( ignored if copy = True )
Returns:
--------
None: if copy or not track
None if message was sent, raises an exception otherwise.
MessageTracker: if track and not copy
a MessageTracker object, whose pending property will be True until the send is completed.
Raises:
-------
TypeError If a unicode object is passed
ValueError If track=True, but an untracked Frame is passed.
ZMQError If the send does not succeed for any reason.
如果代码停止发送并挂起,则发送缓冲区可能已满。客户端是否正确读取所有发送的数据?你的发送循环看起来不错。