2010-01-05 25 views

回答

25

如果切換到httplib,您將更好地控制底層連接。

例如:

import httplib 

conn = httplib.HTTPConnection(url) 

conn.request('GET', '/foo') 
r1 = conn.getresponse() 
r1.read() 

conn.request('GET', '/bar') 
r2 = conn.getresponse() 
r2.read() 

conn.close() 

這將發送相同的基礎TCP連接上2 HTTP的GET。

+0

這是一個很好的答案,因爲httplib是python的一部分。這使我們不必安裝第三方模塊。謝謝! – 2013-04-14 16:54:25

+0

也許這會對有用的人有用,也有HTTPSConnection。 – Petr 2016-05-12 12:26:17

2

我以前使用第三方庫urllib3效果不錯。它旨在通過彙集連接以便重用來補充urllib2。從the wiki

修改例如:

>>> from urllib3 import HTTPConnectionPool 
>>> # Create a connection pool for a specific host 
... http_pool = HTTPConnectionPool('www.google.com') 
>>> # simple GET request, for example 
... r = http_pool.urlopen('GET', '/') 
>>> print r.status, len(r.data) 
200 28050 
>>> r = http_pool.urlopen('GET', '/search?q=hello+world') 
>>> print r.status, len(r.data) 
200 79124 
+0

我無法找到此庫,鏈接已死亡。你介意看看http://stackoverflow.com/questions/18221809/sending-a-few-requests-using-one-connection? – 2013-08-14 04:01:50

0

如果你需要的東西比普通httplib的更加自動化,這可能幫助,但它不是線程安全的。

try: 
    from http.client import HTTPConnection, HTTPSConnection 
except ImportError: 
    from httplib import HTTPConnection, HTTPSConnection 
import select 
connections = {} 


def request(method, url, body=None, headers={}, **kwargs): 
    scheme, _, host, path = url.split('/', 3) 
    h = connections.get((scheme, host)) 
    if h and select.select([h.sock], [], [], 0)[0]: 
     h.close() 
     h = None 
    if not h: 
     Connection = HTTPConnection if scheme == 'http:' else HTTPSConnection 
     h = connections[(scheme, host)] = Connection(host, **kwargs) 
    h.request(method, '/' + path, body, headers) 
    return h.getresponse() 


def urlopen(url, data=None, *args, **kwargs): 
    resp = request('POST' if data else 'GET', url, data, *args, **kwargs) 
    assert resp.status < 400, (resp.status, resp.reason, resp.read()) 
    return resp