3 # See the following URL for a description of the HTTP/1.0 protocol:
4 # http://www.w3.org/hypertext/WWW/Protocols/
5 # (I actually implemented it from a much earlier draft.)
9 # >>> from httplib import HTTP
10 # >>> h = HTTP('www.python.org')
11 # >>> h.putrequest('GET', '/index.html')
12 # >>> h.putheader('Accept', 'text/html')
13 # >>> h.putheader('Accept', 'text/plain')
15 # >>> errcode, errmsg, headers = h.getreply()
16 # >>> if errcode == 200:
18 # ... print f.read() # Print the raw HTML
21 # <TITLE>Python Language Home Page</TITLE>
22 # [...many more lines...]
25 # Note that an HTTP object is used for a single request -- to issue a
26 # second request to the same server, you create a new HTTP object.
27 # (This is in accordance with the protocol, which uses a new TCP
28 # connection for each request.)
38 HTTP_VERSION
= 'HTTP/1.0'
41 replypat
= regsub
.gsub('\\.', '\\\\.', HTTP_VERSION
) + \
42 '[ \t]+\([0-9][0-9][0-9]\)\(.*\)'
43 replyprog
= regex
.compile(replypat
)
47 def __init__(self
, host
= '', port
= 0):
50 if host
: self
.connect(host
, port
)
52 def set_debuglevel(self
, debuglevel
):
53 self
.debuglevel
= debuglevel
55 def connect(self
, host
, port
= 0):
57 i
= string
.find(host
, ':')
59 host
, port
= host
[:i
], host
[i
+1:]
60 try: port
= string
.atoi(port
)
61 except string
.atoi_error
:
62 raise socket
.error
, "nonnumeric port"
63 if not port
: port
= HTTP_PORT
64 self
.sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
65 if self
.debuglevel
> 0: print 'connect:', (host
, port
)
66 self
.sock
.connect(host
, port
)
69 if self
.debuglevel
> 0: print 'send:', `
str`
72 def putrequest(self
, request
, selector
):
73 if not selector
: selector
= '/'
74 str = '%s %s %s\r\n' % (request
, selector
, HTTP_VERSION
)
77 def putheader(self
, header
, *args
):
78 str = '%s: %s\r\n' % (header
, string
.joinfields(args
,'\r\n\t'))
85 self
.file = self
.sock
.makefile('rb')
87 line
= self
.file.readline()
88 if self
.debuglevel
> 0: print 'reply:', `line`
89 if replyprog
.match(line
) < 0:
91 return -1, line
, self
.headers
92 errcode
, errmsg
= replyprog
.group(1, 2)
93 errcode
= string
.atoi(errcode
)
94 errmsg
= string
.strip(errmsg
)
95 self
.headers
= mimetools
.Message(self
.file, 0)
96 return errcode
, errmsg
, self
.headers
110 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'd')
113 if o
== '-d': dl
= dl
+ 1
114 host
= 'www.python.org'
116 if args
[0:]: host
= args
[0]
117 if args
[1:]: selector
= args
[1]
121 h
.putrequest('GET', selector
)
123 errcode
, errmsg
, headers
= h
.getreply()
124 print 'errcode =', errcode
125 print 'errmsg =', errmsg
128 for header
in headers
.headers
: print string
.strip(header
)
130 print h
.getfile().read()
133 if __name__
== '__main__':