annotate Lib/BaseHTTPServer.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents fb1e71c7619a
children c31d700dea8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """HTTP server base class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Note: the class in this module doesn't implement any HTTP request; see
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 SimpleHTTPServer for simple implementations of GET, HEAD and POST
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
5 (including CGI scripts). It does, however, optionally implement HTTP/1.1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
6 persistent connections, as of version 0.3.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 Contents:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 - BaseHTTPRequestHandler: HTTP request handler base class
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 - test: test function
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 XXX To do:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 - log requests even later (to capture byte count)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 - log user-agent header and other interesting goodies
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 - send error log to separate file
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # See also:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # HTTP Working Group T. Berners-Lee
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # INTERNET-DRAFT R. T. Fielding
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # Expires September 8, 1995 March 8, 1995
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
29 #
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
30 # and
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
31 #
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
32 # Network Working Group R. Fielding
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
33 # Request for Comments: 2616 et al
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
34 # Obsoletes: 2068 June 1999
23075
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22830
diff changeset
35 # Category: Standards Track
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
36 #
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
37 # URL: http://www.faqs.org/rfcs/rfc2616.html
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # Log files
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # ---------
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
41 #
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # Here's a quote from the NCSA httpd docs about log file format.
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
43 #
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
44 # | The logfile format is as follows. Each line consists of:
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
45 # |
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
46 # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
47 # |
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
48 # | host: Either the DNS name or the IP number of the remote client
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # | rfc931: Any information returned by identd for this person,
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
50 # | - otherwise.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # | authuser: If user sent a userid for authentication, the user name,
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
52 # | - otherwise.
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
53 # | DD: Day
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
54 # | Mon: Month (calendar name)
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
55 # | YYYY: Year
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
56 # | hh: hour (24-hour format, the machine's timezone)
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
57 # | mm: minutes
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
58 # | ss: seconds
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
59 # | request: The first line of the HTTP request as sent by the client.
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
60 # | ddd: the status code returned by the server, - if not available.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # | bbbb: the total number of bytes sent,
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
62 # | *not including the HTTP/1.0 header*, - if not available
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
63 # |
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # | You can determine the name of the file accessed through request.
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
65 #
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # (Actually, the latter is only true if you know the server configuration
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # at the time the request was made!)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
69 __version__ = "0.3"
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
71 __all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 import sys
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 import time
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 import socket # For gethostbyaddr()
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48248
diff changeset
76 from warnings import filterwarnings, catch_warnings
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48248
diff changeset
77 with catch_warnings():
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48248
diff changeset
78 if sys.py3kwarning:
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48248
diff changeset
79 filterwarnings("ignore", ".*mimetools has been removed",
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48248
diff changeset
80 DeprecationWarning)
48248
894b4acfffe4 Silence the DeprecationWarning raised by importing mimetools in BaseHTTPServer.
Brett Cannon <bcannon@gmail.com>
parents: 47241
diff changeset
81 import mimetools
47241
ba1fa511b72d socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
82 import SocketServer
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
45449
734fc0a19657 #1492: allow overriding BaseHTTPServer's content type for error messages.
Georg Brandl <georg@python.org>
parents: 40593
diff changeset
84 # Default error message template
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 DEFAULT_ERROR_MESSAGE = """\
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 <head>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 <title>Error response</title>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 </head>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 <body>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 <h1>Error response</h1>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 <p>Error code %(code)d.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 <p>Message: %(message)s.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 <p>Error code explanation: %(code)s = %(explain)s.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 </body>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
45449
734fc0a19657 #1492: allow overriding BaseHTTPServer's content type for error messages.
Georg Brandl <georg@python.org>
parents: 40593
diff changeset
97 DEFAULT_ERROR_CONTENT_TYPE = "text/html"
734fc0a19657 #1492: allow overriding BaseHTTPServer's content type for error messages.
Georg Brandl <georg@python.org>
parents: 40593
diff changeset
98
35151
728a0de88395 backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
99 def _quote_html(html):
728a0de88395 backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
100 return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
47241
ba1fa511b72d socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
102 class HTTPServer(SocketServer.TCPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
12634
ddf986a971b5 Set HTTPServer class variable allow_reuse_address to 1, so restarting
Guido van Rossum <guido@python.org>
parents: 11409
diff changeset
104 allow_reuse_address = 1 # Seems to make sense in testing environment
ddf986a971b5 Set HTTPServer class variable allow_reuse_address to 1, so restarting
Guido van Rossum <guido@python.org>
parents: 11409
diff changeset
105
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 def server_bind(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
107 """Override server_bind to store the server name."""
47241
ba1fa511b72d socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
108 SocketServer.TCPServer.server_bind(self)
29340
c69ad9227afd Patch #744041: Use only first two elements of address to support IPv6.
Martin v. Löwis <martin@v.loewis.de>
parents: 28089
diff changeset
109 host, port = self.socket.getsockname()[:2]
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12634
diff changeset
110 self.server_name = socket.getfqdn(host)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
111 self.server_port = port
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
47241
ba1fa511b72d socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
114 class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 """HTTP request handler base class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 The following explanation of HTTP serves to guide you through the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 code as well as to expose any misunderstandings I may have about
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 HTTP (so you don't need to read the code to figure out I'm wrong
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 :-).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 HTTP (HyperText Transfer Protocol) is an extensible protocol on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 top of a reliable stream transport (e.g. TCP/IP). The protocol
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 recognizes three parts to a request:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 1. One line identifying the request type and path
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 2. An optional set of RFC-822-style headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 3. An optional data part
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 The headers and data are separated by a blank line.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 The first line of the request has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 <command> <path> <version>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 where <command> is a (case-sensitive) keyword such as GET or POST,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 <path> is a string containing path information for the request,
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
139 and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
140 <path> is encoded using the URL encoding scheme (using %xx to signify
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
141 the ASCII character with hex code xx).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
27637
601549c64039 Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 23608
diff changeset
143 The specification specifies that lines are separated by CRLF but
601549c64039 Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 23608
diff changeset
144 for compatibility with the widest range of clients recommends
601549c64039 Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 23608
diff changeset
145 servers also handle LF. Similarly, whitespace in the request line
601549c64039 Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 23608
diff changeset
146 is treated sensibly (allowing multiple spaces between components
601549c64039 Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 23608
diff changeset
147 and allowing trailing whitespace).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 Similarly, for output, lines ought to be separated by CRLF pairs
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 but most clients grok LF characters just fine.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 If the first line of the request has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 <command> <path>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 (i.e. <version> is left out) then this is assumed to be an HTTP
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 0.9 request; this form has no optional headers and data part and
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 the reply consists of just the data.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
160 The reply form of the HTTP 1.x protocol again has three parts:
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 1. One line giving the response code
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 2. An optional set of RFC-822-style headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 3. The data
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 Again, the headers and data are separated by a blank line.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 The response code line has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 <version> <responsecode> <responsestring>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
172 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 <responsecode> is a 3-digit response code indicating success or
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 failure of the request, and <responsestring> is an optional
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 human-readable string explaining what the response code means.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 This server parses the request and the headers, and then calls a
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 function specific to the request type (<command>). Specifically,
11287
f67ce152719a Typo: the method called is do_SPAM, not handle_SPAM.
Guido van Rossum <guido@python.org>
parents: 10939
diff changeset
179 a request SPAM will be handled by a method do_SPAM(). If no
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 such method exists the server sends an error response to the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 client. If it exists, it is called with no arguments:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 do_SPAM()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 Note that the request name is case sensitive (i.e. SPAM and spam
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 are different requests).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 The various request details are stored in instance variables:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 - client_address is the client IP address in the form (host,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 port);
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 - command, path and version are the broken-down request line;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 - headers is an instance of mimetools.Message (or a derived
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 class) containing the header information;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 - rfile is a file object open for reading positioned at the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 start of the optional input data part;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 - wfile is a file object open for writing.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 The first thing to be written must be the response line. Then
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 follow 0 or more header lines, then a blank line, and then the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 actual data (if any). The meaning of the header lines depends on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 the command executed by the server; in most cases, when data is
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 returned, there should be at least one header line of the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 Content-type: <type>/<subtype>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 where <type> and <subtype> should be registered MIME types,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 e.g. "text/html" or "text/plain".
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 # The Python system version, truncated to its first component.
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
219 sys_version = "Python/" + sys.version.split()[0]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 # The server software version. You may want to override this.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 # The format is multiple whitespace-separated strings,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 # where each string is of the form name[/version].
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 server_version = "BaseHTTP/" + __version__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
226 # The default request version. This only affects responses up until
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
227 # the point where the request line is parsed, so it mainly decides what
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
228 # the client gets back when sending a malformed request line.
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
229 # Most web servers default to HTTP 0.9, i.e. don't send a status line.
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
230 default_request_version = "HTTP/0.9"
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
231
11409
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
232 def parse_request(self):
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
233 """Parse a request (internal).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
29357
20894dbc7f6a Fix SF bug #747348 docstring mistake in BaseHTTPServer.py. \n\nBackport Candidate
Raymond Hettinger <python@rcn.com>
parents: 29340
diff changeset
235 The request should be stored in self.raw_requestline; the results
11409
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
236 are in self.command, self.path, self.request_version and
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
237 self.headers.
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
238
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
239 Return True for success, False for failure; on failure, an
11409
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
240 error is sent back.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
242 """
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
243 self.command = None # set in case of error on the first line
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents: 46929
diff changeset
244 self.request_version = version = self.default_request_version
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
245 self.close_connection = 1
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
246 requestline = self.raw_requestline
74140
1b1818fee351 port to 2.7 - Minor code style improvements in http.server suggested in Issue13294.
Senthil Kumaran <senthil@uthcode.com>
parents: 68256
diff changeset
247 requestline = requestline.rstrip('\r\n')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
248 self.requestline = requestline
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
249 words = requestline.split()
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
250 if len(words) == 3:
74140
1b1818fee351 port to 2.7 - Minor code style improvements in http.server suggested in Issue13294.
Senthil Kumaran <senthil@uthcode.com>
parents: 68256
diff changeset
251 command, path, version = words
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
252 if version[:5] != 'HTTP/':
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
253 self.send_error(400, "Bad request version (%r)" % version)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
254 return False
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
255 try:
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
256 base_version_number = version.split('/', 1)[1]
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
257 version_number = base_version_number.split(".")
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
258 # RFC 2145 section 3.1 says there can be only one "." and
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
259 # - major and minor numbers MUST be treated as
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
260 # separate integers;
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
261 # - HTTP/2.4 is a lower version than HTTP/2.13, which in
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
262 # turn is lower than HTTP/12.3;
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
263 # - Leading zeros MUST be ignored by recipients.
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
264 if len(version_number) != 2:
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
265 raise ValueError
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
266 version_number = int(version_number[0]), int(version_number[1])
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
267 except (ValueError, IndexError):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
268 self.send_error(400, "Bad request version (%r)" % version)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
269 return False
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
270 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
271 self.close_connection = 0
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
272 if version_number >= (2, 0):
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
273 self.send_error(505,
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
274 "Invalid HTTP Version (%s)" % base_version_number)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
275 return False
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
276 elif len(words) == 2:
74140
1b1818fee351 port to 2.7 - Minor code style improvements in http.server suggested in Issue13294.
Senthil Kumaran <senthil@uthcode.com>
parents: 68256
diff changeset
277 command, path = words
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
278 self.close_connection = 1
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
279 if command != 'GET':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
280 self.send_error(400,
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
281 "Bad HTTP/0.9 request type (%r)" % command)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
282 return False
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
283 elif not words:
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
284 return False
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
285 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
286 self.send_error(400, "Bad request syntax (%r)" % requestline)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
287 return False
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
288 self.command, self.path, self.request_version = command, path, version
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
289
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
290 # Examine the headers and look for a Connection directive
30105
4353f04422e3 SF patch #747364: BaseHTTPServer doesn't need StringIO intermediary
Raymond Hettinger <python@rcn.com>
parents: 29357
diff changeset
291 self.headers = self.MessageClass(self.rfile, 0)
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
292
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
293 conntype = self.headers.get('Connection', "")
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
294 if conntype.lower() == 'close':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
295 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
296 elif (conntype.lower() == 'keep-alive' and
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
297 self.protocol_version >= "HTTP/1.1"):
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
298 self.close_connection = 0
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
299 return True
11409
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
300
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
301 def handle_one_request(self):
11409
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
302 """Handle a single HTTP request.
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
303
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
304 You normally don't need to override this method; see the class
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
305 __doc__ string for information on how to handle specific HTTP
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
306 commands such as GET and POST.
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
307
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
308 """
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
309 try:
66973
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
310 self.raw_requestline = self.rfile.readline(65537)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
311 if len(self.raw_requestline) > 65536:
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
312 self.requestline = ''
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
313 self.request_version = ''
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
314 self.command = ''
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
315 self.send_error(414)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58993
diff changeset
316 return
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
317 if not self.raw_requestline:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
318 self.close_connection = 1
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
319 return
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
320 if not self.parse_request():
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
321 # An error code has been sent, just exit
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
322 return
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
323 mname = 'do_' + self.command
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
324 if not hasattr(self, mname):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
325 self.send_error(501, "Unsupported method (%r)" % self.command)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
326 return
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
327 method = getattr(self, mname)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
328 method()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
329 self.wfile.flush() #actually send the response if not already done.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
330 except socket.timeout, e:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
331 #a read or a write timed out. Discard this connection
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 48948
diff changeset
332 self.log_error("Request timed out: %r", e)
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
333 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
334 return
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
336 def handle(self):
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
337 """Handle multiple requests if necessary."""
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
338 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
339
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
340 self.handle_one_request()
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
341 while not self.close_connection:
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
342 self.handle_one_request()
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
343
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 def send_error(self, code, message=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
345 """Send and log an error reply.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
347 Arguments are the error code, and a detailed message.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
348 The detailed message defaults to the short entry matching the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
349 response code.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
351 This sends an error response (so it must be called before any
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
352 output has been generated), logs the error, and finally sends
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
353 a piece of HTML explaining the error to the user.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
355 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
357 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
358 short, long = self.responses[code]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
359 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
360 short, long = '???', '???'
23608
ec27ac19f9e3 Use is None rather than general boolean
Raymond Hettinger <python@rcn.com>
parents: 23075
diff changeset
361 if message is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
362 message = short
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
363 explain = long
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
364 self.log_error("code %d, message %s", code, message)
35151
728a0de88395 backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
365 # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
366 content = (self.error_message_format %
35151
728a0de88395 backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
367 {'code': code, 'message': _quote_html(message), 'explain': explain})
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
368 self.send_response(code, message)
45449
734fc0a19657 #1492: allow overriding BaseHTTPServer's content type for error messages.
Georg Brandl <georg@python.org>
parents: 40593
diff changeset
369 self.send_header("Content-Type", self.error_content_type)
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
370 self.send_header('Connection', 'close')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
371 self.end_headers()
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
372 if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
373 self.wfile.write(content)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 error_message_format = DEFAULT_ERROR_MESSAGE
45449
734fc0a19657 #1492: allow overriding BaseHTTPServer's content type for error messages.
Georg Brandl <georg@python.org>
parents: 40593
diff changeset
376 error_content_type = DEFAULT_ERROR_CONTENT_TYPE
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 def send_response(self, code, message=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
379 """Send the response header and log the response code.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
381 Also send two standard headers with the server software
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
382 version and the current date.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
384 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
385 self.log_request(code)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
386 if message is None:
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
387 if code in self.responses:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
388 message = self.responses[code][0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
389 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
390 message = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
391 if self.request_version != 'HTTP/0.9':
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
392 self.wfile.write("%s %d %s\r\n" %
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
393 (self.protocol_version, code, message))
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
394 # print (self.protocol_version, code, message)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
395 self.send_header('Server', self.version_string())
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
396 self.send_header('Date', self.date_time_string())
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 def send_header(self, keyword, value):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
399 """Send a MIME header."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
400 if self.request_version != 'HTTP/0.9':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
401 self.wfile.write("%s: %s\r\n" % (keyword, value))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
403 if keyword.lower() == 'connection':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
404 if value.lower() == 'close':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
405 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
406 elif value.lower() == 'keep-alive':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
407 self.close_connection = 0
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
408
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 def end_headers(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
410 """Send the blank line ending the MIME headers."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
411 if self.request_version != 'HTTP/0.9':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
412 self.wfile.write("\r\n")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 def log_request(self, code='-', size='-'):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
415 """Log an accepted request.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416
36879
8d41ddc31e61 Typo fix
Andrew M. Kuchling <amk@amk.ca>
parents: 36559
diff changeset
417 This is called by send_response().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
419 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
421 self.log_message('"%s" %s %s',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
422 self.requestline, str(code), str(size))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
40593
5f130271f386 Fix the signature of log_error(). (A subclass that did the right thing
Guido van Rossum <guido@python.org>
parents: 36879
diff changeset
424 def log_error(self, format, *args):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
425 """Log an error.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
427 This is called when a request cannot be fulfilled. By
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
428 default it passes the message on to log_message().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
429
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
430 Arguments are the same as for log_message().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
432 XXX This should go to the separate error log.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
434 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435
40593
5f130271f386 Fix the signature of log_error(). (A subclass that did the right thing
Guido van Rossum <guido@python.org>
parents: 36879
diff changeset
436 self.log_message(format, *args)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 def log_message(self, format, *args):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
439 """Log an arbitrary message.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
441 This is used by all other logging functions. Override
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
442 it if you have specific logging wishes.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
444 The first argument, FORMAT, is a format string for the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
445 message to be logged. If the format string contains
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
446 any % escapes requiring parameters, they should be
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
447 specified as subsequent arguments (it's just like
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
448 printf!).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
76617
fb1e71c7619a Fix issue6085 - Remove the delay caused by fqdn lookup while logging in BaseHTTPRequestHandler
Senthil Kumaran <senthil@uthcode.com>
parents: 74140
diff changeset
450 The client ip address and current date/time are prefixed to every
fb1e71c7619a Fix issue6085 - Remove the delay caused by fqdn lookup while logging in BaseHTTPRequestHandler
Senthil Kumaran <senthil@uthcode.com>
parents: 74140
diff changeset
451 message.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
453 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
455 sys.stderr.write("%s - - [%s] %s\n" %
76617
fb1e71c7619a Fix issue6085 - Remove the delay caused by fqdn lookup while logging in BaseHTTPRequestHandler
Senthil Kumaran <senthil@uthcode.com>
parents: 74140
diff changeset
456 (self.client_address[0],
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
457 self.log_date_time_string(),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
458 format%args))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 def version_string(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
461 """Return the server software version string."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
462 return self.server_version + ' ' + self.sys_version
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
36555
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
464 def date_time_string(self, timestamp=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
465 """Return the current date and time formatted for a message header."""
36555
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
466 if timestamp is None:
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
467 timestamp = time.time()
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
468 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
469 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
470 self.weekdayname[wd],
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
471 day, self.monthname[month], year,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
472 hh, mm, ss)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
473 return s
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 def log_date_time_string(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
476 """Return the current time formatted for logging."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
477 now = time.time()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
478 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
479 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
480 day, self.monthname[month], year, hh, mm, ss)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
481 return s
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 monthname = [None,
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
486 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
487 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 def address_string(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
490 """Return the client address formatted for logging.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
491
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
492 This version looks up the full hostname using gethostbyaddr(),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
493 and tries to find a name that contains at least one dot.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
495 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
496
29340
c69ad9227afd Patch #744041: Use only first two elements of address to support IPv6.
Martin v. Löwis <martin@v.loewis.de>
parents: 28089
diff changeset
497 host, port = self.client_address[:2]
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12634
diff changeset
498 return socket.getfqdn(host)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 # Essentially static class variables
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 # The version of the HTTP protocol we support.
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
503 # Set this to HTTP/1.1 to enable automatic keepalive
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 protocol_version = "HTTP/1.0"
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 # The Message-like class used to parse headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 MessageClass = mimetools.Message
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 # Table mapping response codes to messages; entries have the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 # form {code: (shortmessage, longmessage)}.
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
511 # See RFC 2616.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 responses = {
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
513 100: ('Continue', 'Request received, please continue'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
514 101: ('Switching Protocols',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
515 'Switching to new protocol; obey Upgrade header'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
516
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
517 200: ('OK', 'Request fulfilled, document follows'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
518 201: ('Created', 'Document created, URL follows'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
519 202: ('Accepted',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
520 'Request accepted, processing continues off-line'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
521 203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
522 204: ('No Content', 'Request fulfilled, nothing follows'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
523 205: ('Reset Content', 'Clear input form for further input.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
524 206: ('Partial Content', 'Partial content follows.'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
525
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
526 300: ('Multiple Choices',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
527 'Object has several resources -- see URI list'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
528 301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
529 302: ('Found', 'Object moved temporarily -- see URI list'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
530 303: ('See Other', 'Object moved -- see Method and URL list'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
531 304: ('Not Modified',
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
532 'Document has not changed since given time'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
533 305: ('Use Proxy',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
534 'You must use proxy specified in Location to access this '
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
535 'resource.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
536 307: ('Temporary Redirect',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
537 'Object moved temporarily -- see URI list'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
538
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
539 400: ('Bad Request',
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
540 'Bad request syntax or unsupported method'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
541 401: ('Unauthorized',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
542 'No permission -- see authorization schemes'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
543 402: ('Payment Required',
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
544 'No payment -- see charging schemes'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
545 403: ('Forbidden',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
546 'Request forbidden -- authorization will not help'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
547 404: ('Not Found', 'Nothing matches the given URI'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
548 405: ('Method Not Allowed',
58994
cc0e69c8f0c2 Merged revisions 78299 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 48948
diff changeset
549 'Specified method is invalid for this resource.'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
550 406: ('Not Acceptable', 'URI not available in preferred format.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
551 407: ('Proxy Authentication Required', 'You must authenticate with '
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
552 'this proxy before proceeding.'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
553 408: ('Request Timeout', 'Request timed out; try again later.'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
554 409: ('Conflict', 'Request conflict.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
555 410: ('Gone',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
556 'URI no longer exists and has been permanently removed.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
557 411: ('Length Required', 'Client must specify Content-Length.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
558 412: ('Precondition Failed', 'Precondition in headers is false.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
559 413: ('Request Entity Too Large', 'Entity is too large.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
560 414: ('Request-URI Too Long', 'URI is too long.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
561 415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
562 416: ('Requested Range Not Satisfiable',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
563 'Cannot satisfy request range.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
564 417: ('Expectation Failed',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
565 'Expect condition could not be satisfied.'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
566
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
567 500: ('Internal Server Error', 'Server got itself in trouble'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
568 501: ('Not Implemented',
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
569 'Server does not support this operation'),
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
570 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
571 503: ('Service Unavailable',
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
572 'The server cannot process the request due to a high load'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
573 504: ('Gateway Timeout',
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
574 'The gateway server did not receive a timely response'),
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
575 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
576 }
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 def test(HandlerClass = BaseHTTPRequestHandler,
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
580 ServerClass = HTTPServer, protocol="HTTP/1.0"):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 """Test the HTTP request handler class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
582
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 This runs an HTTP server on port 8000 (or the first command line
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 argument).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
585
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
587
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 if sys.argv[1:]:
16482
1fb7befb59f0 Correction to test main.
Eric S. Raymond <esr@thyrsus.com>
parents: 16479
diff changeset
589 port = int(sys.argv[1])
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
591 port = 8000
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 server_address = ('', port)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
594 HandlerClass.protocol_version = protocol
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 httpd = ServerClass(server_address, HandlerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
596
18825
40900620293e Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
Martin v. Löwis <martin@v.loewis.de>
parents: 16482
diff changeset
597 sa = httpd.socket.getsockname()
40900620293e Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
Martin v. Löwis <martin@v.loewis.de>
parents: 16482
diff changeset
598 print "Serving HTTP on", sa[0], "port", sa[1], "..."
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 httpd.serve_forever()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
601
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 test()