I’m trying to learn Python. At the moment I’m stuck with a simple thing 🙁
When I send request with urllib2, it add “Connection:close” header. What should I do to send request WITHOUT this header?
My code:
request = urllib2.Request('http://example.com')
request.add_header('User-Agent', self.client_list[self.client])
opener = urllib2.build_opener()
opener.open(request).read()
Thanks guys, I’m very appreciate your help!
You can’t. Not with
urllib2in any case. From the source code:Use the
requestslibrary instead, it supports connection keep-alive:The above example uses http://httpbin.org to reflect the request headers; as you can see
requestsused aConnection: keep-aliveheader.