Customizing Python httplib
Here are some notes I have come across which relate to different customizations of httplib.
Customizing Socket Timeouts
Today, I have read in other messages in this mailing list that ZSI uses "httplib" and in what ZSI parts customizations to "httplib" need to be incorporated (--> see the thread about "http proxy" use).
To ensure, that "httplib.HTTP" uses sockets with a given timeout, you give it a non standard "_connection_class". When you do not want to modify the behaviour of "HTTP" globally, you make a derived class and use this instead of "HTTP" in ZSI.
An adequate connection class could look like "HTTPConnectionWithTimeout" below:
from httplib import HTTPConnection import socket from dm.reuse import rebindFunction class _SocketWrapper(object): '''a (very partial) emulation of 'socket' with just the 'socket' attribute.''' def __init__(self, timeout): self.timeout = timeout def socket(self, *args, **kw): sock = socket.socket(*args, **kw): if self.timeout: sock.settimeout(self.timeout) return sock class HTTPConnectionWithTimeout(HTTPConnection): connect = rebindFunction(HTTPConnection.connect, socket=_SocketWrapper(<your timeout>)
You find "dm.reuse" on "PyPI". It is easier to install when you have "setuptools" installed.
If you do not want to install "dm.reuse", you can copy the "connect" method and add the "sock.settimeout" call in your copy.
- Dieter Maurer [firstname.lastname@example.org]
Maybe, it is time to learn about Python's self documenting feature:
from socket import setdefaulttimeout help(setdefaulttimeout)
Help on built-in function setdefaulttimeout in module _socket:
setdefaulttimeout(...) setdefaulttimeout(timeout) Set the default timeout in floating seconds for new socket objects. A value of None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.
# you import 'setdefaulttimeout' from socket import setdefaulttimeout # then you call it with your timeout, e.g. 5 s setdefaulttimeout(5) # any socket newly created after this call with have an initial timeout 5 s