Do objects created by urllib2.urlopen() represent a constant connection?

风格不统一 提交于 2019-12-02 05:04:24

问题


In the following code, is the connection to the remote server held open until close() is called or is it recreated every time read() is called? In the following code I do see a new network communication happens every time read() is called, rather than the remote file being buffered as soon as urlopen() is called.

import urllib2

handle = urllib2.urlopen('http://download.thinkbroadband.com/5MB.zip')
while True:
    buff = handle.read(64*1024) # Is a new connection to the server created here?
    if len(x) == 0:
        break
handle.close()

回答1:


Try running wireshark or fiddler and look at port 80 with nothing else running. Run your program and see what traffic you get. Should answer your question.



来源:https://stackoverflow.com/questions/10308375/do-objects-created-by-urllib2-urlopen-represent-a-constant-connection

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