def _get_ready_socket(self):
     sock = socket.safety_socket(self.timeout, socket.AF_INET,
                                 socket.SOCK_STREAM)
     self.msglog('Going to connect to server')
     sock.connect((self._server, self._port))
     http_sock = sock
     if self._secure:
         realsock = sock
         if hasattr(sock, '_sock'):
             realsock = sock._sock
         ssl = socket.ssl(realsock, properties.PRIVATE_KEY_FILE,
                          properties.CERTIFICATE_PEM_FILE)
         http_sock = FakeSocket(sock, ssl)
     return http_sock
class TimeoutHTTPSConnection(TimeoutHTTPConnection):
    """A timeout control enabled HTTPSConnection."""
    default_port = HTTPS_PORT
    _timeout = None

    def __init__(self,
                 host,
                 port=None,
                 key_file=None,
                 cert_file=None,
                 strict=None,
                 timeout=None):
        """Initialize the object.

    Args:
      host: the connection host.
      port: optional port.
      key_file: the ssl key file.
      cert_file: the ssl cert file.
      strict: strict connection.
      timeout: socket connection timeout value.

    TimeoutHTTPSConnection maintains its own _timeout attribute and
    does not override the its super class's value.
    """
        TimeoutHTTPConnection.__init__(self, host, port, strict)
        self._timeout = timeout or TimeoutHTTPSConnection._timeout
        if self._timeout: self._timeout = float(self._timeout)
        self.key_file = key_file
        self.cert_file = cert_file

    def connect(self):
        """Perform the secure http connection.
    
    Raises:
      HTTPSConnectionTimeoutError: when timeout is hit.
    """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if self._timeout: sock.settimeout(self._timeout)
        try:
            sock.connect((self.host, self.port))
        except socket.timeout, msg:
            raise HTTPSConnectionTimeoutError, msg
        ssl = socket.ssl(sock, self.key_file, self.cert_file)
        self.sock = FakeSocket(sock, ssl)
Esempio n. 3
0
    def connect(self):
        """Connect (using SSL) to the host and port specified in __init__
        (through a proxy)."""
        import socket
        # Set the connection with the proxy
        HTTPProxyConnection.connect(self)
        # Use the stock HTTPConnection putrequest
        host = "%s:%s" % (self._host, self._port)
        HTTPConnection.putrequest(self, "CONNECT", host)
        # Add proxy-specific stuff
        self._add_auth_proxy_header()
        # And send the request
        HTTPConnection.endheaders(self)
        # Save the response class
        response_class = self.response_class
        # And replace the response class with our own one, which does not
        # close the connection
        self.response_class = HTTPSProxyResponse
        response = HTTPConnection.getresponse(self)
        # Restore the response class
        self.response_class = response_class
        # Close the response object manually
        response.close()
        if response.status != 200:
            # Close the connection manually
            self.close()
            # XXX Find the appropriate error code
            raise socket.error(1001, response.status, response.value)

        # NgPS: I haven't read the code recently, but I think it is
        # reasonable to assume that self.sock is a connected TCP socket at
        # this point.

        # Use the real stuff. ;-)
        if self.ssl_ctx and isinstance(self.ssl_ctx, SSL.Context):
           self.sock =  SSL.Connection(self.ssl_ctx)
           self.sock.connect((self.host, self.port))
        else:
           # Fake the socket
           ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
           self.sock = FakeSocket(self.sock, ssl)
        if self.debuglevel > 0: print('socket type:', self.sock)
Esempio n. 4
0
        def connect(self):
            "Connect to a host on a given (SSL) port."
            try:
                self.sock = socket.create_connection(
                    (self.host, self.port), self.timeout, self.source_address)
            except (AttributeError, TypeError):
                self.sock = create_connection(
                    (self.host, self.port), self.timeout, self.source_address)

            if self._tunnel_host:
                self._tunnel()

            if ssl:
                try:
                    kwargs = {}
                    if hasattr(ssl, 'SSLContext'):
                        if self._tunnel_host:
                            kwargs['server_hostname'] = self._tunnel_host
                        else:
                            kwargs['server_hostname'] = self.host
                    self.sock = self._context.wrap_socket(self.sock, **kwargs)
                except AttributeError:
                    self.sock = ssl.wrap_socket(self.sock)
                    try:
                        self.sock.server_hostname = self.host
                    except AttributeError:
                        pass
            elif FakeSocket:
                # Python 2.4/2.5 support
                try:
                    self.sock = FakeSocket(self.sock, socket.ssl(self.sock))
                except AttributeError:
                    raise SpeedtestException(
                        'This version of Python does not support HTTPS/SSL '
                        'functionality.')
            else:
                raise SpeedtestException(
                    'This version of Python does not support HTTPS/SSL '
                    'functionality.')
Esempio n. 5
0
 def __init__(self, sock, ssl):
     FakeSocket.__init__(self, sock, ssl)
     self.ok = True
Esempio n. 6
0
 def __init__(self, sock, ssl):
     FakeSocket.__init__(self, sock, ssl)
     self.ok = True
Esempio n. 7
0
 def _ssl_wrapper(sck, **kwargs):
     ssl_sck = socket.ssl(sck, **kwargs)
     return FakeSocket(sck, ssl_sck)
Esempio n. 8
0
File: net.py Progetto: peicheng/zine
 def wrap_socket(sock, key, cert):
     ssl = socket.ssl(sock, key, cert)
     return FakeSocket(sock, ssl)