我正在尝试向网站发送HTTP请求并读取它返回的数据。我尝试成功的第一个网站。它返回了大约4个数据包,然后返回了一个脚本捕获并终止的0包。如何阅读Python套接字recv
但是,试图加载http://www.google.com/不能这样工作。相反,它返回大约10个相同长度的数据包,最后一个较小的数据包,然后继续超时。这是否正常?这一切都取决于主机正在使用的服务器?
如果任何人都可以推荐一种用socket.recv()读取的替代方法,它会考虑到并不总是发送最终的空数据包,这将不胜感激。谢谢。
try:
data = s.recv(4096)
while True:
more = s.recv(4096)
print len(more)
if not more:
break
else:
data += more
except socket.timeout:
errMsg = "Connection timed-out while connecting to %s. Request headers were as follows: %s", (parsedUrl.netloc, rHeader.headerContent)
self.logger.exception(errMsg)
raise Exception
你能用上面的代码编辑你的问题来查看你的请求吗? – sean