3
我正在阅读下面代码中所述请求库的XML事件。一旦请求启动,我该如何提出连接丢失错误?服务器正在模拟HTTP推送/长轮询 - >http://en.wikipedia.org/wiki/Push_technology#Long_polling,并且默认情况下不会结束。 如果10分钟后没有新消息,则应退出while循环。Python,流请求期间捕获超时
import requests
from time import time
if __name__ == '__main__':
#: Set a default content-length
content_length = 512
try:
requests_stream = requests.get('http://agent.mtconnect.org:80/sample?interval=0', stream=True, timeout=2)
while True:
start_time = time()
#: Read three lines to determine the content-length
for line in requests_stream.iter_lines(3, decode_unicode=None):
if line.startswith('Content-length'):
content_length = int(''.join(x for x in line if x.isdigit()))
#: pause the generator
break
#: Continue the generator and read the exact amount of the body.
for xml in requests_stream.iter_content(content_length):
print "Received XML document with content length of %s in %s seconds" % (len(xml), time() - start_time)
break
except requests.exceptions.RequestException as e:
print('error: ', e)
服务器推送可以与卷曲通过命令行进行测试:
curl http://agent.mtconnect.org:80/sample\?interval\=0
嗯,它看起来像它的作品。但是,我只收到每5秒一条XML消息。我需要尽可能快地得到这些;) – wiesson
5秒钟的睡眠实际上并不会挂起子进程。它只是睡觉的主线。 XML消息应该在子进程中返回时立即处理。很可能,服务器或“请求”模块正在增加5秒的延迟时间。 –
如果它的工作,你可能会继续并接受答案:) –