unbuffered urllib2.urlopen

匆匆过客 提交于 2019-12-07 11:17:29

A quick hack that has occurred to me is to use urllib.urlopen() with threading.Timer() to emulate timeout. But that's only quick and dirty hack.

urllib2 is buffered when you just call read()

you could define a size to read and therefore disable buffering.

for example:

import urllib2

CHUNKSIZE = 80

r = urllib2.urlopen('http://www.python.org')
while True:
    chunk = r.read(CHUNKSIZE)
    if not chunk:
        break
    print(chunk)

this would print the response after each chunk is read from the socket, not buffer until the entire response is received.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!