I am using urllib2 for loading web-page, my code is:
httpRequest = urllib2.Request("http:/www....com")
pageContent = urllib2.urlopen(httpRequest)
pageContent.readline()
How can I get hold of the socket properties to set TCP_NODELAY
?
In normal socket I would be using function:
socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
For requests, the classes seem to be in request.packages.urllib3
; there are 2 classes,
HTTPConnection, and HTTPSConnection. They should be monkeypatchable in place at the module top level:
from requests.packages.urllib3 import connectionpool
_HTTPConnection = connectionpool.HTTPConnection
_HTTPSConnection = connectionpool.HTTPSConnection
class HTTPConnection(_HTTPConnection):
def connect(self):
_HTTPConnection.connect(self)
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
class HTTPSConnection(_HTTPSConnection):
def connect(self):
_HTTPSConnection.connect(self)
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
connectionpool.HTTPConnection = HTTPConnection
connectionpool.HTTPSConnection = HTTPSConnection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With