我正在使用httplib从一个网站获取大量资源,我想以最低的成本,所以我在我的请求上设置'连接:保持活动'HTTP标头,但我不确定它实际上使用相同的TCP连接尽可能多的请求与网络服务器允许.
i = 0
while 1:
i += 1
print i
con = httplib.HTTPConnection("myweb.com")
con.request("GET", "/x.css", headers={"Connection":" keep-alive"})
result = con.getresponse()
print result.reason, result.getheaders()
Run Code Online (Sandbox Code Playgroud)
我的实施是对的吗?保持活力吗?我应该把'con = httplib.HTTPConnection("myweb.com")'放出循环吗?
PS:网络服务器对keep-alive的响应没问题,我知道urllib3
您的示例每次通过循环创建一个新的TCP连接,所以不,它不会重用该连接.
这个怎么样?
con = httplib.HTTPConnection("myweb.com")
while True:
con.request("GET", "/x.css", headers={"Connection":" keep-alive"})
result = con.getresponse()
result.read()
print result.reason, result.getheaders()
Run Code Online (Sandbox Code Playgroud)
另外,如果你想要的只是标题,你可以使用HTTP HEAD方法,而不是调用GET并丢弃内容.