2000-06-26 05:28:01 -03:00
|
|
|
"""HTTP/1.1 client library
|
|
|
|
|
|
|
|
<intro stuff goes here>
|
|
|
|
<other stuff, too>
|
|
|
|
|
|
|
|
HTTPConnection go through a number of "states", which defines when a client
|
|
|
|
may legally make another request or fetch the response for a particular
|
|
|
|
request. This diagram details these state transitions:
|
|
|
|
|
|
|
|
(null)
|
|
|
|
|
|
|
|
|
| HTTPConnection()
|
|
|
|
v
|
|
|
|
Idle
|
|
|
|
|
|
|
|
|
| putrequest()
|
|
|
|
v
|
|
|
|
Request-started
|
|
|
|
|
|
|
|
|
| ( putheader() )* endheaders()
|
|
|
|
v
|
|
|
|
Request-sent
|
|
|
|
|
|
|
|
|
| response = getresponse()
|
|
|
|
v
|
|
|
|
Unread-response [Response-headers-read]
|
|
|
|
|\____________________
|
2001-01-13 15:16:21 -04:00
|
|
|
| |
|
|
|
|
| response.read() | putrequest()
|
|
|
|
v v
|
|
|
|
Idle Req-started-unread-response
|
|
|
|
______/|
|
|
|
|
/ |
|
|
|
|
response.read() | | ( putheader() )* endheaders()
|
|
|
|
v v
|
|
|
|
Request-started Req-sent-unread-response
|
|
|
|
|
|
|
|
|
| response.read()
|
|
|
|
v
|
|
|
|
Request-sent
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
This diagram presents the following rules:
|
|
|
|
-- a second request may not be started until {response-headers-read}
|
|
|
|
-- a response [object] cannot be retrieved until {request-sent}
|
|
|
|
-- there is no differentiation between an unread response body and a
|
|
|
|
partially read response body
|
|
|
|
|
|
|
|
Note: this enforcement is applied by the HTTPConnection class. The
|
|
|
|
HTTPResponse class does not enforce this state machine, which
|
|
|
|
implies sophisticated clients may accelerate the request/response
|
|
|
|
pipeline. Caution should be taken, though: accelerating the states
|
|
|
|
beyond the above pattern may imply knowledge of the server's
|
|
|
|
connection-close behavior for certain requests. For example, it
|
|
|
|
is impossible to tell whether the server will close the connection
|
|
|
|
UNTIL the response headers have been read; this means that further
|
|
|
|
requests cannot be placed into the pipeline until it is known that
|
|
|
|
the server will NOT be closing the connection.
|
|
|
|
|
|
|
|
Logical State __state __response
|
|
|
|
------------- ------- ----------
|
|
|
|
Idle _CS_IDLE None
|
|
|
|
Request-started _CS_REQ_STARTED None
|
|
|
|
Request-sent _CS_REQ_SENT None
|
|
|
|
Unread-response _CS_IDLE <response_class>
|
|
|
|
Req-started-unread-response _CS_REQ_STARTED <response_class>
|
|
|
|
Req-sent-unread-response _CS_REQ_SENT <response_class>
|
1997-12-08 20:12:23 -04:00
|
|
|
"""
|
1994-02-21 12:36:04 -04:00
|
|
|
|
2001-10-11 14:47:22 -03:00
|
|
|
import errno
|
1995-08-07 17:13:02 -03:00
|
|
|
import mimetools
|
2001-10-11 14:47:22 -03:00
|
|
|
import socket
|
2002-03-08 15:35:51 -04:00
|
|
|
from urlparse import urlsplit
|
1994-02-21 12:36:04 -04:00
|
|
|
|
1999-12-07 17:37:17 -04:00
|
|
|
try:
|
2000-07-18 06:09:48 -03:00
|
|
|
from cStringIO import StringIO
|
2000-06-26 05:28:01 -03:00
|
|
|
except ImportError:
|
2000-07-18 06:09:48 -03:00
|
|
|
from StringIO import StringIO
|
1999-12-07 17:37:17 -04:00
|
|
|
|
2001-06-01 13:25:38 -03:00
|
|
|
__all__ = ["HTTP", "HTTPResponse", "HTTPConnection", "HTTPSConnection",
|
|
|
|
"HTTPException", "NotConnected", "UnknownProtocol",
|
|
|
|
"UnknownTransferEncoding", "IllegalKeywordArgument",
|
2002-03-24 12:56:45 -04:00
|
|
|
"UnimplementedFileMode", "IncompleteRead", "InvalidURL",
|
2001-06-01 13:25:38 -03:00
|
|
|
"ImproperConnectionState", "CannotSendRequest", "CannotSendHeader",
|
|
|
|
"ResponseNotReady", "BadStatusLine", "error"]
|
2001-01-23 11:35:05 -04:00
|
|
|
|
1994-02-21 12:36:04 -04:00
|
|
|
HTTP_PORT = 80
|
1999-12-07 17:37:17 -04:00
|
|
|
HTTPS_PORT = 443
|
|
|
|
|
2000-06-26 05:28:01 -03:00
|
|
|
_UNKNOWN = 'UNKNOWN'
|
|
|
|
|
|
|
|
# connection states
|
|
|
|
_CS_IDLE = 'Idle'
|
|
|
|
_CS_REQ_STARTED = 'Request-started'
|
|
|
|
_CS_REQ_SENT = 'Request-sent'
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPResponse:
|
2000-09-18 19:50:38 -03:00
|
|
|
def __init__(self, sock, debuglevel=0):
|
2000-07-18 06:09:48 -03:00
|
|
|
self.fp = sock.makefile('rb', 0)
|
2000-09-18 19:50:38 -03:00
|
|
|
self.debuglevel = debuglevel
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
self.msg = None
|
|
|
|
|
|
|
|
# from the Status-Line of the response
|
2001-01-14 19:47:14 -04:00
|
|
|
self.version = _UNKNOWN # HTTP-Version
|
|
|
|
self.status = _UNKNOWN # Status-Code
|
|
|
|
self.reason = _UNKNOWN # Reason-Phrase
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2001-01-14 19:47:14 -04:00
|
|
|
self.chunked = _UNKNOWN # is "chunked" being used?
|
|
|
|
self.chunk_left = _UNKNOWN # bytes left to read in current chunk
|
|
|
|
self.length = _UNKNOWN # number of bytes left in response
|
|
|
|
self.will_close = _UNKNOWN # conn will close at end of response
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
def begin(self):
|
|
|
|
if self.msg is not None:
|
|
|
|
# we've already started reading the response
|
|
|
|
return
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
line = self.fp.readline()
|
2000-09-18 19:50:38 -03:00
|
|
|
if self.debuglevel > 0:
|
|
|
|
print "reply:", repr(line)
|
2000-07-18 06:09:48 -03:00
|
|
|
try:
|
2000-12-15 11:09:42 -04:00
|
|
|
[version, status, reason] = line.split(None, 2)
|
2000-07-18 06:09:48 -03:00
|
|
|
except ValueError:
|
|
|
|
try:
|
2000-12-15 11:09:42 -04:00
|
|
|
[version, status] = line.split(None, 1)
|
2000-07-18 06:09:48 -03:00
|
|
|
reason = ""
|
|
|
|
except ValueError:
|
2000-10-12 16:58:36 -03:00
|
|
|
version = "HTTP/0.9"
|
|
|
|
status = "200"
|
|
|
|
reason = ""
|
2000-07-18 06:09:48 -03:00
|
|
|
if version[:5] != 'HTTP/':
|
|
|
|
self.close()
|
|
|
|
raise BadStatusLine(line)
|
|
|
|
|
2001-04-13 11:57:08 -03:00
|
|
|
# The status code is a three-digit number
|
|
|
|
try:
|
|
|
|
self.status = status = int(status)
|
|
|
|
if status < 100 or status > 999:
|
|
|
|
raise BadStatusLine(line)
|
|
|
|
except ValueError:
|
|
|
|
raise BadStatusLine(line)
|
2000-12-15 11:09:42 -04:00
|
|
|
self.reason = reason.strip()
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
if version == 'HTTP/1.0':
|
|
|
|
self.version = 10
|
2000-10-12 16:58:36 -03:00
|
|
|
elif version.startswith('HTTP/1.'):
|
2001-01-14 19:47:14 -04:00
|
|
|
self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
|
2000-10-12 16:58:36 -03:00
|
|
|
elif version == 'HTTP/0.9':
|
|
|
|
self.version = 9
|
2000-07-18 06:09:48 -03:00
|
|
|
else:
|
|
|
|
raise UnknownProtocol(version)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-10-12 16:58:36 -03:00
|
|
|
if self.version == 9:
|
|
|
|
self.msg = mimetools.Message(StringIO())
|
|
|
|
return
|
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
self.msg = mimetools.Message(self.fp, 0)
|
2000-09-18 19:50:38 -03:00
|
|
|
if self.debuglevel > 0:
|
|
|
|
for hdr in self.msg.headers:
|
|
|
|
print "header:", hdr,
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
# don't let the msg keep an fp
|
|
|
|
self.msg.fp = None
|
|
|
|
|
|
|
|
# are we using the chunked-style of transfer encoding?
|
|
|
|
tr_enc = self.msg.getheader('transfer-encoding')
|
|
|
|
if tr_enc:
|
2000-12-15 11:09:42 -04:00
|
|
|
if tr_enc.lower() != 'chunked':
|
2000-07-18 06:09:48 -03:00
|
|
|
raise UnknownTransferEncoding()
|
|
|
|
self.chunked = 1
|
|
|
|
self.chunk_left = None
|
|
|
|
else:
|
|
|
|
self.chunked = 0
|
|
|
|
|
|
|
|
# will the connection close at the end of the response?
|
|
|
|
conn = self.msg.getheader('connection')
|
|
|
|
if conn:
|
2000-12-15 11:09:42 -04:00
|
|
|
conn = conn.lower()
|
2000-07-18 06:09:48 -03:00
|
|
|
# a "Connection: close" will always close the connection. if we
|
|
|
|
# don't see that and this is not HTTP/1.1, then the connection will
|
|
|
|
# close unless we see a Keep-Alive header.
|
2000-12-15 11:09:42 -04:00
|
|
|
self.will_close = conn.find('close') != -1 or \
|
2000-07-18 06:09:48 -03:00
|
|
|
( self.version != 11 and \
|
|
|
|
not self.msg.getheader('keep-alive') )
|
|
|
|
else:
|
|
|
|
# for HTTP/1.1, the connection will always remain open
|
|
|
|
# otherwise, it will remain open IFF we see a Keep-Alive header
|
|
|
|
self.will_close = self.version != 11 and \
|
|
|
|
not self.msg.getheader('keep-alive')
|
|
|
|
|
|
|
|
# do we have a Content-Length?
|
|
|
|
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
|
|
|
|
length = self.msg.getheader('content-length')
|
|
|
|
if length and not self.chunked:
|
2000-09-14 17:34:27 -03:00
|
|
|
try:
|
|
|
|
self.length = int(length)
|
|
|
|
except ValueError:
|
|
|
|
self.length = None
|
2000-07-18 06:09:48 -03:00
|
|
|
else:
|
|
|
|
self.length = None
|
|
|
|
|
|
|
|
# does the body have a fixed length? (of zero)
|
2001-01-14 19:47:14 -04:00
|
|
|
if (status == 204 or # No Content
|
|
|
|
status == 304 or # Not Modified
|
|
|
|
100 <= status < 200): # 1xx codes
|
2000-07-18 06:09:48 -03:00
|
|
|
self.length = 0
|
|
|
|
|
|
|
|
# if the connection remains open, and we aren't using chunked, and
|
|
|
|
# a content-length was not provided, then assume that the connection
|
|
|
|
# WILL close.
|
|
|
|
if not self.will_close and \
|
|
|
|
not self.chunked and \
|
|
|
|
self.length is None:
|
|
|
|
self.will_close = 1
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.fp:
|
|
|
|
self.fp.close()
|
|
|
|
self.fp = None
|
|
|
|
|
|
|
|
def isclosed(self):
|
|
|
|
# NOTE: it is possible that we will not ever call self.close(). This
|
|
|
|
# case occurs when will_close is TRUE, length is None, and we
|
|
|
|
# read up to the last byte, but NOT past it.
|
|
|
|
#
|
|
|
|
# IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
|
|
|
|
# called, meaning self.isclosed() is meaningful.
|
|
|
|
return self.fp is None
|
|
|
|
|
|
|
|
def read(self, amt=None):
|
|
|
|
if self.fp is None:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
if self.chunked:
|
|
|
|
chunk_left = self.chunk_left
|
|
|
|
value = ''
|
|
|
|
while 1:
|
|
|
|
if chunk_left is None:
|
|
|
|
line = self.fp.readline()
|
2000-12-15 11:09:42 -04:00
|
|
|
i = line.find(';')
|
2000-07-18 06:09:48 -03:00
|
|
|
if i >= 0:
|
2001-01-14 19:47:14 -04:00
|
|
|
line = line[:i] # strip chunk-extensions
|
2000-12-15 11:09:42 -04:00
|
|
|
chunk_left = int(line, 16)
|
2000-07-18 06:09:48 -03:00
|
|
|
if chunk_left == 0:
|
|
|
|
break
|
|
|
|
if amt is None:
|
|
|
|
value = value + self._safe_read(chunk_left)
|
|
|
|
elif amt < chunk_left:
|
|
|
|
value = value + self._safe_read(amt)
|
|
|
|
self.chunk_left = chunk_left - amt
|
|
|
|
return value
|
|
|
|
elif amt == chunk_left:
|
|
|
|
value = value + self._safe_read(amt)
|
2001-01-14 19:47:14 -04:00
|
|
|
self._safe_read(2) # toss the CRLF at the end of the chunk
|
2000-07-18 06:09:48 -03:00
|
|
|
self.chunk_left = None
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
value = value + self._safe_read(chunk_left)
|
|
|
|
amt = amt - chunk_left
|
|
|
|
|
|
|
|
# we read the whole chunk, get another
|
2001-01-14 19:47:14 -04:00
|
|
|
self._safe_read(2) # toss the CRLF at the end of the chunk
|
2000-07-18 06:09:48 -03:00
|
|
|
chunk_left = None
|
|
|
|
|
|
|
|
# read and discard trailer up to the CRLF terminator
|
|
|
|
### note: we shouldn't have any trailers!
|
|
|
|
while 1:
|
|
|
|
line = self.fp.readline()
|
|
|
|
if line == '\r\n':
|
|
|
|
break
|
|
|
|
|
|
|
|
# we read everything; close the "file"
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
elif amt is None:
|
|
|
|
# unbounded read
|
|
|
|
if self.will_close:
|
|
|
|
s = self.fp.read()
|
|
|
|
else:
|
|
|
|
s = self._safe_read(self.length)
|
2001-01-14 19:47:14 -04:00
|
|
|
self.close() # we read everything
|
2000-07-18 06:09:48 -03:00
|
|
|
return s
|
|
|
|
|
|
|
|
if self.length is not None:
|
|
|
|
if amt > self.length:
|
|
|
|
# clip the read to the "end of response"
|
|
|
|
amt = self.length
|
|
|
|
self.length = self.length - amt
|
|
|
|
|
|
|
|
# we do not use _safe_read() here because this may be a .will_close
|
|
|
|
# connection, and the user is reading more bytes than will be provided
|
|
|
|
# (for example, reading in 1k chunks)
|
|
|
|
s = self.fp.read(amt)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
def _safe_read(self, amt):
|
|
|
|
"""Read the number of bytes requested, compensating for partial reads.
|
|
|
|
|
|
|
|
Normally, we have a blocking socket, but a read() can be interrupted
|
|
|
|
by a signal (resulting in a partial read).
|
|
|
|
|
|
|
|
Note that we cannot distinguish between EOF and an interrupt when zero
|
|
|
|
bytes have been read. IncompleteRead() will be raised in this
|
|
|
|
situation.
|
|
|
|
|
|
|
|
This function should be used when <amt> bytes "should" be present for
|
|
|
|
reading. If the bytes are truly not available (due to EOF), then the
|
|
|
|
IncompleteRead exception can be used to detect the problem.
|
|
|
|
"""
|
|
|
|
s = ''
|
|
|
|
while amt > 0:
|
|
|
|
chunk = self.fp.read(amt)
|
|
|
|
if not chunk:
|
|
|
|
raise IncompleteRead(s)
|
|
|
|
s = s + chunk
|
|
|
|
amt = amt - len(chunk)
|
|
|
|
return s
|
|
|
|
|
|
|
|
def getheader(self, name, default=None):
|
|
|
|
if self.msg is None:
|
|
|
|
raise ResponseNotReady()
|
|
|
|
return self.msg.getheader(name, default)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPConnection:
|
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
_http_vsn = 11
|
|
|
|
_http_vsn_str = 'HTTP/1.1'
|
|
|
|
|
|
|
|
response_class = HTTPResponse
|
|
|
|
default_port = HTTP_PORT
|
|
|
|
auto_open = 1
|
2000-09-18 19:50:38 -03:00
|
|
|
debuglevel = 0
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
def __init__(self, host, port=None):
|
|
|
|
self.sock = None
|
|
|
|
self.__response = None
|
|
|
|
self.__state = _CS_IDLE
|
|
|
|
|
|
|
|
self._set_hostport(host, port)
|
|
|
|
|
|
|
|
def _set_hostport(self, host, port):
|
|
|
|
if port is None:
|
2000-12-15 11:09:42 -04:00
|
|
|
i = host.find(':')
|
2000-07-18 06:09:48 -03:00
|
|
|
if i >= 0:
|
2002-03-24 12:53:50 -04:00
|
|
|
try:
|
|
|
|
port = int(host[i+1:])
|
|
|
|
except ValueError:
|
|
|
|
raise InvalidURL, "nonnumeric port: '%s'"%host[i+1:]
|
2000-07-18 06:09:48 -03:00
|
|
|
host = host[:i]
|
|
|
|
else:
|
|
|
|
port = self.default_port
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
|
2000-09-18 19:50:38 -03:00
|
|
|
def set_debuglevel(self, level):
|
|
|
|
self.debuglevel = level
|
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def connect(self):
|
|
|
|
"""Connect to the host and port specified in __init__."""
|
2001-07-31 05:40:21 -03:00
|
|
|
msg = "getaddrinfo returns an empty list"
|
2001-07-26 10:37:33 -03:00
|
|
|
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
|
|
|
|
af, socktype, proto, canonname, sa = res
|
|
|
|
try:
|
|
|
|
self.sock = socket.socket(af, socktype, proto)
|
|
|
|
if self.debuglevel > 0:
|
|
|
|
print "connect: (%s, %s)" % (self.host, self.port)
|
|
|
|
self.sock.connect(sa)
|
|
|
|
except socket.error, msg:
|
|
|
|
if self.debuglevel > 0:
|
|
|
|
print 'connect fail:', (self.host, self.port)
|
2001-10-07 05:53:32 -03:00
|
|
|
if self.sock:
|
|
|
|
self.sock.close()
|
2001-07-26 10:37:33 -03:00
|
|
|
self.sock = None
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
if not self.sock:
|
|
|
|
raise socket.error, msg
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Close the connection to the HTTP server."""
|
|
|
|
if self.sock:
|
2001-01-14 19:47:14 -04:00
|
|
|
self.sock.close() # close it manually... there may be other refs
|
2000-07-18 06:09:48 -03:00
|
|
|
self.sock = None
|
|
|
|
if self.__response:
|
|
|
|
self.__response.close()
|
|
|
|
self.__response = None
|
|
|
|
self.__state = _CS_IDLE
|
|
|
|
|
|
|
|
def send(self, str):
|
|
|
|
"""Send `str' to the server."""
|
|
|
|
if self.sock is None:
|
|
|
|
if self.auto_open:
|
|
|
|
self.connect()
|
|
|
|
else:
|
|
|
|
raise NotConnected()
|
|
|
|
|
|
|
|
# send the data to the server. if we get a broken pipe, then close
|
|
|
|
# the socket. we want to reconnect when somebody tries to send again.
|
|
|
|
#
|
|
|
|
# NOTE: we DO propagate the error, though, because we cannot simply
|
|
|
|
# ignore the error... the caller will know if they can retry.
|
2000-09-18 19:50:38 -03:00
|
|
|
if self.debuglevel > 0:
|
|
|
|
print "send:", repr(str)
|
2000-07-18 06:09:48 -03:00
|
|
|
try:
|
2002-02-16 19:06:19 -04:00
|
|
|
self.sock.sendall(str)
|
2000-07-18 06:09:48 -03:00
|
|
|
except socket.error, v:
|
2001-01-14 19:47:14 -04:00
|
|
|
if v[0] == 32: # Broken pipe
|
2000-07-18 06:09:48 -03:00
|
|
|
self.close()
|
|
|
|
raise
|
|
|
|
|
2002-03-09 02:07:23 -04:00
|
|
|
def putrequest(self, method, url, skip_host=0):
|
2000-07-18 06:09:48 -03:00
|
|
|
"""Send a request to the server.
|
|
|
|
|
|
|
|
`method' specifies an HTTP request method, e.g. 'GET'.
|
|
|
|
`url' specifies the object being requested, e.g. '/index.html'.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# check if a prior response has been completed
|
|
|
|
if self.__response and self.__response.isclosed():
|
|
|
|
self.__response = None
|
|
|
|
|
|
|
|
#
|
|
|
|
# in certain cases, we cannot issue another request on this connection.
|
|
|
|
# this occurs when:
|
|
|
|
# 1) we are in the process of sending a request. (_CS_REQ_STARTED)
|
|
|
|
# 2) a response to a previous request has signalled that it is going
|
|
|
|
# to close the connection upon completion.
|
|
|
|
# 3) the headers for the previous response have not been read, thus
|
|
|
|
# we cannot determine whether point (2) is true. (_CS_REQ_SENT)
|
|
|
|
#
|
|
|
|
# if there is no prior response, then we can request at will.
|
|
|
|
#
|
|
|
|
# if point (2) is true, then we will have passed the socket to the
|
|
|
|
# response (effectively meaning, "there is no prior response"), and
|
|
|
|
# will open a new one when a new request is made.
|
|
|
|
#
|
|
|
|
# Note: if a prior response exists, then we *can* start a new request.
|
|
|
|
# We are not allowed to begin fetching the response to this new
|
|
|
|
# request, however, until that prior response is complete.
|
|
|
|
#
|
|
|
|
if self.__state == _CS_IDLE:
|
|
|
|
self.__state = _CS_REQ_STARTED
|
|
|
|
else:
|
|
|
|
raise CannotSendRequest()
|
|
|
|
|
|
|
|
if not url:
|
|
|
|
url = '/'
|
|
|
|
str = '%s %s %s\r\n' % (method, url, self._http_vsn_str)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.send(str)
|
|
|
|
except socket.error, v:
|
|
|
|
# trap 'Broken pipe' if we're allowed to automatically reconnect
|
|
|
|
if v[0] != 32 or not self.auto_open:
|
|
|
|
raise
|
|
|
|
# try one more time (the socket was closed; this will reopen)
|
|
|
|
self.send(str)
|
|
|
|
|
|
|
|
if self._http_vsn == 11:
|
|
|
|
# Issue some standard headers for better HTTP/1.1 compliance
|
|
|
|
|
2002-03-09 02:07:23 -04:00
|
|
|
if not skip_host:
|
|
|
|
# this header is issued *only* for HTTP/1.1
|
|
|
|
# connections. more specifically, this means it is
|
|
|
|
# only issued when the client uses the new
|
|
|
|
# HTTPConnection() class. backwards-compat clients
|
|
|
|
# will be using HTTP/1.0 and those clients may be
|
|
|
|
# issuing this header themselves. we should NOT issue
|
|
|
|
# it twice; some web servers (such as Apache) barf
|
|
|
|
# when they see two Host: headers
|
|
|
|
|
|
|
|
# If we need a non-standard port,include it in the
|
|
|
|
# header. If the request is going through a proxy,
|
|
|
|
# but the host of the actual URL, not the host of the
|
|
|
|
# proxy.
|
|
|
|
|
|
|
|
netloc = ''
|
|
|
|
if url.startswith('http'):
|
|
|
|
nil, netloc, nil, nil, nil = urlsplit(url)
|
|
|
|
|
|
|
|
if netloc:
|
|
|
|
self.putheader('Host', netloc)
|
|
|
|
elif self.port == HTTP_PORT:
|
|
|
|
self.putheader('Host', self.host)
|
|
|
|
else:
|
|
|
|
self.putheader('Host', "%s:%s" % (self.host, self.port))
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
# note: we are assuming that clients will not attempt to set these
|
|
|
|
# headers since *this* library must deal with the
|
|
|
|
# consequences. this also means that when the supporting
|
|
|
|
# libraries are updated to recognize other forms, then this
|
|
|
|
# code should be changed (removed or updated).
|
|
|
|
|
|
|
|
# we only want a Content-Encoding of "identity" since we don't
|
|
|
|
# support encodings such as x-gzip or x-deflate.
|
|
|
|
self.putheader('Accept-Encoding', 'identity')
|
|
|
|
|
|
|
|
# we can accept "chunked" Transfer-Encodings, but no others
|
|
|
|
# NOTE: no TE header implies *only* "chunked"
|
|
|
|
#self.putheader('TE', 'chunked')
|
|
|
|
|
|
|
|
# if TE is supplied in the header, then it must appear in a
|
|
|
|
# Connection header.
|
|
|
|
#self.putheader('Connection', 'TE')
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
else:
|
|
|
|
# For HTTP/1.0, the server will assume "not chunked"
|
|
|
|
pass
|
|
|
|
|
|
|
|
def putheader(self, header, value):
|
|
|
|
"""Send a request header line to the server.
|
|
|
|
|
|
|
|
For example: h.putheader('Accept', 'text/html')
|
|
|
|
"""
|
|
|
|
if self.__state != _CS_REQ_STARTED:
|
|
|
|
raise CannotSendHeader()
|
|
|
|
|
|
|
|
str = '%s: %s\r\n' % (header, value)
|
|
|
|
self.send(str)
|
|
|
|
|
|
|
|
def endheaders(self):
|
|
|
|
"""Indicate that the last header line has been sent to the server."""
|
|
|
|
|
|
|
|
if self.__state == _CS_REQ_STARTED:
|
|
|
|
self.__state = _CS_REQ_SENT
|
|
|
|
else:
|
|
|
|
raise CannotSendHeader()
|
|
|
|
|
|
|
|
self.send('\r\n')
|
|
|
|
|
|
|
|
def request(self, method, url, body=None, headers={}):
|
|
|
|
"""Send a complete request to the server."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._send_request(method, url, body, headers)
|
|
|
|
except socket.error, v:
|
|
|
|
# trap 'Broken pipe' if we're allowed to automatically reconnect
|
|
|
|
if v[0] != 32 or not self.auto_open:
|
|
|
|
raise
|
|
|
|
# try one more time
|
|
|
|
self._send_request(method, url, body, headers)
|
|
|
|
|
|
|
|
def _send_request(self, method, url, body, headers):
|
2002-03-09 02:07:23 -04:00
|
|
|
# If headers already contains a host header, then define the
|
|
|
|
# optional skip_host argument to putrequest(). The check is
|
|
|
|
# harder because field names are case insensitive.
|
2002-06-01 11:18:47 -03:00
|
|
|
if 'Host' in (headers
|
2002-03-09 02:07:23 -04:00
|
|
|
or [k for k in headers.iterkeys() if k.lower() == "host"]):
|
|
|
|
self.putrequest(method, url, skip_host=1)
|
|
|
|
else:
|
|
|
|
self.putrequest(method, url)
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
if body:
|
|
|
|
self.putheader('Content-Length', str(len(body)))
|
|
|
|
for hdr, value in headers.items():
|
|
|
|
self.putheader(hdr, value)
|
|
|
|
self.endheaders()
|
|
|
|
|
|
|
|
if body:
|
|
|
|
self.send(body)
|
|
|
|
|
|
|
|
def getresponse(self):
|
|
|
|
"Get the response from the server."
|
|
|
|
|
|
|
|
# check if a prior response has been completed
|
|
|
|
if self.__response and self.__response.isclosed():
|
|
|
|
self.__response = None
|
|
|
|
|
|
|
|
#
|
|
|
|
# if a prior response exists, then it must be completed (otherwise, we
|
|
|
|
# cannot read this response's header to determine the connection-close
|
|
|
|
# behavior)
|
|
|
|
#
|
|
|
|
# note: if a prior response existed, but was connection-close, then the
|
|
|
|
# socket and response were made independent of this HTTPConnection
|
|
|
|
# object since a new request requires that we open a whole new
|
|
|
|
# connection
|
|
|
|
#
|
|
|
|
# this means the prior response had one of two states:
|
|
|
|
# 1) will_close: this connection was reset and the prior socket and
|
|
|
|
# response operate independently
|
|
|
|
# 2) persistent: the response was retained and we await its
|
|
|
|
# isclosed() status to become true.
|
|
|
|
#
|
|
|
|
if self.__state != _CS_REQ_SENT or self.__response:
|
|
|
|
raise ResponseNotReady()
|
|
|
|
|
2000-09-18 19:50:38 -03:00
|
|
|
if self.debuglevel > 0:
|
|
|
|
response = self.response_class(self.sock, self.debuglevel)
|
|
|
|
else:
|
|
|
|
response = self.response_class(self.sock)
|
2000-07-18 06:09:48 -03:00
|
|
|
|
|
|
|
response.begin()
|
|
|
|
self.__state = _CS_IDLE
|
|
|
|
|
|
|
|
if response.will_close:
|
|
|
|
# this effectively passes the connection to the response
|
|
|
|
self.close()
|
|
|
|
else:
|
|
|
|
# remember this, so we can tell when it is complete
|
|
|
|
self.__response = response
|
|
|
|
|
|
|
|
return response
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
|
1999-12-07 17:37:17 -04:00
|
|
|
class FakeSocket:
|
2000-07-18 06:09:48 -03:00
|
|
|
def __init__(self, sock, ssl):
|
|
|
|
self.__sock = sock
|
|
|
|
self.__ssl = ssl
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-08-23 17:34:17 -03:00
|
|
|
def makefile(self, mode, bufsize=None):
|
|
|
|
"""Return a readable file-like object with data from socket.
|
|
|
|
|
|
|
|
This method offers only partial support for the makefile
|
|
|
|
interface of a real socket. It only supports modes 'r' and
|
|
|
|
'rb' and the bufsize argument is ignored.
|
|
|
|
|
2001-01-13 15:16:21 -04:00
|
|
|
The returned object contains *all* of the file data
|
2000-08-23 17:34:17 -03:00
|
|
|
"""
|
2000-07-18 06:09:48 -03:00
|
|
|
if mode != 'r' and mode != 'rb':
|
|
|
|
raise UnimplementedFileMode()
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2001-02-01 19:35:20 -04:00
|
|
|
msgbuf = []
|
2000-07-18 06:09:48 -03:00
|
|
|
while 1:
|
|
|
|
try:
|
2001-02-01 19:35:20 -04:00
|
|
|
buf = self.__ssl.read()
|
2001-10-11 14:47:22 -03:00
|
|
|
except socket.sslerror, err:
|
|
|
|
if (err[0] == socket.SSL_ERROR_WANT_READ
|
2002-02-11 13:59:51 -04:00
|
|
|
or err[0] == socket.SSL_ERROR_WANT_WRITE):
|
2001-10-11 14:47:22 -03:00
|
|
|
continue
|
2002-04-20 04:47:40 -03:00
|
|
|
if (err[0] == socket.SSL_ERROR_ZERO_RETURN
|
|
|
|
or err[0] == socket.SSL_ERROR_EOF):
|
2001-10-11 14:47:22 -03:00
|
|
|
break
|
|
|
|
raise
|
|
|
|
except socket.error, err:
|
2001-10-11 15:15:51 -03:00
|
|
|
if err[0] == errno.EINTR:
|
2001-10-11 14:47:22 -03:00
|
|
|
continue
|
|
|
|
raise
|
2001-02-01 19:35:20 -04:00
|
|
|
if buf == '':
|
|
|
|
break
|
|
|
|
msgbuf.append(buf)
|
|
|
|
return StringIO("".join(msgbuf))
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def send(self, stuff, flags = 0):
|
|
|
|
return self.__ssl.write(stuff)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2002-03-18 18:51:48 -04:00
|
|
|
def sendall(self, stuff, flags = 0):
|
|
|
|
return self.__ssl.write(stuff)
|
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def recv(self, len = 1024, flags = 0):
|
|
|
|
return self.__ssl.read(len)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.__sock, attr)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPSConnection(HTTPConnection):
|
2000-07-18 06:09:48 -03:00
|
|
|
"This class allows communication via SSL."
|
|
|
|
|
|
|
|
default_port = HTTPS_PORT
|
|
|
|
|
|
|
|
def __init__(self, host, port=None, **x509):
|
|
|
|
keys = x509.keys()
|
|
|
|
try:
|
|
|
|
keys.remove('key_file')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
keys.remove('cert_file')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if keys:
|
|
|
|
raise IllegalKeywordArgument()
|
|
|
|
HTTPConnection.__init__(self, host, port)
|
|
|
|
self.key_file = x509.get('key_file')
|
|
|
|
self.cert_file = x509.get('cert_file')
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
"Connect to a host on a given (SSL) port."
|
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.connect((self.host, self.port))
|
2000-12-11 16:32:20 -04:00
|
|
|
realsock = sock
|
|
|
|
if hasattr(sock, "_sock"):
|
|
|
|
realsock = sock._sock
|
|
|
|
ssl = socket.ssl(realsock, self.key_file, self.cert_file)
|
2000-07-18 06:09:48 -03:00
|
|
|
self.sock = FakeSocket(sock, ssl)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
|
2000-08-01 14:33:32 -03:00
|
|
|
class HTTP:
|
2000-07-18 06:09:48 -03:00
|
|
|
"Compatibility class with httplib.py from 1.5."
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
_http_vsn = 10
|
|
|
|
_http_vsn_str = 'HTTP/1.0'
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
debuglevel = 0
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-08-01 14:33:32 -03:00
|
|
|
_connection_class = HTTPConnection
|
|
|
|
|
2001-08-18 06:20:23 -03:00
|
|
|
def __init__(self, host='', port=None):
|
2000-07-18 06:09:48 -03:00
|
|
|
"Provide a default host, since the superclass requires one."
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
# some joker passed 0 explicitly, meaning default port
|
|
|
|
if port == 0:
|
|
|
|
port = None
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
# Note that we may pass an empty string as the host; this will throw
|
|
|
|
# an error when we attempt to connect. Presumably, the client code
|
|
|
|
# will call connect before then, with a proper host.
|
2001-08-18 06:20:23 -03:00
|
|
|
self._setup(self._connection_class(host, port))
|
|
|
|
|
|
|
|
def _setup(self, conn):
|
|
|
|
self._conn = conn
|
|
|
|
|
2000-08-01 14:33:32 -03:00
|
|
|
# set up delegation to flesh out interface
|
2001-08-18 06:20:23 -03:00
|
|
|
self.send = conn.send
|
|
|
|
self.putrequest = conn.putrequest
|
|
|
|
self.endheaders = conn.endheaders
|
|
|
|
self.set_debuglevel = conn.set_debuglevel
|
|
|
|
|
|
|
|
conn._http_vsn = self._http_vsn
|
|
|
|
conn._http_vsn_str = self._http_vsn_str
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
self.file = None
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def connect(self, host=None, port=None):
|
|
|
|
"Accept arguments to set the host/port, since the superclass doesn't."
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
if host is not None:
|
2000-08-01 14:33:32 -03:00
|
|
|
self._conn._set_hostport(host, port)
|
|
|
|
self._conn.connect()
|
1999-12-07 17:37:17 -04:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def getfile(self):
|
|
|
|
"Provide a getfile, since the superclass' does not use this concept."
|
|
|
|
return self.file
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def putheader(self, header, *values):
|
|
|
|
"The superclass allows only one value argument."
|
2000-12-15 11:09:42 -04:00
|
|
|
self._conn.putheader(header, '\r\n\t'.join(values))
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def getreply(self):
|
|
|
|
"""Compat definition since superclass does not define it.
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
Returns a tuple consisting of:
|
|
|
|
- server status code (e.g. '200' if all goes well)
|
|
|
|
- server "reason" corresponding to status code
|
|
|
|
- any RFC822 headers in the response from the server
|
|
|
|
"""
|
|
|
|
try:
|
2000-08-01 14:33:32 -03:00
|
|
|
response = self._conn.getresponse()
|
2000-07-18 06:09:48 -03:00
|
|
|
except BadStatusLine, e:
|
|
|
|
### hmm. if getresponse() ever closes the socket on a bad request,
|
|
|
|
### then we are going to have problems with self.sock
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
### should we keep this behavior? do people use it?
|
|
|
|
# keep the socket open (as a file), and return it
|
2000-08-01 14:33:32 -03:00
|
|
|
self.file = self._conn.sock.makefile('rb', 0)
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
# close our socket -- we want to restart after any protocol error
|
|
|
|
self.close()
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
self.headers = None
|
|
|
|
return -1, e.line, None
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
self.headers = response.msg
|
|
|
|
self.file = response.fp
|
|
|
|
return response.status, response.reason, response.msg
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
def close(self):
|
2000-08-01 14:33:32 -03:00
|
|
|
self._conn.close()
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-07-18 06:09:48 -03:00
|
|
|
# note that self.file == response.fp, which gets closed by the
|
|
|
|
# superclass. just clear the object ref here.
|
|
|
|
### hmm. messy. if status==-1, then self.file is owned by us.
|
|
|
|
### well... we aren't explicitly closing, but losing this ref will
|
|
|
|
### do it
|
|
|
|
self.file = None
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2000-08-01 14:33:32 -03:00
|
|
|
if hasattr(socket, 'ssl'):
|
|
|
|
class HTTPS(HTTP):
|
|
|
|
"""Compatibility with 1.5 httplib interface
|
|
|
|
|
|
|
|
Python 1.5.2 did not have an HTTPS class, but it defined an
|
|
|
|
interface for sending http requests that is also useful for
|
2001-01-13 15:16:21 -04:00
|
|
|
https.
|
2000-08-01 14:33:32 -03:00
|
|
|
"""
|
|
|
|
|
2000-09-21 19:09:47 -03:00
|
|
|
_connection_class = HTTPSConnection
|
2001-01-13 15:16:21 -04:00
|
|
|
|
2001-08-18 06:20:23 -03:00
|
|
|
def __init__(self, host='', port=None, **x509):
|
|
|
|
# provide a default host, pass the X509 cert info
|
|
|
|
|
|
|
|
# urf. compensate for bad input.
|
|
|
|
if port == 0:
|
|
|
|
port = None
|
|
|
|
self._setup(self._connection_class(host, port, **x509))
|
|
|
|
|
|
|
|
# we never actually use these for anything, but we keep them
|
|
|
|
# here for compatibility with post-1.5.2 CVS.
|
|
|
|
self.key_file = x509.get('key_file')
|
|
|
|
self.cert_file = x509.get('cert_file')
|
|
|
|
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class HTTPException(Exception):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class NotConnected(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2002-03-24 12:53:50 -04:00
|
|
|
class InvalidURL(HTTPException):
|
|
|
|
pass
|
|
|
|
|
2000-06-26 05:28:01 -03:00
|
|
|
class UnknownProtocol(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
def __init__(self, version):
|
|
|
|
self.version = version
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class UnknownTransferEncoding(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class IllegalKeywordArgument(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class UnimplementedFileMode(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class IncompleteRead(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
def __init__(self, partial):
|
|
|
|
self.partial = partial
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class ImproperConnectionState(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class CannotSendRequest(ImproperConnectionState):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class CannotSendHeader(ImproperConnectionState):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class ResponseNotReady(ImproperConnectionState):
|
2000-07-18 06:09:48 -03:00
|
|
|
pass
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
class BadStatusLine(HTTPException):
|
2000-07-18 06:09:48 -03:00
|
|
|
def __init__(self, line):
|
|
|
|
self.line = line
|
2000-06-26 05:28:01 -03:00
|
|
|
|
|
|
|
# for backwards compatibility
|
|
|
|
error = HTTPException
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# snarfed from httplib.py for now...
|
|
|
|
#
|
1994-02-21 12:36:04 -04:00
|
|
|
def test():
|
1997-12-08 20:12:23 -04:00
|
|
|
"""Test this module.
|
|
|
|
|
|
|
|
The test consists of retrieving and displaying the Python
|
|
|
|
home page, along with the error code and error string returned
|
|
|
|
by the www.python.org server.
|
|
|
|
"""
|
2000-06-26 05:28:01 -03:00
|
|
|
|
1997-12-08 20:12:23 -04:00
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'd')
|
|
|
|
dl = 0
|
|
|
|
for o, a in opts:
|
|
|
|
if o == '-d': dl = dl + 1
|
|
|
|
host = 'www.python.org'
|
|
|
|
selector = '/'
|
|
|
|
if args[0:]: host = args[0]
|
|
|
|
if args[1:]: selector = args[1]
|
|
|
|
h = HTTP()
|
|
|
|
h.set_debuglevel(dl)
|
|
|
|
h.connect(host)
|
|
|
|
h.putrequest('GET', selector)
|
|
|
|
h.endheaders()
|
2000-06-26 05:28:01 -03:00
|
|
|
status, reason, headers = h.getreply()
|
|
|
|
print 'status =', status
|
|
|
|
print 'reason =', reason
|
1997-12-08 20:12:23 -04:00
|
|
|
print
|
|
|
|
if headers:
|
2000-12-15 11:09:42 -04:00
|
|
|
for header in headers.headers: print header.strip()
|
1997-12-08 20:12:23 -04:00
|
|
|
print
|
|
|
|
print h.getfile().read()
|
2000-06-26 05:28:01 -03:00
|
|
|
|
2002-03-08 15:35:51 -04:00
|
|
|
# minimal test that code to extract host from url works
|
|
|
|
class HTTP11(HTTP):
|
|
|
|
_http_vsn = 11
|
|
|
|
_http_vsn_str = 'HTTP/1.1'
|
|
|
|
|
|
|
|
h = HTTP11('www.python.org')
|
|
|
|
h.putrequest('GET', 'http://www.python.org/~jeremy/')
|
|
|
|
h.endheaders()
|
|
|
|
h.getreply()
|
|
|
|
h.close()
|
|
|
|
|
2000-06-26 05:28:01 -03:00
|
|
|
if hasattr(socket, 'ssl'):
|
2000-08-01 14:33:32 -03:00
|
|
|
host = 'sourceforge.net'
|
2001-02-01 19:35:20 -04:00
|
|
|
selector = '/projects/python'
|
2000-07-18 06:09:48 -03:00
|
|
|
hs = HTTPS()
|
|
|
|
hs.connect(host)
|
|
|
|
hs.putrequest('GET', selector)
|
|
|
|
hs.endheaders()
|
|
|
|
status, reason, headers = hs.getreply()
|
|
|
|
print 'status =', status
|
|
|
|
print 'reason =', reason
|
|
|
|
print
|
|
|
|
if headers:
|
2000-12-15 11:09:42 -04:00
|
|
|
for header in headers.headers: print header.strip()
|
2000-07-18 06:09:48 -03:00
|
|
|
print
|
|
|
|
print hs.getfile().read()
|
1994-02-21 12:36:04 -04:00
|
|
|
|
1995-09-07 16:28:19 -03:00
|
|
|
|
1994-02-21 12:36:04 -04:00
|
|
|
if __name__ == '__main__':
|
1997-12-08 20:12:23 -04:00
|
|
|
test()
|