Customizing Python httplib

From PeformIQ Upgrade
Revision as of 11:34, 17 March 2008 by PeterHarding (talk | contribs) (New page: =Introduction= 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 th...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

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 [dieter@handshake.de]