HTTP client class
See the following URL for a description of the HTTP/1.0 protocol:
(I actually implemented it from a much earlier draft.)
>>> from httplib import HTTP
>>> h = HTTP('www.python.org')
>>> h.putrequest('GET', '/index.html')
>>> h.putheader('Accept', 'text/html')
>>> h.putheader('Accept', 'text/plain')
>>> errcode, errmsg, headers = h.getreply()
>>> if errcode == 200:
... f = h.getfile()
... print f.read() # Print the raw HTML
<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.)
||This class manages a connection to an HTTP server.
- __init__(self, host='', port=0)
- 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.
- Close the connection to the HTTP server.
- connect(self, host, port=0)
- Connect to a host on a given port.
- Note: This method is automatically invoked by __init__,
- if a host is specified during instantiation.
- Indicate that the last header line has been sent to the server.
- Get a file object from which to receive data from the HTTP server.
- NOTE: This method must not be invoked until getreplies
- has been invoked.
- Get a reply from the server.
- Returns a tuple consisting of:
- - server response code (e.g. '200' if all goes well)
- - server response string corresponding to response code
- - any RFC822 headers in the response from the server
- putheader(self, header, *args)
- Send a request header line to the server.
- For example: h.putheader('Accept', 'text/html')
- putrequest(self, request, selector)
- Send a request to the server.
- `request' specifies an HTTP request method, e.g. 'GET'.
- `selector' specifies the object being requested, e.g.
- send(self, str)
- Send `str' to the server.
- 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.
- Test this module.
- The test consists of retrieving and displaying the Python
- home page, along with the error code and error string returned
- by the www.python.org server.