而且不能從source code:。
# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
使用requests
library代替,它支持連接保持活動:
>>> import requests
>>> import pprint
>>> r = requests.get('http://httpbin.org/get')
>>> pprint.pprint(r.json)
{u'args': {},
u'headers': {u'Accept': u'*/*',
u'Accept-Encoding': u'gzip, deflate, compress',
u'Connection': u'keep-alive',
u'Content-Length': u'',
u'Content-Type': u'',
u'Host': u'httpbin.org',
u'User-Agent': u'python-requests/0.14.1 CPython/2.6.8 Darwin/11.4.2'},
u'origin': u'176.11.12.149',
u'url': u'http://httpbin.org/get'}
以上的例子使用http://httpbin.org以反映請求頭;正如你所見,requests
使用了一個Connection: keep-alive
標題。