mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Doc strings by Mitch Chapman (with a little reformatting).
Also reformatted the whole module with 4 spaces and no tabs.
This commit is contained in:
parent
1707aad27c
commit
41999c164e
1 changed files with 163 additions and 107 deletions
112
Lib/httplib.py
112
Lib/httplib.py
|
@ -1,32 +1,32 @@
|
|||
# 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.)
|
||||
"""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
|
||||
|
@ -36,16 +36,36 @@ HTTP_VERSION = 'HTTP/1.0'
|
|||
HTTP_PORT = 80
|
||||
|
||||
class HTTP:
|
||||
"""This class manages a connection to an HTTP server."""
|
||||
|
||||
def __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.
|
||||
|
||||
"""
|
||||
self.debuglevel = 0
|
||||
self.file = None
|
||||
if host: self.connect(host, port)
|
||||
|
||||
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):
|
||||
"""Connect to a host on a given port.
|
||||
|
||||
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:
|
||||
|
@ -59,22 +79,44 @@ class HTTP:
|
|||
self.sock.connect(host, port)
|
||||
|
||||
def send(self, str):
|
||||
"""Send `str' to the server."""
|
||||
if self.debuglevel > 0: print 'send:', `str`
|
||||
self.sock.send(str)
|
||||
|
||||
def 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.
|
||||
'/index.html'.
|
||||
|
||||
"""
|
||||
if not selector: selector = '/'
|
||||
str = '%s %s %s\r\n' % (request, selector, HTTP_VERSION)
|
||||
self.send(str)
|
||||
|
||||
def putheader(self, header, *args):
|
||||
"""Send a request header line to the server.
|
||||
|
||||
For example: h.putheader('Accept', 'text/html')
|
||||
|
||||
"""
|
||||
str = '%s: %s\r\n' % (header, string.joinfields(args,'\r\n\t'))
|
||||
self.send(str)
|
||||
|
||||
def endheaders(self):
|
||||
"""Indicate that the last header line has been sent to the server."""
|
||||
self.send('\r\n')
|
||||
|
||||
def getreply(self):
|
||||
"""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
|
||||
|
||||
"""
|
||||
self.file = self.sock.makefile('rb')
|
||||
line = self.file.readline()
|
||||
if self.debuglevel > 0: print 'reply:', `line`
|
||||
|
@ -92,9 +134,16 @@ class HTTP:
|
|||
return errcode, errmsg, self.headers
|
||||
|
||||
def getfile(self):
|
||||
"""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.
|
||||
|
||||
"""
|
||||
return self.file
|
||||
|
||||
def close(self):
|
||||
"""Close the connection to the HTTP server."""
|
||||
if self.file:
|
||||
self.file.close()
|
||||
self.file = None
|
||||
|
@ -104,6 +153,13 @@ class HTTP:
|
|||
|
||||
|
||||
def test():
|
||||
"""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.
|
||||
|
||||
"""
|
||||
import sys
|
||||
import getopt
|
||||
opts, args = getopt.getopt(sys.argv[1:], 'd')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue