blob: 362b38d59708a2729ad240971d80043c8645b069 [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')
15# >>> errcode, errmsg, headers = h.getreply()
16# >>> if errcode == 200:
17# ... f = h.getfile()
18# ... print f.read() # Print the raw HTML
19# ...
20# <TITLE>Home Page of CWI, Amsterdam</TITLE>
21# [...many more lines...]
22# >>>
23#
24# Note that an HTTP object is used for a single request -- to issue a
25# second request to the same server, you create a new HTTP object.
26# (This is in accordance with the protocol, which uses a new TCP
27# connection for each request.)
28
29
30import os
31import socket
32import string
33import regex
34import regsub
35import rfc822
36
37HTTP_VERSION = 'HTTP/1.0'
38HTTP_PORT = 80
39
40replypat = regsub.gsub('\\.', '\\\\.', HTTP_VERSION) + \
41 '[ \t]+\([0-9][0-9][0-9]\)\(.*\)'
42replyprog = regex.compile(replypat)
43
44class HTTP:
45
Guido van Rossum2922c6d1994-05-06 14:28:19 +000046 def __init__(self, host = '', port = 0):
Guido van Rossum23acc951994-02-21 16:36:04 +000047 self.debuglevel = 0
Guido van Rossum2922c6d1994-05-06 14:28:19 +000048 if host: self.connect(host, port)
Guido van Rossum23acc951994-02-21 16:36:04 +000049
50 def set_debuglevel(self, debuglevel):
51 self.debuglevel = debuglevel
52
Guido van Rossum2922c6d1994-05-06 14:28:19 +000053 def connect(self, host, port = 0):
54 if not port:
Guido van Rossum23acc951994-02-21 16:36:04 +000055 i = string.find(host, ':')
56 if i >= 0:
57 host, port = host[:i], host[i+1:]
58 try: port = string.atoi(port)
Guido van Rossum76ca3c11994-02-22 16:06:02 +000059 except string.atoi_error: pass
Guido van Rossum23acc951994-02-21 16:36:04 +000060 if not port: port = HTTP_PORT
61 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
62 if self.debuglevel > 0: print 'connect:', (host, port)
63 self.sock.connect(host, port)
64
65 def send(self, str):
66 if self.debuglevel > 0: print 'send:', `str`
67 self.sock.send(str)
68
69 def putrequest(self, request, selector):
70 str = '%s %s %s\r\n' % (request, selector, HTTP_VERSION)
71 self.send(str)
72
73 def putheader(self, header, *args):
74 str = '%s: %s\r\n' % (header, string.joinfields(args,'\r\n\t'))
75 self.send(str)
76
77 def endheaders(self):
78 self.send('\r\n')
79
80 def endrequest(self):
81 if self.debuglevel > 0: print 'shutdown: 1'
82 self.sock.shutdown(1)
83
84 def getreply(self):
85 self.endrequest()
86 self.file = self.sock.makefile('r')
87 line = self.file.readline()
88 if self.debuglevel > 0: print 'reply:', `line`
89 if replyprog.match(line) < 0:
90 self.headers = None
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 = rfc822.Message(self.file)
96 return errcode, errmsg, self.headers
97
98 def getfile(self):
99 return self.file
100
101
102def test():
103 import sys
104 import getopt
105 opts, args = getopt.getopt(sys.argv[1:], 'd')
106 dl = 0
107 for o, a in opts:
108 if o == '-d': dl = dl + 1
109 host = 'www.cwi.nl:80'
110 selector = '/index.html'
111 if args[0:]: host = args[0]
112 if args[1:]: selector = args[1]
113 h = HTTP()
114 h.set_debuglevel(dl)
115 h.connect(host)
116 h.putrequest('GET', selector)
117 errcode, errmsg, headers = h.getreply()
118 print 'errcode =', errcode
119 print 'headers =', headers
120 print 'errmsg =', errmsg
121 if headers:
122 for header in headers.headers: print string.strip(header)
123 print h.getfile().read()
124
125if __name__ == '__main__':
126 test()