blob: ca431ab4cb218ada054d2f33b7cb3a8f50fd0a10 [file] [log] [blame]
Guido van Rossum23acc951994-02-21 16:36:04 +00001# HTTP client class
2#
Guido van Rossum928fced1995-09-30 16:50:46 +00003# 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.)
Guido van Rossum23acc951994-02-21 16:36:04 +00006#
7# Example:
8#
9# >>> from httplib import HTTP
Guido van Rossum928fced1995-09-30 16:50:46 +000010# >>> h = HTTP('www.python.org')
11# >>> h.putrequest('GET', '/index.html')
Guido van Rossum23acc951994-02-21 16:36:04 +000012# >>> h.putheader('Accept', 'text/html')
13# >>> h.putheader('Accept', 'text/plain')
Guido van Rossum4cdcef71995-06-22 18:48:48 +000014# >>> h.endheaders()
Guido van Rossum23acc951994-02-21 16:36:04 +000015# >>> errcode, errmsg, headers = h.getreply()
16# >>> if errcode == 200:
17# ... f = h.getfile()
18# ... print f.read() # Print the raw HTML
19# ...
Guido van Rossum928fced1995-09-30 16:50:46 +000020# <HEAD>
21# <TITLE>Python Language Home Page</TITLE>
Guido van Rossum23acc951994-02-21 16:36:04 +000022# [...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 Rossum928fced1995-09-30 16:50:46 +000061 except string.atoi_error:
62 raise socket.error, "nonnumeric port"
Guido van Rossum23acc951994-02-21 16:36:04 +000063 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)
67
68 def send(self, str):
69 if self.debuglevel > 0: print 'send:', `str`
70 self.sock.send(str)
71
72 def putrequest(self, request, selector):
Guido van Rossum4cdcef71995-06-22 18:48:48 +000073 if not selector: selector = '/'
Guido van Rossum23acc951994-02-21 16:36:04 +000074 str = '%s %s %s\r\n' % (request, selector, HTTP_VERSION)
75 self.send(str)
76
77 def putheader(self, header, *args):
78 str = '%s: %s\r\n' % (header, string.joinfields(args,'\r\n\t'))
79 self.send(str)
80
81 def endheaders(self):
82 self.send('\r\n')
83
Guido van Rossum23acc951994-02-21 16:36:04 +000084 def getreply(self):
Jack Jansen2bb57b81996-02-14 16:06:24 +000085 self.file = self.sock.makefile('rb')
Guido van Rossum4cdcef71995-06-22 18:48:48 +000086 self.sock = None
Guido van Rossum23acc951994-02-21 16:36:04 +000087 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)
Guido van Rossum65ab98c1995-08-07 20:13:02 +000095 self.headers = mimetools.Message(self.file, 0)
Guido van Rossum23acc951994-02-21 16:36:04 +000096 return errcode, errmsg, self.headers
97
98 def getfile(self):
99 return self.file
100
Guido van Rossum65ab98c1995-08-07 20:13:02 +0000101 def close(self):
102 if self.file:
103 self.file.close()
104 self.file = None
105
Guido van Rossum23acc951994-02-21 16:36:04 +0000106
107def test():
108 import sys
109 import getopt
110 opts, args = getopt.getopt(sys.argv[1:], 'd')
111 dl = 0
112 for o, a in opts:
113 if o == '-d': dl = dl + 1
Guido van Rossuma0dfc7a1995-09-07 19:28:19 +0000114 host = 'www.python.org'
115 selector = '/'
Guido van Rossum23acc951994-02-21 16:36:04 +0000116 if args[0:]: host = args[0]
117 if args[1:]: selector = args[1]
118 h = HTTP()
119 h.set_debuglevel(dl)
120 h.connect(host)
121 h.putrequest('GET', selector)
Guido van Rossuma0dfc7a1995-09-07 19:28:19 +0000122 h.endheaders()
Guido van Rossum23acc951994-02-21 16:36:04 +0000123 errcode, errmsg, headers = h.getreply()
124 print 'errcode =', errcode
Guido van Rossum23acc951994-02-21 16:36:04 +0000125 print 'errmsg =', errmsg
Guido van Rossuma0dfc7a1995-09-07 19:28:19 +0000126 print
Guido van Rossum23acc951994-02-21 16:36:04 +0000127 if headers:
128 for header in headers.headers: print string.strip(header)
Guido van Rossuma0dfc7a1995-09-07 19:28:19 +0000129 print
Guido van Rossum23acc951994-02-21 16:36:04 +0000130 print h.getfile().read()
131
Guido van Rossuma0dfc7a1995-09-07 19:28:19 +0000132
Guido van Rossum23acc951994-02-21 16:36:04 +0000133if __name__ == '__main__':
134 test()