1994-02-21 16:36:04 +00:00
|
|
|
# HTTP client class
|
|
|
|
#
|
1995-09-30 16:50:46 +00:00
|
|
|
# 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.)
|
1994-02-21 16:36:04 +00:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# >>> from httplib import HTTP
|
1995-09-30 16:50:46 +00:00
|
|
|
# >>> h = HTTP('www.python.org')
|
|
|
|
# >>> h.putrequest('GET', '/index.html')
|
1994-02-21 16:36:04 +00:00
|
|
|
# >>> h.putheader('Accept', 'text/html')
|
|
|
|
# >>> h.putheader('Accept', 'text/plain')
|
1995-06-22 18:48:48 +00:00
|
|
|
# >>> h.endheaders()
|
1994-02-21 16:36:04 +00:00
|
|
|
# >>> errcode, errmsg, headers = h.getreply()
|
|
|
|
# >>> if errcode == 200:
|
|
|
|
# ... f = h.getfile()
|
|
|
|
# ... print f.read() # Print the raw HTML
|
|
|
|
# ...
|
1995-09-30 16:50:46 +00:00
|
|
|
# <HEAD>
|
|
|
|
# <TITLE>Python Language Home Page</TITLE>
|
1994-02-21 16:36:04 +00:00
|
|
|
# [...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 os
|
|
|
|
import socket
|
|
|
|
import string
|
|
|
|
import regex
|
|
|
|
import regsub
|
1995-08-07 20:13:02 +00:00
|
|
|
import mimetools
|
1994-02-21 16:36:04 +00:00
|
|
|
|
|
|
|
HTTP_VERSION = 'HTTP/1.0'
|
1996-12-10 15:58:22 +00:00
|
|
|
HTTP_VERSIONS_ACCEPTED = 'HTTP/1\.[0-9.]+'
|
1994-02-21 16:36:04 +00:00
|
|
|
HTTP_PORT = 80
|
|
|
|
|
1996-12-10 15:58:22 +00:00
|
|
|
replypat = HTTP_VERSIONS_ACCEPTED + '[ \t]+\([0-9][0-9][0-9]\)\(.*\)'
|
1994-02-21 16:36:04 +00:00
|
|
|
replyprog = regex.compile(replypat)
|
|
|
|
|
|
|
|
class HTTP:
|
|
|
|
|
1994-05-06 14:28:19 +00:00
|
|
|
def __init__(self, host = '', port = 0):
|
1994-02-21 16:36:04 +00:00
|
|
|
self.debuglevel = 0
|
1995-08-07 20:13:02 +00:00
|
|
|
self.file = None
|
1994-05-06 14:28:19 +00:00
|
|
|
if host: self.connect(host, port)
|
1994-02-21 16:36:04 +00:00
|
|
|
|
|
|
|
def set_debuglevel(self, debuglevel):
|
|
|
|
self.debuglevel = debuglevel
|
|
|
|
|
1994-05-06 14:28:19 +00:00
|
|
|
def connect(self, host, port = 0):
|
|
|
|
if not port:
|
1994-02-21 16:36:04 +00:00
|
|
|
i = string.find(host, ':')
|
|
|
|
if i >= 0:
|
|
|
|
host, port = host[:i], host[i+1:]
|
|
|
|
try: port = string.atoi(port)
|
1995-09-30 16:50:46 +00:00
|
|
|
except string.atoi_error:
|
|
|
|
raise socket.error, "nonnumeric port"
|
1994-02-21 16:36:04 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
def send(self, str):
|
|
|
|
if self.debuglevel > 0: print 'send:', `str`
|
|
|
|
self.sock.send(str)
|
|
|
|
|
|
|
|
def putrequest(self, request, selector):
|
1995-06-22 18:48:48 +00:00
|
|
|
if not selector: selector = '/'
|
1994-02-21 16:36:04 +00:00
|
|
|
str = '%s %s %s\r\n' % (request, selector, HTTP_VERSION)
|
|
|
|
self.send(str)
|
|
|
|
|
|
|
|
def putheader(self, header, *args):
|
|
|
|
str = '%s: %s\r\n' % (header, string.joinfields(args,'\r\n\t'))
|
|
|
|
self.send(str)
|
|
|
|
|
|
|
|
def endheaders(self):
|
|
|
|
self.send('\r\n')
|
|
|
|
|
|
|
|
def getreply(self):
|
1996-02-14 16:06:24 +00:00
|
|
|
self.file = self.sock.makefile('rb')
|
1995-06-22 18:48:48 +00:00
|
|
|
self.sock = None
|
1994-02-21 16:36:04 +00:00
|
|
|
line = self.file.readline()
|
|
|
|
if self.debuglevel > 0: print 'reply:', `line`
|
|
|
|
if replyprog.match(line) < 0:
|
|
|
|
self.headers = None
|
|
|
|
return -1, line, self.headers
|
|
|
|
errcode, errmsg = replyprog.group(1, 2)
|
|
|
|
errcode = string.atoi(errcode)
|
|
|
|
errmsg = string.strip(errmsg)
|
1995-08-07 20:13:02 +00:00
|
|
|
self.headers = mimetools.Message(self.file, 0)
|
1994-02-21 16:36:04 +00:00
|
|
|
return errcode, errmsg, self.headers
|
|
|
|
|
|
|
|
def getfile(self):
|
|
|
|
return self.file
|
|
|
|
|
1995-08-07 20:13:02 +00:00
|
|
|
def close(self):
|
|
|
|
if self.file:
|
|
|
|
self.file.close()
|
|
|
|
self.file = None
|
|
|
|
|
1994-02-21 16:36:04 +00:00
|
|
|
|
|
|
|
def test():
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'd')
|
|
|
|
dl = 0
|
|
|
|
for o, a in opts:
|
|
|
|
if o == '-d': dl = dl + 1
|
1995-09-07 19:28:19 +00:00
|
|
|
host = 'www.python.org'
|
|
|
|
selector = '/'
|
1994-02-21 16:36:04 +00:00
|
|
|
if args[0:]: host = args[0]
|
|
|
|
if args[1:]: selector = args[1]
|
|
|
|
h = HTTP()
|
|
|
|
h.set_debuglevel(dl)
|
|
|
|
h.connect(host)
|
|
|
|
h.putrequest('GET', selector)
|
1995-09-07 19:28:19 +00:00
|
|
|
h.endheaders()
|
1994-02-21 16:36:04 +00:00
|
|
|
errcode, errmsg, headers = h.getreply()
|
|
|
|
print 'errcode =', errcode
|
|
|
|
print 'errmsg =', errmsg
|
1995-09-07 19:28:19 +00:00
|
|
|
print
|
1994-02-21 16:36:04 +00:00
|
|
|
if headers:
|
|
|
|
for header in headers.headers: print string.strip(header)
|
1995-09-07 19:28:19 +00:00
|
|
|
print
|
1994-02-21 16:36:04 +00:00
|
|
|
print h.getfile().read()
|
|
|
|
|
1995-09-07 19:28:19 +00:00
|
|
|
|
1994-02-21 16:36:04 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test()
|