Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes
http.client.HTTPSConnection Class Reference
Inheritance diagram for http.client.HTTPSConnection:
Inheritance graph
[legend]
Collaboration diagram for http.client.HTTPSConnection:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def connect
def set_tunnel
def set_debuglevel
def close
def send
def putrequest
def putheader
def endheaders
def request
def getresponse

Public Attributes

 key_file
 cert_file
 sock
 debuglevel
 timeout
 source_address
 host
 port

Static Public Attributes

 default_port = HTTPS_PORT
 response_class = HTTPResponse
int auto_open = 1
int debuglevel = 0

Private Attributes

 _context
 _check_hostname

Detailed Description

Definition at line 1064 of file client.py.


Constructor & Destructor Documentation

def http.client.HTTPSConnection.__init__ (   self,
  host,
  port = None,
  key_file = None,
  cert_file = None,
  strict = _strict_sentinel,
  timeout = socket._GLOBAL_DEFAULT_TIMEOUT,
  source_address = None,
  context = None,
  check_hostname = None 
)

Definition at line 1073 of file client.py.

01073 
01074                      source_address=None, *, context=None, check_hostname=None):
01075             super(HTTPSConnection, self).__init__(host, port, strict, timeout,
01076                                                   source_address)
01077             self.key_file = key_file
01078             self.cert_file = cert_file
01079             if context is None:
01080                 # Some reasonable defaults
01081                 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
01082                 context.options |= ssl.OP_NO_SSLv2
01083             will_verify = context.verify_mode != ssl.CERT_NONE
01084             if check_hostname is None:
01085                 check_hostname = will_verify
01086             elif check_hostname and not will_verify:
01087                 raise ValueError("check_hostname needs a SSL context with "
01088                                  "either CERT_OPTIONAL or CERT_REQUIRED")
01089             if key_file or cert_file:
01090                 context.load_cert_chain(cert_file, key_file)
01091             self._context = context
01092             self._check_hostname = check_hostname

Here is the caller graph for this function:


Member Function Documentation

def http.client.HTTPConnection.close (   self) [inherited]
Close the connection to the HTTP server.

Definition at line 725 of file client.py.

00725 
00726     def close(self):
00727         """Close the connection to the HTTP server."""
00728         if self.sock:
00729             self.sock.close()   # close it manually... there may be other refs
00730             self.sock = None
00731         if self.__response:
00732             self.__response.close()
00733             self.__response = None
00734         self.__state = _CS_IDLE

Here is the caller graph for this function:

Connect to the host and port specified in __init__.

Reimplemented from http.client.HTTPConnection.

Definition at line 1093 of file client.py.

01093 
01094         def connect(self):
01095             "Connect to a host on a given (SSL) port."
01096 
01097             sock = socket.create_connection((self.host, self.port),
01098                                             self.timeout, self.source_address)
01099 
01100             if self._tunnel_host:
01101                 self.sock = sock
01102                 self._tunnel()
01103 
01104             server_hostname = self.host if ssl.HAS_SNI else None
01105             self.sock = self._context.wrap_socket(sock,
01106                                                   server_hostname=server_hostname)
01107             try:
01108                 if self._check_hostname:
01109                     ssl.match_hostname(self.sock.getpeercert(), self.host)
01110             except Exception:
01111                 self.sock.shutdown(socket.SHUT_RDWR)
01112                 self.sock.close()
01113                 raise
01114 
    __all__.append("HTTPSConnection")

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.endheaders (   self,
  message_body = None 
) [inherited]
Indicate that the last header line has been sent to the server.

This method sends the request to the server.  The optional
message_body argument can be used to pass message body
associated with the request.  The message body will be sent in
the same packet as the message headers if possible.  The
message_body should be a string.

Definition at line 947 of file client.py.

00947 
00948     def endheaders(self, message_body=None):
00949         """Indicate that the last header line has been sent to the server.
00950 
00951         This method sends the request to the server.  The optional
00952         message_body argument can be used to pass message body
00953         associated with the request.  The message body will be sent in
00954         the same packet as the message headers if possible.  The
00955         message_body should be a string.
00956         """
00957         if self.__state == _CS_REQ_STARTED:
00958             self.__state = _CS_REQ_SENT
00959         else:
00960             raise CannotSendHeader()
00961         self._send_output(message_body)

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.getresponse (   self) [inherited]
Get the response from the server.

If the HTTPConnection is in the correct state, returns an
instance of HTTPResponse or of whatever object is returned by
class the response_class variable.

If a request has not been sent or if a previous response has
not be handled, ResponseNotReady is raised.  If the HTTP
response indicates that the connection should be closed, then
it will be closed before the response is returned.  When the
connection is closed, the underlying socket is closed.

Definition at line 1004 of file client.py.

01004 
01005     def getresponse(self):
01006         """Get the response from the server.
01007 
01008         If the HTTPConnection is in the correct state, returns an
01009         instance of HTTPResponse or of whatever object is returned by
01010         class the response_class variable.
01011 
01012         If a request has not been sent or if a previous response has
01013         not be handled, ResponseNotReady is raised.  If the HTTP
01014         response indicates that the connection should be closed, then
01015         it will be closed before the response is returned.  When the
01016         connection is closed, the underlying socket is closed.
01017         """
01018 
01019         # if a prior response has been completed, then forget about it.
01020         if self.__response and self.__response.isclosed():
01021             self.__response = None
01022 
01023         # if a prior response exists, then it must be completed (otherwise, we
01024         # cannot read this response's header to determine the connection-close
01025         # behavior)
01026         #
01027         # note: if a prior response existed, but was connection-close, then the
01028         # socket and response were made independent of this HTTPConnection
01029         # object since a new request requires that we open a whole new
01030         # connection
01031         #
01032         # this means the prior response had one of two states:
01033         #   1) will_close: this connection was reset and the prior socket and
01034         #                  response operate independently
01035         #   2) persistent: the response was retained and we await its
01036         #                  isclosed() status to become true.
01037         #
01038         if self.__state != _CS_REQ_SENT or self.__response:
01039             raise ResponseNotReady(self.__state)
01040 
01041         if self.debuglevel > 0:
01042             response = self.response_class(self.sock, self.debuglevel,
01043                                            method=self._method)
01044         else:
01045             response = self.response_class(self.sock, method=self._method)
01046 
01047         response.begin()
01048         assert response.will_close != _UNKNOWN
01049         self.__state = _CS_IDLE
01050 
01051         if response.will_close:
01052             # this effectively passes the connection to the response
01053             self.close()
01054         else:
01055             # remember this, so we can tell when it is complete
01056             self.__response = response
01057 
01058         return response
01059 
try:

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.putheader (   self,
  header,
  values 
) [inherited]
Send a request header line to the server.

For example: h.putheader('Accept', 'text/html')

Definition at line 927 of file client.py.

00927 
00928     def putheader(self, header, *values):
00929         """Send a request header line to the server.
00930 
00931         For example: h.putheader('Accept', 'text/html')
00932         """
00933         if self.__state != _CS_REQ_STARTED:
00934             raise CannotSendHeader()
00935 
00936         if hasattr(header, 'encode'):
00937             header = header.encode('ascii')
00938         values = list(values)
00939         for i, one_value in enumerate(values):
00940             if hasattr(one_value, 'encode'):
00941                 values[i] = one_value.encode('latin1')
00942             elif isinstance(one_value, int):
00943                 values[i] = str(one_value).encode('ascii')
00944         value = b'\r\n\t'.join(values)
00945         header = header + b': ' + value
00946         self._output(header)

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.putrequest (   self,
  method,
  url,
  skip_host = 0,
  skip_accept_encoding = 0 
) [inherited]
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'.
`skip_host' if True does not add automatically a 'Host:' header
`skip_accept_encoding' if True does not add automatically an
   'Accept-Encoding:' header

Definition at line 811 of file client.py.

00811 
00812     def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):
00813         """Send a request to the server.
00814 
00815         `method' specifies an HTTP request method, e.g. 'GET'.
00816         `url' specifies the object being requested, e.g. '/index.html'.
00817         `skip_host' if True does not add automatically a 'Host:' header
00818         `skip_accept_encoding' if True does not add automatically an
00819            'Accept-Encoding:' header
00820         """
00821 
00822         # if a prior response has been completed, then forget about it.
00823         if self.__response and self.__response.isclosed():
00824             self.__response = None
00825 
00826 
00827         # in certain cases, we cannot issue another request on this connection.
00828         # this occurs when:
00829         #   1) we are in the process of sending a request.   (_CS_REQ_STARTED)
00830         #   2) a response to a previous request has signalled that it is going
00831         #      to close the connection upon completion.
00832         #   3) the headers for the previous response have not been read, thus
00833         #      we cannot determine whether point (2) is true.   (_CS_REQ_SENT)
00834         #
00835         # if there is no prior response, then we can request at will.
00836         #
00837         # if point (2) is true, then we will have passed the socket to the
00838         # response (effectively meaning, "there is no prior response"), and
00839         # will open a new one when a new request is made.
00840         #
00841         # Note: if a prior response exists, then we *can* start a new request.
00842         #       We are not allowed to begin fetching the response to this new
00843         #       request, however, until that prior response is complete.
00844         #
00845         if self.__state == _CS_IDLE:
00846             self.__state = _CS_REQ_STARTED
00847         else:
00848             raise CannotSendRequest(self.__state)
00849 
00850         # Save the method we use, we need it later in the response phase
00851         self._method = method
00852         if not url:
00853             url = '/'
00854         request = '%s %s %s' % (method, url, self._http_vsn_str)
00855 
00856         # Non-ASCII characters should have been eliminated earlier
00857         self._output(request.encode('ascii'))
00858 
00859         if self._http_vsn == 11:
00860             # Issue some standard headers for better HTTP/1.1 compliance
00861 
00862             if not skip_host:
00863                 # this header is issued *only* for HTTP/1.1
00864                 # connections. more specifically, this means it is
00865                 # only issued when the client uses the new
00866                 # HTTPConnection() class. backwards-compat clients
00867                 # will be using HTTP/1.0 and those clients may be
00868                 # issuing this header themselves. we should NOT issue
00869                 # it twice; some web servers (such as Apache) barf
00870                 # when they see two Host: headers
00871 
00872                 # If we need a non-standard port,include it in the
00873                 # header.  If the request is going through a proxy,
00874                 # but the host of the actual URL, not the host of the
00875                 # proxy.
00876 
00877                 netloc = ''
00878                 if url.startswith('http'):
00879                     nil, netloc, nil, nil, nil = urlsplit(url)
00880 
00881                 if netloc:
00882                     try:
00883                         netloc_enc = netloc.encode("ascii")
00884                     except UnicodeEncodeError:
00885                         netloc_enc = netloc.encode("idna")
00886                     self.putheader('Host', netloc_enc)
00887                 else:
00888                     try:
00889                         host_enc = self.host.encode("ascii")
00890                     except UnicodeEncodeError:
00891                         host_enc = self.host.encode("idna")
00892 
00893                     # As per RFC 273, IPv6 address should be wrapped with []
00894                     # when used as Host header
00895 
00896                     if self.host.find(':') >= 0:
00897                         host_enc = b'[' + host_enc + b']'
00898 
00899                     if self.port == self.default_port:
00900                         self.putheader('Host', host_enc)
00901                     else:
00902                         host_enc = host_enc.decode("ascii")
00903                         self.putheader('Host', "%s:%s" % (host_enc, self.port))
00904 
00905             # note: we are assuming that clients will not attempt to set these
00906             #       headers since *this* library must deal with the
00907             #       consequences. this also means that when the supporting
00908             #       libraries are updated to recognize other forms, then this
00909             #       code should be changed (removed or updated).
00910 
00911             # we only want a Content-Encoding of "identity" since we don't
00912             # support encodings such as x-gzip or x-deflate.
00913             if not skip_accept_encoding:
00914                 self.putheader('Accept-Encoding', 'identity')
00915 
00916             # we can accept "chunked" Transfer-Encodings, but no others
00917             # NOTE: no TE header implies *only* "chunked"
00918             #self.putheader('TE', 'chunked')
00919 
00920             # if TE is supplied in the header, then it must appear in a
00921             # Connection header.
00922             #self.putheader('Connection', 'TE')
00923 
00924         else:
00925             # For HTTP/1.0, the server will assume "not chunked"
00926             pass

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.request (   self,
  method,
  url,
  body = None,
  headers = {} 
) [inherited]
Send a complete request to the server.

Definition at line 962 of file client.py.

00962 
00963     def request(self, method, url, body=None, headers={}):
00964         """Send a complete request to the server."""
00965         self._send_request(method, url, body, headers)

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.send (   self,
  data 
) [inherited]
Send `data' to the server.
``data`` can be a string object, a bytes object, an array object, a
file-like object that supports a .read() method, or an iterable object.

Definition at line 735 of file client.py.

00735 
00736     def send(self, data):
00737         """Send `data' to the server.
00738         ``data`` can be a string object, a bytes object, an array object, a
00739         file-like object that supports a .read() method, or an iterable object.
00740         """
00741 
00742         if self.sock is None:
00743             if self.auto_open:
00744                 self.connect()
00745             else:
00746                 raise NotConnected()
00747 
00748         if self.debuglevel > 0:
00749             print("send:", repr(data))
00750         blocksize = 8192
00751         if hasattr(data, "read") :
00752             if self.debuglevel > 0:
00753                 print("sendIng a read()able")
00754             encode = False
00755             try:
00756                 mode = data.mode
00757             except AttributeError:
00758                 # io.BytesIO and other file-like objects don't have a `mode`
00759                 # attribute.
00760                 pass
00761             else:
00762                 if "b" not in mode:
00763                     encode = True
00764                     if self.debuglevel > 0:
00765                         print("encoding file using iso-8859-1")
00766             while 1:
00767                 datablock = data.read(blocksize)
00768                 if not datablock:
00769                     break
00770                 if encode:
00771                     datablock = datablock.encode("iso-8859-1")
00772                 self.sock.sendall(datablock)
00773 
00774         try:
00775             self.sock.sendall(data)
00776         except TypeError:
00777             if isinstance(data, collections.Iterable):
00778                 for d in data:
00779                     self.sock.sendall(d)
00780             else:
00781                 raise TypeError("data should be a bytes-like object "
00782                                 "or an iterable, got %r" % type(data))

Here is the call graph for this function:

Here is the caller graph for this function:

def http.client.HTTPConnection.set_debuglevel (   self,
  level 
) [inherited]

Definition at line 690 of file client.py.

00690 
00691     def set_debuglevel(self, level):
00692         self.debuglevel = level

def http.client.HTTPConnection.set_tunnel (   self,
  host,
  port = None,
  headers = None 
) [inherited]
Sets up the host and the port for the HTTP CONNECT Tunnelling.

The headers argument should be a mapping of extra HTTP headers
to send with the CONNECT request.

Definition at line 660 of file client.py.

00660 
00661     def set_tunnel(self, host, port=None, headers=None):
00662         """ Sets up the host and the port for the HTTP CONNECT Tunnelling.
00663 
00664         The headers argument should be a mapping of extra HTTP headers
00665         to send with the CONNECT request.
00666         """
00667         self._tunnel_host = host
00668         self._tunnel_port = port
00669         if headers:
00670             self._tunnel_headers = headers
00671         else:
00672             self._tunnel_headers.clear()


Member Data Documentation

Definition at line 1091 of file client.py.

Definition at line 1090 of file client.py.

int http.client.HTTPConnection.auto_open = 1 [static, inherited]

Definition at line 638 of file client.py.

Definition at line 1077 of file client.py.

int http.client.HTTPConnection.debuglevel = 0 [static, inherited]

Definition at line 639 of file client.py.

Definition at line 691 of file client.py.

Reimplemented from http.client.HTTPConnection.

Definition at line 1067 of file client.py.

Definition at line 687 of file client.py.

Definition at line 1076 of file client.py.

Definition at line 688 of file client.py.

Definition at line 636 of file client.py.

Reimplemented from http.client.HTTPConnection.

Definition at line 1100 of file client.py.

Definition at line 648 of file client.py.

Definition at line 647 of file client.py.


The documentation for this class was generated from the following file: