Difference between revisions of "Customizing Python httplib"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 47: Line 47:
Maybe, it is time to learn about Python's self documenting feature:
Maybe, it is time to learn about Python's self documenting feature:


      from socket import setdefaulttimeout
<pre>
      help(setdefaulttimeout)
  from socket import setdefaulttimeout
  help(setdefaulttimeout)
</pre>


Help on built-in function setdefaulttimeout in module _socket:
Help on built-in function setdefaulttimeout in module _socket:


setdefaulttimeout(...)
<pre>
    setdefaulttimeout(timeout)
  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.


      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.
</pre>


This means:
This means:


<pre>
     # you import 'setdefaulttimeout'
     # you import 'setdefaulttimeout'
     from socket import setdefaulttimeout
     from socket import setdefaulttimeout
Line 67: Line 71:
     setdefaulttimeout(5)
     setdefaulttimeout(5)
     # any socket newly created after this call with have an initial timeout 5 s
     # any socket newly created after this call with have an initial timeout 5 s
</pre>
[[Category:Python]]
[[Category:Python httplib]]

Latest revision as of 14:06, 1 August 2015

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]

setdefaulttimeout

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.

This means:

     # 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