Patch: httplib.py default timeout

Aahz Maruch aahz at netcom.com
Fri Dec 24 12:32:28 EST 1999


In article <Pine.LNX.4.21.9912221718440.3315-100000 at fep132.fep.ru>,
Oleg Broytmann  <phd at phd.russ.ru> wrote:
>On 22 Dec 1999, Aahz Maruch wrote:
>>
>> I've got a patch more-or-less ready to go to Guido; I'll probably send
>> it next week after we've tested it a bit more thoroughly.
>
>   Show it here for discussion...

Here's the top part of httplib.py; the end hasn't changed at all.  I'm
going to clean it up a bit more before submitting it to Guido.  The one
question I have left is whether the timeout should be specified in its
raw form (milliseconds) or in a more user-friendly form (seconds); I'm
currently leaving it as milliseconds:

"""HTTP client class

See the following URL for a description of the HTTP/1.0 protocol:
http://www.w3.org/hypertext/WWW/Protocols/
(I actually implemented it from a much earlier draft.)

Example:

>>> from httplib import HTTP
>>> h = HTTP('www.python.org')
>>> h.putrequest('GET', '/index.html')
>>> h.putheader('Accept', 'text/html')
>>> h.putheader('Accept', 'text/plain')
>>> h.endheaders()
>>> errcode, errmsg, headers = h.getreply()
>>> if errcode == 200:
...     f = h.getfile()
...     print f.read() # Print the raw HTML
...
<HEAD>
<TITLE>Python Language Home Page</TITLE>
[...many more lines...]
>>>

Note that an HTTP object is used for a single request -- to issue a
second request to the same server, you create a new HTTP object.
(This is in accordance with the protocol, which uses a new TCP
connection for each request.)
"""

import socket
import string
import mimetools

HTTP_VERSION = 'HTTP/1.0'
HTTP_PORT = 80
defaultTimeout = 60 * 1000   # Sixty seconds

class HTTP:
    """This class manages a connection to an HTTP server."""
    
    def __init__(self, host = '', port = 0, timeout = None):
        """Initialize a new instance.

        If specified, `host' is the name of the remote host to which
        to connect.  If specified, `port' specifies the port to which
        to connect.  By default, httplib.HTTP_PORT is used.

        """
        self.debuglevel = 0
        self.file = None
        if host: self.connect(host, port, timeout)
    
    def set_debuglevel(self, debuglevel):
        """Set the debug output level.

        A non-false value results in debug messages for connection and
        for all messages sent to and received from the server.

        """
        self.debuglevel = debuglevel
    
    def connect(self, host, port = 0, timeout = None):
        """Connect to a host on a given port with timeout in milliseconds.
        
        Note:  This method is automatically invoked by __init__,
        if a host is specified during instantiation.

        """
        if not port:
            i = string.find(host, ':')
            if i >= 0:
                host, port = host[:i], host[i+1:]
                try: port = string.atoi(port)
                except string.atoi_error:
                    raise socket.error, "nonnumeric port"
        if not port: port = HTTP_PORT
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if self.debuglevel > 0: print 'connect:', (host, port)
        self.sock.connect(host, port)
        if timeout is None:
            self.sock.setsockopt ( socket.SOL_SOCKET, socket.SO_RCVTIMEO, defaultTimeout )
        elif timeout > 0:
            self.sock.setsockopt ( socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout )
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

TEOTWAWKI -- 8 days and counting!



More information about the Python-list mailing list