blob: 68c9d8b3acfa127d9b13d3dfb91fdf62fb05b070 [file] [log] [blame]
Guido van Rossum23acc951994-02-21 16:36:04 +00001# HTTP client class
2#
3# See the following document for a tentative protocol description:
4# Hypertext Transfer Protocol (HTTP) Tim Berners-Lee, CERN
5# Internet Draft 5 Nov 1993
6# draft-ietf-iiir-http-00.txt Expires 5 May 1994
7#
8# Example:
9#
10# >>> from httplib import HTTP
11# >>> h = HTTP('www.cwi.nl')
12# >>> h.putreqest('GET', '/index.html')
13# >>> h.putheader('Accept', 'text/html')
14# >>> h.putheader('Accept', 'text/plain')
Guido van Rossum4cdcef71995-06-22 18:48:48 +000015# >>> h.endheaders()
Guido van Rossum23acc951994-02-21 16:36:04 +000016# >>> errcode, errmsg, headers = h.getreply()
17# >>> if errcode == 200:
18# ... f = h.getfile()
19# ... print f.read() # Print the raw HTML
20# ...
21# <TITLE>Home Page of CWI, Amsterdam</TITLE>
22# [...many more lines...]
23# >>>
24#
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.)
29
30
31import os
32import socket
33import string
34import regex
35import regsub
Guido van Rossum65ab98c1995-08-07 20:13:02 +000036import mimetools
Guido van Rossum23acc951994-02-21 16:36:04 +000037
38HTTP_VERSION = 'HTTP/1.0'
39HTTP_PORT = 80
40
41replypat = regsub.gsub('\\.', '\\\\.', HTTP_VERSION) + \
42 '[ \t]+\([0-9][0-9][0-9]\)\(.*\)'
43replyprog = regex.compile(replypat)
44
45class HTTP:
46
Guido van Rossum2922c6d1994-05-06 14:28:19 +000047 def __init__(self, host = '', port = 0):
Guido van Rossum23acc951994-02-21 16:36:04 +000048 self.debuglevel = 0
Guido van Rossum65ab98c1995-08-07 20:13:02 +000049 self.file = None
Guido van Rossum2922c6d1994-05-06 14:28:19 +000050 if host: self.connect(host, port)
Guido van Rossum23acc951994-02-21 16:36:04 +000051
52 def set_debuglevel(self, debuglevel):
53 self.debuglevel = debuglevel
54
Guido van Rossum2922c6d1994-05-06 14:28:19 +000055 def connect(self, host, port = 0):
56 if not port:
Guido van Rossum23acc951994-02-21 16:36:04 +000057 i = string.find(host, ':')
58 if i >= 0:
59 host, port = host[:i], host[i+1:]
60 try: port = string.atoi(port)
Guido van Rossum76ca3c11994-02-22 16:06:02 +000061 except string.atoi_error: pass
Guido van Rossum23acc951994-02-21 16:36:04 +000062 if not port: port = HTTP_PORT
63 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
64 if self.debuglevel > 0: print 'connect:', (host, port)
65 self.sock.connect(host, port)
66
67 def send(self, str):
68 if self.debuglevel > 0: print 'send:', `str`
69 self.sock.send(str)
70
71 def putrequest(self, request, selector):
Guido van Rossum4cdcef71995-06-22 18:48:48 +000072 if not selector: selector = '/'
Guido van Rossum23acc951994-02-21 16:36:04 +000073 str = '%s %s %s\r\n' % (request, selector, HTTP_VERSION)
74 self.send(str)
75
76 def putheader(self, header, *args):
77 str = '%s: %s\r\n' % (header, string.joinfields(args,'\r\n\t'))
78 self.send(str)
79
80 def endheaders(self):
81 self.send('\r\n')
82
Guido van Rossum23acc951994-02-21 16:36:04 +000083 def getreply(self):
Guido van Rossum23acc951994-02-21 16:36:04 +000084 self.file = self.sock.makefile('r')
Guido van Rossum4cdcef71995-06-22 18:48:48 +000085 self.sock = None
Guido van Rossum23acc951994-02-21 16:36:04 +000086 line = self.file.readline()
87 if self.debuglevel > 0: print 'reply:', `line`
88 if replyprog.match(line) < 0:
89 self.headers = None
90 return -1, line, self.headers
91 errcode, errmsg = replyprog.group(1, 2)
92 errcode = string.atoi(errcode)
93 errmsg = string.strip(errmsg)
Guido van Rossum65ab98c1995-08-07 20:13:02 +000094 self.headers = mimetools.Message(self.file, 0)
Guido van Rossum23acc951994-02-21 16:36:04 +000095 return errcode, errmsg, self.headers
96
97 def getfile(self):
98 return self.file
99
Guido van Rossum65ab98c1995-08-07 20:13:02 +0000100 def close(self):
101 if self.file:
102 self.file.close()
103 self.file = None
104
Guido van Rossum23acc951994-02-21 16:36:04 +0000105
106def test():
107 import sys
108 import getopt
109 opts, args = getopt.getopt(sys.argv[1:], 'd')
110 dl = 0
111 for o, a in opts:
112 if o == '-d': dl = dl + 1
113 host = 'www.cwi.nl:80'
114 selector = '/index.html'
115 if args[0:]: host = args[0]
116 if args[1:]: selector = args[1]
117 h = HTTP()
118 h.set_debuglevel(dl)
119 h.connect(host)
120 h.putrequest('GET', selector)
121 errcode, errmsg, headers = h.getreply()
122 print 'errcode =', errcode
123 print 'headers =', headers
124 print 'errmsg =', errmsg
125 if headers:
126 for header in headers.headers: print string.strip(header)
127 print h.getfile().read()
128
129if __name__ == '__main__':
130 test()