Skip to main content
 首页 » 编程设计

python之urllib2.urlopen() 创建的对象是否代表一个常量连接

2025年05月04日10emanlee

在下面的代码中,与远程服务器的连接是在调用 close() 之前保持打开状态,还是在每次调用 read() 时都重新创建?在下面的代码中,我确实看到每次调用 read() 时都会发生新的网络通信,而不是在调用 urlopen() 时立即缓冲远程文件。

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() 

请您参考如下方法:

尝试运行 wireshark 或 fiddler 并在没有其他运行的情况下查看端口 80。运行你的程序,看看你得到了什么流量。应该回答你的问题。