问题
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