Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 1 | """HTTP/1.1 client library |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 2 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 3 | <intro stuff goes here> |
| 4 | <other stuff, too> |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 5 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 6 | HTTPConnection go through a number of "states", which defines when a client |
| 7 | may legally make another request or fetch the response for a particular |
| 8 | request. This diagram details these state transitions: |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 9 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 10 | (null) |
| 11 | | |
| 12 | | HTTPConnection() |
| 13 | v |
| 14 | Idle |
| 15 | | |
| 16 | | putrequest() |
| 17 | v |
| 18 | Request-started |
| 19 | | |
| 20 | | ( putheader() )* endheaders() |
| 21 | v |
| 22 | Request-sent |
| 23 | | |
| 24 | | response = getresponse() |
| 25 | v |
| 26 | Unread-response [Response-headers-read] |
| 27 | |\____________________ |
Tim Peters | 5ceadc8 | 2001-01-13 19:16:21 +0000 | [diff] [blame] | 28 | | | |
| 29 | | response.read() | putrequest() |
| 30 | v v |
| 31 | Idle Req-started-unread-response |
| 32 | ______/| |
| 33 | / | |
| 34 | response.read() | | ( putheader() )* endheaders() |
| 35 | v v |
| 36 | Request-started Req-sent-unread-response |
| 37 | | |
| 38 | | response.read() |
| 39 | v |
| 40 | Request-sent |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 41 | |
| 42 | This diagram presents the following rules: |
| 43 | -- a second request may not be started until {response-headers-read} |
| 44 | -- a response [object] cannot be retrieved until {request-sent} |
| 45 | -- there is no differentiation between an unread response body and a |
| 46 | partially read response body |
| 47 | |
| 48 | Note: this enforcement is applied by the HTTPConnection class. The |
| 49 | HTTPResponse class does not enforce this state machine, which |
| 50 | implies sophisticated clients may accelerate the request/response |
| 51 | pipeline. Caution should be taken, though: accelerating the states |
| 52 | beyond the above pattern may imply knowledge of the server's |
| 53 | connection-close behavior for certain requests. For example, it |
| 54 | is impossible to tell whether the server will close the connection |
| 55 | UNTIL the response headers have been read; this means that further |
| 56 | requests cannot be placed into the pipeline until it is known that |
| 57 | the server will NOT be closing the connection. |
| 58 | |
| 59 | Logical State __state __response |
| 60 | ------------- ------- ---------- |
| 61 | Idle _CS_IDLE None |
| 62 | Request-started _CS_REQ_STARTED None |
| 63 | Request-sent _CS_REQ_SENT None |
| 64 | Unread-response _CS_IDLE <response_class> |
| 65 | Req-started-unread-response _CS_REQ_STARTED <response_class> |
| 66 | Req-sent-unread-response _CS_REQ_SENT <response_class> |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 67 | """ |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 68 | |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 69 | import errno |
Guido van Rossum | 65ab98c | 1995-08-07 20:13:02 +0000 | [diff] [blame] | 70 | import mimetools |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 71 | import socket |
Jeremy Hylton | 8acf1e0 | 2002-03-08 19:35:51 +0000 | [diff] [blame] | 72 | from urlparse import urlsplit |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 74 | try: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 75 | from cStringIO import StringIO |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 76 | except ImportError: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 77 | from StringIO import StringIO |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 78 | |
Skip Montanaro | 951a884 | 2001-06-01 16:25:38 +0000 | [diff] [blame] | 79 | __all__ = ["HTTP", "HTTPResponse", "HTTPConnection", "HTTPSConnection", |
| 80 | "HTTPException", "NotConnected", "UnknownProtocol", |
Jeremy Hylton | 7c75c99 | 2002-06-28 23:38:14 +0000 | [diff] [blame^] | 81 | "UnknownTransferEncoding", "UnimplementedFileMode", |
| 82 | "IncompleteRead", "InvalidURL", "ImproperConnectionState", |
| 83 | "CannotSendRequest", "CannotSendHeader", "ResponseNotReady", |
| 84 | "BadStatusLine", "error"] |
Skip Montanaro | 2dd4276 | 2001-01-23 15:35:05 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 86 | HTTP_PORT = 80 |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 87 | HTTPS_PORT = 443 |
| 88 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 89 | _UNKNOWN = 'UNKNOWN' |
| 90 | |
| 91 | # connection states |
| 92 | _CS_IDLE = 'Idle' |
| 93 | _CS_REQ_STARTED = 'Request-started' |
| 94 | _CS_REQ_SENT = 'Request-sent' |
| 95 | |
| 96 | |
| 97 | class HTTPResponse: |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 98 | def __init__(self, sock, debuglevel=0): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 99 | self.fp = sock.makefile('rb', 0) |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 100 | self.debuglevel = debuglevel |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 101 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 102 | self.msg = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 103 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 104 | # from the Status-Line of the response |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 105 | self.version = _UNKNOWN # HTTP-Version |
| 106 | self.status = _UNKNOWN # Status-Code |
| 107 | self.reason = _UNKNOWN # Reason-Phrase |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 108 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 109 | self.chunked = _UNKNOWN # is "chunked" being used? |
| 110 | self.chunk_left = _UNKNOWN # bytes left to read in current chunk |
| 111 | self.length = _UNKNOWN # number of bytes left in response |
| 112 | self.will_close = _UNKNOWN # conn will close at end of response |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 113 | |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 114 | def _read_status(self): |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 115 | line = self.fp.readline() |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 116 | if self.debuglevel > 0: |
| 117 | print "reply:", repr(line) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 118 | try: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 119 | [version, status, reason] = line.split(None, 2) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 120 | except ValueError: |
| 121 | try: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 122 | [version, status] = line.split(None, 1) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 123 | reason = "" |
| 124 | except ValueError: |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 125 | version = "HTTP/0.9" |
| 126 | status = "200" |
| 127 | reason = "" |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 128 | if version[:5] != 'HTTP/': |
| 129 | self.close() |
| 130 | raise BadStatusLine(line) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 131 | |
Jeremy Hylton | 23d4047 | 2001-04-13 14:57:08 +0000 | [diff] [blame] | 132 | # The status code is a three-digit number |
| 133 | try: |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 134 | status = int(status) |
Jeremy Hylton | 23d4047 | 2001-04-13 14:57:08 +0000 | [diff] [blame] | 135 | if status < 100 or status > 999: |
| 136 | raise BadStatusLine(line) |
| 137 | except ValueError: |
| 138 | raise BadStatusLine(line) |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 139 | return version, status, reason |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 140 | |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 141 | def _begin(self): |
| 142 | if self.msg is not None: |
| 143 | # we've already started reading the response |
| 144 | return |
| 145 | |
| 146 | # read until we get a non-100 response |
| 147 | while 1: |
| 148 | version, status, reason = self._read_status() |
| 149 | if status != 100: |
| 150 | break |
| 151 | # skip the header from the 100 response |
| 152 | while 1: |
| 153 | skip = self.fp.readline().strip() |
| 154 | if not skip: |
| 155 | break |
| 156 | if self.debuglevel > 0: |
| 157 | print "header:", skip |
| 158 | |
| 159 | self.status = status |
| 160 | self.reason = reason.strip() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 161 | if version == 'HTTP/1.0': |
| 162 | self.version = 10 |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 163 | elif version.startswith('HTTP/1.'): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 164 | self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1 |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 165 | elif version == 'HTTP/0.9': |
| 166 | self.version = 9 |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 167 | else: |
| 168 | raise UnknownProtocol(version) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 169 | |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 170 | if self.version == 9: |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 171 | self.chunked = 0 |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 172 | self.msg = mimetools.Message(StringIO()) |
| 173 | return |
| 174 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 175 | self.msg = mimetools.Message(self.fp, 0) |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 176 | if self.debuglevel > 0: |
| 177 | for hdr in self.msg.headers: |
| 178 | print "header:", hdr, |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 179 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 180 | # don't let the msg keep an fp |
| 181 | self.msg.fp = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 182 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 183 | # are we using the chunked-style of transfer encoding? |
| 184 | tr_enc = self.msg.getheader('transfer-encoding') |
| 185 | if tr_enc: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 186 | if tr_enc.lower() != 'chunked': |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 187 | raise UnknownTransferEncoding() |
| 188 | self.chunked = 1 |
| 189 | self.chunk_left = None |
| 190 | else: |
| 191 | self.chunked = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 192 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 193 | # will the connection close at the end of the response? |
| 194 | conn = self.msg.getheader('connection') |
| 195 | if conn: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 196 | conn = conn.lower() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 197 | # a "Connection: close" will always close the connection. if we |
| 198 | # don't see that and this is not HTTP/1.1, then the connection will |
| 199 | # close unless we see a Keep-Alive header. |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 200 | self.will_close = conn.find('close') != -1 or \ |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 201 | ( self.version != 11 and \ |
| 202 | not self.msg.getheader('keep-alive') ) |
| 203 | else: |
| 204 | # for HTTP/1.1, the connection will always remain open |
| 205 | # otherwise, it will remain open IFF we see a Keep-Alive header |
| 206 | self.will_close = self.version != 11 and \ |
| 207 | not self.msg.getheader('keep-alive') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 208 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 209 | # do we have a Content-Length? |
| 210 | # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked" |
| 211 | length = self.msg.getheader('content-length') |
| 212 | if length and not self.chunked: |
Jeremy Hylton | 30a8181 | 2000-09-14 20:34:27 +0000 | [diff] [blame] | 213 | try: |
| 214 | self.length = int(length) |
| 215 | except ValueError: |
| 216 | self.length = None |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 217 | else: |
| 218 | self.length = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 219 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 220 | # does the body have a fixed length? (of zero) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 221 | if (status == 204 or # No Content |
| 222 | status == 304 or # Not Modified |
| 223 | 100 <= status < 200): # 1xx codes |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 224 | self.length = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 225 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 226 | # if the connection remains open, and we aren't using chunked, and |
| 227 | # a content-length was not provided, then assume that the connection |
| 228 | # WILL close. |
| 229 | if not self.will_close and \ |
| 230 | not self.chunked and \ |
| 231 | self.length is None: |
| 232 | self.will_close = 1 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 233 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 234 | def close(self): |
| 235 | if self.fp: |
| 236 | self.fp.close() |
| 237 | self.fp = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 238 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 239 | def isclosed(self): |
| 240 | # NOTE: it is possible that we will not ever call self.close(). This |
| 241 | # case occurs when will_close is TRUE, length is None, and we |
| 242 | # read up to the last byte, but NOT past it. |
| 243 | # |
| 244 | # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be |
| 245 | # called, meaning self.isclosed() is meaningful. |
| 246 | return self.fp is None |
| 247 | |
| 248 | def read(self, amt=None): |
| 249 | if self.fp is None: |
| 250 | return '' |
| 251 | |
| 252 | if self.chunked: |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 253 | assert self.chunked != _UNKNOWN |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 254 | chunk_left = self.chunk_left |
| 255 | value = '' |
| 256 | while 1: |
| 257 | if chunk_left is None: |
| 258 | line = self.fp.readline() |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 259 | i = line.find(';') |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 260 | if i >= 0: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 261 | line = line[:i] # strip chunk-extensions |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 262 | chunk_left = int(line, 16) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 263 | if chunk_left == 0: |
| 264 | break |
| 265 | if amt is None: |
| 266 | value = value + self._safe_read(chunk_left) |
| 267 | elif amt < chunk_left: |
| 268 | value = value + self._safe_read(amt) |
| 269 | self.chunk_left = chunk_left - amt |
| 270 | return value |
| 271 | elif amt == chunk_left: |
| 272 | value = value + self._safe_read(amt) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 273 | self._safe_read(2) # toss the CRLF at the end of the chunk |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 274 | self.chunk_left = None |
| 275 | return value |
| 276 | else: |
| 277 | value = value + self._safe_read(chunk_left) |
| 278 | amt = amt - chunk_left |
| 279 | |
| 280 | # we read the whole chunk, get another |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 281 | self._safe_read(2) # toss the CRLF at the end of the chunk |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 282 | chunk_left = None |
| 283 | |
| 284 | # read and discard trailer up to the CRLF terminator |
| 285 | ### note: we shouldn't have any trailers! |
| 286 | while 1: |
| 287 | line = self.fp.readline() |
| 288 | if line == '\r\n': |
| 289 | break |
| 290 | |
| 291 | # we read everything; close the "file" |
| 292 | self.close() |
| 293 | |
| 294 | return value |
| 295 | |
| 296 | elif amt is None: |
| 297 | # unbounded read |
| 298 | if self.will_close: |
| 299 | s = self.fp.read() |
| 300 | else: |
| 301 | s = self._safe_read(self.length) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 302 | self.close() # we read everything |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 303 | return s |
| 304 | |
| 305 | if self.length is not None: |
| 306 | if amt > self.length: |
| 307 | # clip the read to the "end of response" |
| 308 | amt = self.length |
| 309 | self.length = self.length - amt |
| 310 | |
| 311 | # we do not use _safe_read() here because this may be a .will_close |
| 312 | # connection, and the user is reading more bytes than will be provided |
| 313 | # (for example, reading in 1k chunks) |
| 314 | s = self.fp.read(amt) |
| 315 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 316 | return s |
| 317 | |
| 318 | def _safe_read(self, amt): |
| 319 | """Read the number of bytes requested, compensating for partial reads. |
| 320 | |
| 321 | Normally, we have a blocking socket, but a read() can be interrupted |
| 322 | by a signal (resulting in a partial read). |
| 323 | |
| 324 | Note that we cannot distinguish between EOF and an interrupt when zero |
| 325 | bytes have been read. IncompleteRead() will be raised in this |
| 326 | situation. |
| 327 | |
| 328 | This function should be used when <amt> bytes "should" be present for |
| 329 | reading. If the bytes are truly not available (due to EOF), then the |
| 330 | IncompleteRead exception can be used to detect the problem. |
| 331 | """ |
| 332 | s = '' |
| 333 | while amt > 0: |
| 334 | chunk = self.fp.read(amt) |
| 335 | if not chunk: |
| 336 | raise IncompleteRead(s) |
| 337 | s = s + chunk |
| 338 | amt = amt - len(chunk) |
| 339 | return s |
| 340 | |
| 341 | def getheader(self, name, default=None): |
| 342 | if self.msg is None: |
| 343 | raise ResponseNotReady() |
| 344 | return self.msg.getheader(name, default) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | class HTTPConnection: |
| 348 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 349 | _http_vsn = 11 |
| 350 | _http_vsn_str = 'HTTP/1.1' |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 351 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 352 | response_class = HTTPResponse |
| 353 | default_port = HTTP_PORT |
| 354 | auto_open = 1 |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 355 | debuglevel = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 356 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 357 | def __init__(self, host, port=None): |
| 358 | self.sock = None |
| 359 | self.__response = None |
| 360 | self.__state = _CS_IDLE |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 361 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 362 | self._set_hostport(host, port) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 363 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 364 | def _set_hostport(self, host, port): |
| 365 | if port is None: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 366 | i = host.find(':') |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 367 | if i >= 0: |
Skip Montanaro | 9d38997 | 2002-03-24 16:53:50 +0000 | [diff] [blame] | 368 | try: |
| 369 | port = int(host[i+1:]) |
| 370 | except ValueError: |
| 371 | raise InvalidURL, "nonnumeric port: '%s'"%host[i+1:] |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 372 | host = host[:i] |
| 373 | else: |
| 374 | port = self.default_port |
| 375 | self.host = host |
| 376 | self.port = port |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 377 | |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 378 | def set_debuglevel(self, level): |
| 379 | self.debuglevel = level |
| 380 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 381 | def connect(self): |
| 382 | """Connect to the host and port specified in __init__.""" |
Martin v. Löwis | 2ad2569 | 2001-07-31 08:40:21 +0000 | [diff] [blame] | 383 | msg = "getaddrinfo returns an empty list" |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 384 | for res in socket.getaddrinfo(self.host, self.port, 0, |
| 385 | socket.SOCK_STREAM): |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 386 | af, socktype, proto, canonname, sa = res |
| 387 | try: |
| 388 | self.sock = socket.socket(af, socktype, proto) |
| 389 | if self.debuglevel > 0: |
| 390 | print "connect: (%s, %s)" % (self.host, self.port) |
| 391 | self.sock.connect(sa) |
| 392 | except socket.error, msg: |
| 393 | if self.debuglevel > 0: |
| 394 | print 'connect fail:', (self.host, self.port) |
Martin v. Löwis | 322c0d1 | 2001-10-07 08:53:32 +0000 | [diff] [blame] | 395 | if self.sock: |
| 396 | self.sock.close() |
Martin v. Löwis | 4eb5940 | 2001-07-26 13:37:33 +0000 | [diff] [blame] | 397 | self.sock = None |
| 398 | continue |
| 399 | break |
| 400 | if not self.sock: |
| 401 | raise socket.error, msg |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 402 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 403 | def close(self): |
| 404 | """Close the connection to the HTTP server.""" |
| 405 | if self.sock: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 406 | self.sock.close() # close it manually... there may be other refs |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 407 | self.sock = None |
| 408 | if self.__response: |
| 409 | self.__response.close() |
| 410 | self.__response = None |
| 411 | self.__state = _CS_IDLE |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 412 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 413 | def send(self, str): |
| 414 | """Send `str' to the server.""" |
| 415 | if self.sock is None: |
| 416 | if self.auto_open: |
| 417 | self.connect() |
| 418 | else: |
| 419 | raise NotConnected() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 420 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 421 | # send the data to the server. if we get a broken pipe, then close |
| 422 | # the socket. we want to reconnect when somebody tries to send again. |
| 423 | # |
| 424 | # NOTE: we DO propagate the error, though, because we cannot simply |
| 425 | # ignore the error... the caller will know if they can retry. |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 426 | if self.debuglevel > 0: |
| 427 | print "send:", repr(str) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 428 | try: |
Martin v. Löwis | e12454f | 2002-02-16 23:06:19 +0000 | [diff] [blame] | 429 | self.sock.sendall(str) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 430 | except socket.error, v: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 431 | if v[0] == 32: # Broken pipe |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 432 | self.close() |
| 433 | raise |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 434 | |
Jeremy Hylton | 3921ff6 | 2002-03-09 06:07:23 +0000 | [diff] [blame] | 435 | def putrequest(self, method, url, skip_host=0): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 436 | """Send a request to the server. |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 437 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 438 | `method' specifies an HTTP request method, e.g. 'GET'. |
| 439 | `url' specifies the object being requested, e.g. '/index.html'. |
| 440 | """ |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 441 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 442 | # check if a prior response has been completed |
| 443 | if self.__response and self.__response.isclosed(): |
| 444 | self.__response = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 445 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 446 | # |
| 447 | # in certain cases, we cannot issue another request on this connection. |
| 448 | # this occurs when: |
| 449 | # 1) we are in the process of sending a request. (_CS_REQ_STARTED) |
| 450 | # 2) a response to a previous request has signalled that it is going |
| 451 | # to close the connection upon completion. |
| 452 | # 3) the headers for the previous response have not been read, thus |
| 453 | # we cannot determine whether point (2) is true. (_CS_REQ_SENT) |
| 454 | # |
| 455 | # if there is no prior response, then we can request at will. |
| 456 | # |
| 457 | # if point (2) is true, then we will have passed the socket to the |
| 458 | # response (effectively meaning, "there is no prior response"), and |
| 459 | # will open a new one when a new request is made. |
| 460 | # |
| 461 | # Note: if a prior response exists, then we *can* start a new request. |
| 462 | # We are not allowed to begin fetching the response to this new |
| 463 | # request, however, until that prior response is complete. |
| 464 | # |
| 465 | if self.__state == _CS_IDLE: |
| 466 | self.__state = _CS_REQ_STARTED |
| 467 | else: |
| 468 | raise CannotSendRequest() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 469 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 470 | if not url: |
| 471 | url = '/' |
| 472 | str = '%s %s %s\r\n' % (method, url, self._http_vsn_str) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 473 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 474 | try: |
| 475 | self.send(str) |
| 476 | except socket.error, v: |
| 477 | # trap 'Broken pipe' if we're allowed to automatically reconnect |
| 478 | if v[0] != 32 or not self.auto_open: |
| 479 | raise |
| 480 | # try one more time (the socket was closed; this will reopen) |
| 481 | self.send(str) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 482 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 483 | if self._http_vsn == 11: |
| 484 | # Issue some standard headers for better HTTP/1.1 compliance |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 485 | |
Jeremy Hylton | 3921ff6 | 2002-03-09 06:07:23 +0000 | [diff] [blame] | 486 | if not skip_host: |
| 487 | # this header is issued *only* for HTTP/1.1 |
| 488 | # connections. more specifically, this means it is |
| 489 | # only issued when the client uses the new |
| 490 | # HTTPConnection() class. backwards-compat clients |
| 491 | # will be using HTTP/1.0 and those clients may be |
| 492 | # issuing this header themselves. we should NOT issue |
| 493 | # it twice; some web servers (such as Apache) barf |
| 494 | # when they see two Host: headers |
Guido van Rossum | f6922aa | 2001-01-14 21:03:01 +0000 | [diff] [blame] | 495 | |
Jeremy Hylton | 3921ff6 | 2002-03-09 06:07:23 +0000 | [diff] [blame] | 496 | # If we need a non-standard port,include it in the |
| 497 | # header. If the request is going through a proxy, |
| 498 | # but the host of the actual URL, not the host of the |
| 499 | # proxy. |
Jeremy Hylton | 8acf1e0 | 2002-03-08 19:35:51 +0000 | [diff] [blame] | 500 | |
Jeremy Hylton | 3921ff6 | 2002-03-09 06:07:23 +0000 | [diff] [blame] | 501 | netloc = '' |
| 502 | if url.startswith('http'): |
| 503 | nil, netloc, nil, nil, nil = urlsplit(url) |
| 504 | |
| 505 | if netloc: |
| 506 | self.putheader('Host', netloc) |
| 507 | elif self.port == HTTP_PORT: |
| 508 | self.putheader('Host', self.host) |
| 509 | else: |
| 510 | self.putheader('Host', "%s:%s" % (self.host, self.port)) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 511 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 512 | # note: we are assuming that clients will not attempt to set these |
| 513 | # headers since *this* library must deal with the |
| 514 | # consequences. this also means that when the supporting |
| 515 | # libraries are updated to recognize other forms, then this |
| 516 | # code should be changed (removed or updated). |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 517 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 518 | # we only want a Content-Encoding of "identity" since we don't |
| 519 | # support encodings such as x-gzip or x-deflate. |
| 520 | self.putheader('Accept-Encoding', 'identity') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 521 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 522 | # we can accept "chunked" Transfer-Encodings, but no others |
| 523 | # NOTE: no TE header implies *only* "chunked" |
| 524 | #self.putheader('TE', 'chunked') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 525 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 526 | # if TE is supplied in the header, then it must appear in a |
| 527 | # Connection header. |
| 528 | #self.putheader('Connection', 'TE') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 529 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 530 | else: |
| 531 | # For HTTP/1.0, the server will assume "not chunked" |
| 532 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 533 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 534 | def putheader(self, header, value): |
| 535 | """Send a request header line to the server. |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 536 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 537 | For example: h.putheader('Accept', 'text/html') |
| 538 | """ |
| 539 | if self.__state != _CS_REQ_STARTED: |
| 540 | raise CannotSendHeader() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 541 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 542 | str = '%s: %s\r\n' % (header, value) |
| 543 | self.send(str) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 544 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 545 | def endheaders(self): |
| 546 | """Indicate that the last header line has been sent to the server.""" |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 547 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 548 | if self.__state == _CS_REQ_STARTED: |
| 549 | self.__state = _CS_REQ_SENT |
| 550 | else: |
| 551 | raise CannotSendHeader() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 552 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 553 | self.send('\r\n') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 554 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 555 | def request(self, method, url, body=None, headers={}): |
| 556 | """Send a complete request to the server.""" |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 557 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 558 | try: |
| 559 | self._send_request(method, url, body, headers) |
| 560 | except socket.error, v: |
| 561 | # trap 'Broken pipe' if we're allowed to automatically reconnect |
| 562 | if v[0] != 32 or not self.auto_open: |
| 563 | raise |
| 564 | # try one more time |
| 565 | self._send_request(method, url, body, headers) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 566 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 567 | def _send_request(self, method, url, body, headers): |
Jeremy Hylton | 3921ff6 | 2002-03-09 06:07:23 +0000 | [diff] [blame] | 568 | # If headers already contains a host header, then define the |
| 569 | # optional skip_host argument to putrequest(). The check is |
| 570 | # harder because field names are case insensitive. |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 571 | if 'Host' in (headers |
Jeremy Hylton | 3921ff6 | 2002-03-09 06:07:23 +0000 | [diff] [blame] | 572 | or [k for k in headers.iterkeys() if k.lower() == "host"]): |
| 573 | self.putrequest(method, url, skip_host=1) |
| 574 | else: |
| 575 | self.putrequest(method, url) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 576 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 577 | if body: |
| 578 | self.putheader('Content-Length', str(len(body))) |
| 579 | for hdr, value in headers.items(): |
| 580 | self.putheader(hdr, value) |
| 581 | self.endheaders() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 582 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 583 | if body: |
| 584 | self.send(body) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 585 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 586 | def getresponse(self): |
| 587 | "Get the response from the server." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 588 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 589 | # check if a prior response has been completed |
| 590 | if self.__response and self.__response.isclosed(): |
| 591 | self.__response = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 592 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 593 | # |
| 594 | # if a prior response exists, then it must be completed (otherwise, we |
| 595 | # cannot read this response's header to determine the connection-close |
| 596 | # behavior) |
| 597 | # |
| 598 | # note: if a prior response existed, but was connection-close, then the |
| 599 | # socket and response were made independent of this HTTPConnection |
| 600 | # object since a new request requires that we open a whole new |
| 601 | # connection |
| 602 | # |
| 603 | # this means the prior response had one of two states: |
| 604 | # 1) will_close: this connection was reset and the prior socket and |
| 605 | # response operate independently |
| 606 | # 2) persistent: the response was retained and we await its |
| 607 | # isclosed() status to become true. |
| 608 | # |
| 609 | if self.__state != _CS_REQ_SENT or self.__response: |
| 610 | raise ResponseNotReady() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 611 | |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 612 | if self.debuglevel > 0: |
| 613 | response = self.response_class(self.sock, self.debuglevel) |
| 614 | else: |
| 615 | response = self.response_class(self.sock) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 616 | |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 617 | response._begin() |
| 618 | assert response.will_close != _UNKNOWN |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 619 | self.__state = _CS_IDLE |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 620 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 621 | if response.will_close: |
| 622 | # this effectively passes the connection to the response |
| 623 | self.close() |
| 624 | else: |
| 625 | # remember this, so we can tell when it is complete |
| 626 | self.__response = response |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 627 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 628 | return response |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 629 | |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 630 | class SSLFile: |
| 631 | """File-like object wrapping an SSL socket.""" |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 632 | |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 633 | BUFSIZE = 8192 |
| 634 | |
| 635 | def __init__(self, sock, ssl, bufsize=None): |
| 636 | self._sock = sock |
| 637 | self._ssl = ssl |
| 638 | self._buf = '' |
| 639 | self._bufsize = bufsize or self.__class__.BUFSIZE |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 640 | |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 641 | def _read(self): |
| 642 | buf = '' |
| 643 | # put in a loop so that we retry on transient errors |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 644 | while 1: |
| 645 | try: |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 646 | buf = self._ssl.read(self._bufsize) |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 647 | except socket.sslerror, err: |
| 648 | if (err[0] == socket.SSL_ERROR_WANT_READ |
Neal Norwitz | 22c5d77 | 2002-02-11 17:59:51 +0000 | [diff] [blame] | 649 | or err[0] == socket.SSL_ERROR_WANT_WRITE): |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 650 | continue |
Martin v. Löwis | 6af3e2d | 2002-04-20 07:47:40 +0000 | [diff] [blame] | 651 | if (err[0] == socket.SSL_ERROR_ZERO_RETURN |
| 652 | or err[0] == socket.SSL_ERROR_EOF): |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 653 | break |
| 654 | raise |
| 655 | except socket.error, err: |
Tim Peters | f3623f3 | 2001-10-11 18:15:51 +0000 | [diff] [blame] | 656 | if err[0] == errno.EINTR: |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 657 | continue |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 658 | if err[0] == errno.EBADF: |
| 659 | # XXX socket was closed? |
| 660 | break |
Jeremy Hylton | 6459c8d | 2001-10-11 17:47:22 +0000 | [diff] [blame] | 661 | raise |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 662 | else: |
Jeremy Hylton | 42dd01a | 2001-02-01 23:35:20 +0000 | [diff] [blame] | 663 | break |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 664 | return buf |
| 665 | |
| 666 | def read(self, size=None): |
| 667 | L = [self._buf] |
| 668 | avail = len(self._buf) |
| 669 | while size is None or avail < size: |
| 670 | s = self._read() |
| 671 | if s == '': |
| 672 | break |
| 673 | L.append(s) |
| 674 | avail += len(s) |
| 675 | all = "".join(L) |
| 676 | if size is None: |
| 677 | self._buf = '' |
| 678 | return all |
| 679 | else: |
| 680 | self._buf = all[size:] |
| 681 | return all[:size] |
| 682 | |
| 683 | def readline(self): |
| 684 | L = [self._buf] |
| 685 | self._buf = '' |
| 686 | while 1: |
| 687 | i = L[-1].find("\n") |
| 688 | if i >= 0: |
| 689 | break |
| 690 | s = self._read() |
| 691 | if s == '': |
| 692 | break |
| 693 | L.append(s) |
| 694 | if i == -1: |
| 695 | # loop exited because there is no more data |
| 696 | return "".join(L) |
| 697 | else: |
| 698 | all = "".join(L) |
| 699 | # XXX could do enough bookkeeping not to do a 2nd search |
| 700 | i = all.find("\n") + 1 |
| 701 | line = all[:i] |
| 702 | self._buf = all[i:] |
| 703 | return line |
| 704 | |
| 705 | def close(self): |
| 706 | self._sock.close() |
| 707 | |
| 708 | class FakeSocket: |
| 709 | def __init__(self, sock, ssl): |
| 710 | self.__sock = sock |
| 711 | self.__ssl = ssl |
| 712 | |
| 713 | def makefile(self, mode, bufsize=None): |
| 714 | if mode != 'r' and mode != 'rb': |
| 715 | raise UnimplementedFileMode() |
| 716 | return SSLFile(self.__sock, self.__ssl, bufsize) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 717 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 718 | def send(self, stuff, flags = 0): |
| 719 | return self.__ssl.write(stuff) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 720 | |
Andrew M. Kuchling | a3c0b93 | 2002-03-18 22:51:48 +0000 | [diff] [blame] | 721 | def sendall(self, stuff, flags = 0): |
| 722 | return self.__ssl.write(stuff) |
| 723 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 724 | def recv(self, len = 1024, flags = 0): |
| 725 | return self.__ssl.read(len) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 726 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 727 | def __getattr__(self, attr): |
| 728 | return getattr(self.__sock, attr) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 729 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 730 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 731 | class HTTPSConnection(HTTPConnection): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 732 | "This class allows communication via SSL." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 733 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 734 | default_port = HTTPS_PORT |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 735 | |
Jeremy Hylton | 7c75c99 | 2002-06-28 23:38:14 +0000 | [diff] [blame^] | 736 | def __init__(self, host, port=None, key_file=None, cert_file=None): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 737 | HTTPConnection.__init__(self, host, port) |
Jeremy Hylton | 7c75c99 | 2002-06-28 23:38:14 +0000 | [diff] [blame^] | 738 | self.key_file = key_file |
| 739 | self.cert_file = cert_file |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 740 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 741 | def connect(self): |
| 742 | "Connect to a host on a given (SSL) port." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 743 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 744 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 745 | sock.connect((self.host, self.port)) |
Guido van Rossum | 0aee722 | 2000-12-11 20:32:20 +0000 | [diff] [blame] | 746 | realsock = sock |
| 747 | if hasattr(sock, "_sock"): |
| 748 | realsock = sock._sock |
| 749 | ssl = socket.ssl(realsock, self.key_file, self.cert_file) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 750 | self.sock = FakeSocket(sock, ssl) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 751 | |
| 752 | |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 753 | class HTTP: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 754 | "Compatibility class with httplib.py from 1.5." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 755 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 756 | _http_vsn = 10 |
| 757 | _http_vsn_str = 'HTTP/1.0' |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 758 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 759 | debuglevel = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 760 | |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 761 | _connection_class = HTTPConnection |
| 762 | |
Greg Stein | 81937a4 | 2001-08-18 09:20:23 +0000 | [diff] [blame] | 763 | def __init__(self, host='', port=None): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 764 | "Provide a default host, since the superclass requires one." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 765 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 766 | # some joker passed 0 explicitly, meaning default port |
| 767 | if port == 0: |
| 768 | port = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 769 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 770 | # Note that we may pass an empty string as the host; this will throw |
| 771 | # an error when we attempt to connect. Presumably, the client code |
| 772 | # will call connect before then, with a proper host. |
Greg Stein | 81937a4 | 2001-08-18 09:20:23 +0000 | [diff] [blame] | 773 | self._setup(self._connection_class(host, port)) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 774 | |
Greg Stein | 81937a4 | 2001-08-18 09:20:23 +0000 | [diff] [blame] | 775 | def _setup(self, conn): |
| 776 | self._conn = conn |
| 777 | |
| 778 | # set up delegation to flesh out interface |
| 779 | self.send = conn.send |
| 780 | self.putrequest = conn.putrequest |
| 781 | self.endheaders = conn.endheaders |
| 782 | self.set_debuglevel = conn.set_debuglevel |
| 783 | |
| 784 | conn._http_vsn = self._http_vsn |
| 785 | conn._http_vsn_str = self._http_vsn_str |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 786 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 787 | self.file = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 788 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 789 | def connect(self, host=None, port=None): |
| 790 | "Accept arguments to set the host/port, since the superclass doesn't." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 791 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 792 | if host is not None: |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 793 | self._conn._set_hostport(host, port) |
| 794 | self._conn.connect() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 795 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 796 | def getfile(self): |
| 797 | "Provide a getfile, since the superclass' does not use this concept." |
| 798 | return self.file |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 799 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 800 | def putheader(self, header, *values): |
| 801 | "The superclass allows only one value argument." |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 802 | self._conn.putheader(header, '\r\n\t'.join(values)) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 803 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 804 | def getreply(self): |
| 805 | """Compat definition since superclass does not define it. |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 806 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 807 | Returns a tuple consisting of: |
| 808 | - server status code (e.g. '200' if all goes well) |
| 809 | - server "reason" corresponding to status code |
| 810 | - any RFC822 headers in the response from the server |
| 811 | """ |
| 812 | try: |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 813 | response = self._conn.getresponse() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 814 | except BadStatusLine, e: |
| 815 | ### hmm. if getresponse() ever closes the socket on a bad request, |
| 816 | ### then we are going to have problems with self.sock |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 817 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 818 | ### should we keep this behavior? do people use it? |
| 819 | # keep the socket open (as a file), and return it |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 820 | self.file = self._conn.sock.makefile('rb', 0) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 821 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 822 | # close our socket -- we want to restart after any protocol error |
| 823 | self.close() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 824 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 825 | self.headers = None |
| 826 | return -1, e.line, None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 827 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 828 | self.headers = response.msg |
| 829 | self.file = response.fp |
| 830 | return response.status, response.reason, response.msg |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 831 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 832 | def close(self): |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 833 | self._conn.close() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 834 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 835 | # note that self.file == response.fp, which gets closed by the |
| 836 | # superclass. just clear the object ref here. |
| 837 | ### hmm. messy. if status==-1, then self.file is owned by us. |
| 838 | ### well... we aren't explicitly closing, but losing this ref will |
| 839 | ### do it |
| 840 | self.file = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 841 | |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 842 | if hasattr(socket, 'ssl'): |
| 843 | class HTTPS(HTTP): |
| 844 | """Compatibility with 1.5 httplib interface |
| 845 | |
| 846 | Python 1.5.2 did not have an HTTPS class, but it defined an |
| 847 | interface for sending http requests that is also useful for |
Tim Peters | 5ceadc8 | 2001-01-13 19:16:21 +0000 | [diff] [blame] | 848 | https. |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 849 | """ |
| 850 | |
Martin v. Löwis | d7bf974 | 2000-09-21 22:09:47 +0000 | [diff] [blame] | 851 | _connection_class = HTTPSConnection |
Tim Peters | 5ceadc8 | 2001-01-13 19:16:21 +0000 | [diff] [blame] | 852 | |
Greg Stein | 81937a4 | 2001-08-18 09:20:23 +0000 | [diff] [blame] | 853 | def __init__(self, host='', port=None, **x509): |
| 854 | # provide a default host, pass the X509 cert info |
| 855 | |
| 856 | # urf. compensate for bad input. |
| 857 | if port == 0: |
| 858 | port = None |
| 859 | self._setup(self._connection_class(host, port, **x509)) |
| 860 | |
| 861 | # we never actually use these for anything, but we keep them |
| 862 | # here for compatibility with post-1.5.2 CVS. |
| 863 | self.key_file = x509.get('key_file') |
| 864 | self.cert_file = x509.get('cert_file') |
| 865 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 866 | |
| 867 | class HTTPException(Exception): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 868 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 869 | |
| 870 | class NotConnected(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 871 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 872 | |
Skip Montanaro | 9d38997 | 2002-03-24 16:53:50 +0000 | [diff] [blame] | 873 | class InvalidURL(HTTPException): |
| 874 | pass |
| 875 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 876 | class UnknownProtocol(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 877 | def __init__(self, version): |
| 878 | self.version = version |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 879 | |
| 880 | class UnknownTransferEncoding(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 881 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 882 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 883 | class UnimplementedFileMode(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 884 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 885 | |
| 886 | class IncompleteRead(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 887 | def __init__(self, partial): |
| 888 | self.partial = partial |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 889 | |
| 890 | class ImproperConnectionState(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 891 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 892 | |
| 893 | class CannotSendRequest(ImproperConnectionState): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 894 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 895 | |
| 896 | class CannotSendHeader(ImproperConnectionState): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 897 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 898 | |
| 899 | class ResponseNotReady(ImproperConnectionState): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 900 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 901 | |
| 902 | class BadStatusLine(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 903 | def __init__(self, line): |
| 904 | self.line = line |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 905 | |
| 906 | # for backwards compatibility |
| 907 | error = HTTPException |
| 908 | |
| 909 | |
| 910 | # |
| 911 | # snarfed from httplib.py for now... |
| 912 | # |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 913 | def test(): |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 914 | """Test this module. |
| 915 | |
| 916 | The test consists of retrieving and displaying the Python |
| 917 | home page, along with the error code and error string returned |
| 918 | by the www.python.org server. |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 919 | """ |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 921 | import sys |
| 922 | import getopt |
| 923 | opts, args = getopt.getopt(sys.argv[1:], 'd') |
| 924 | dl = 0 |
| 925 | for o, a in opts: |
| 926 | if o == '-d': dl = dl + 1 |
| 927 | host = 'www.python.org' |
| 928 | selector = '/' |
| 929 | if args[0:]: host = args[0] |
| 930 | if args[1:]: selector = args[1] |
| 931 | h = HTTP() |
| 932 | h.set_debuglevel(dl) |
| 933 | h.connect(host) |
| 934 | h.putrequest('GET', selector) |
| 935 | h.endheaders() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 936 | status, reason, headers = h.getreply() |
| 937 | print 'status =', status |
| 938 | print 'reason =', reason |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 939 | print |
| 940 | if headers: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 941 | for header in headers.headers: print header.strip() |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 942 | print |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 943 | print "read", len(h.getfile().read()) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 944 | |
Jeremy Hylton | 8acf1e0 | 2002-03-08 19:35:51 +0000 | [diff] [blame] | 945 | # minimal test that code to extract host from url works |
| 946 | class HTTP11(HTTP): |
| 947 | _http_vsn = 11 |
| 948 | _http_vsn_str = 'HTTP/1.1' |
| 949 | |
| 950 | h = HTTP11('www.python.org') |
| 951 | h.putrequest('GET', 'http://www.python.org/~jeremy/') |
| 952 | h.endheaders() |
| 953 | h.getreply() |
| 954 | h.close() |
| 955 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 956 | if hasattr(socket, 'ssl'): |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 957 | host = 'sourceforge.net' |
Jeremy Hylton | 42dd01a | 2001-02-01 23:35:20 +0000 | [diff] [blame] | 958 | selector = '/projects/python' |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 959 | hs = HTTPS() |
| 960 | hs.connect(host) |
| 961 | hs.putrequest('GET', selector) |
| 962 | hs.endheaders() |
| 963 | status, reason, headers = hs.getreply() |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 964 | # XXX why does this give a 302 response? |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 965 | print 'status =', status |
| 966 | print 'reason =', reason |
| 967 | print |
| 968 | if headers: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame] | 969 | for header in headers.headers: print header.strip() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 970 | print |
Jeremy Hylton | be4fcf1 | 2002-06-28 22:38:01 +0000 | [diff] [blame] | 971 | print "read", len(hs.getfile().read()) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 972 | |
Guido van Rossum | a0dfc7a | 1995-09-07 19:28:19 +0000 | [diff] [blame] | 973 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 974 | if __name__ == '__main__': |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 975 | test() |