httplib会重用TCP连接吗?

httplib会重用TCP连接吗?

问题描述:

我正在使用httplib从网站上获取大量资源,并且我希望以最低的成本获得它,因此我在请求中设置了"Connection:keep-alive" HTTP标头,但我不确定它实际上使用的是相同的HTTP标头TCP连接可用于Web服务器允许的尽可能多的请求.

I'm using httplib to grab bunch of resources from a website and i want it at minimum cost, so i set 'Connection: keep-alive' HTTP header on my requests but i'm not sure it actually uses the same TCP connection for as many requests as the webserver allows.

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

我的实现对吗? 保持活力吗? 我应该将"con = httplib.HTTPConnection("myweb.com"))置于循环之外吗?

Is my implementation right? does keep-alive work? Should i put 'con = httplib.HTTPConnection("myweb.com")' out of the loop?

P.S:Web服务器对保持活动状态的响应正常, 我知道urllib3

P.S: the web server's response to keep-alive is ok, i'm aware of urllib3

您的示例每次通过循环都会创建一个新的TCP连接,因此不会,它不会重用该连接.

your example creates a new TCP connection each time through the loops, so no, it will not reuse that connection.

这个怎么样?

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

此外,如果只需要标题,则可以使用HTTP HEAD方法,而不是调用GET并丢弃内容.

also, if all you want is headers, you can use the HTTP HEAD method, rather than calling GET and discarding the content.