Python httplib.FakeSocket() Examples

The following are 28 code examples of httplib.FakeSocket(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib , or try the search function .
Example #1
Source File: connection.py    From conary with Apache License 2.0 6 votes vote down vote up
def startSSL(self, sock):
        """If needed, start SSL on the proxy or endpoint connection."""
        if not self.doSSL:
            return sock
        if self.caCerts:
            # If cert checking is requested use m2crypto
            if SSL:
                return startSSLWithChecker(sock, self.caCerts, self.commonName)
            else:
                warnings.warn("m2crypto is not installed; server certificates "
                        "will not be validated!")
        try:
            # Python >= 2.6
            import ssl
            return ssl.SSLSocket(sock)
        except ImportError:
            # Python < 2.6
            sslSock = socket.ssl(sock, None, None)
            return httplib.FakeSocket(sock, sslSock) 
Example #2
Source File: __init__.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(
    sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname
):
    if not disable_validation:
        raise CertificateValidationUnsupported(
            "SSL certificate validation is not supported without "
            "the ssl module installed. To avoid this error, install "
            "the ssl module, or explicity disable validation."
        )
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #3
Source File: __init__.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #4
Source File: __init__.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #5
Source File: __init__.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #6
Source File: __init__.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #7
Source File: __init__.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #8
Source File: proxy.py    From darkc0de-old-stuff with GNU General Public License v3.0 5 votes vote down vote up
def connect(self):
        ProxyHTTPConnection.connect(self)

        # Make the sock ssl-aware
        ssl = socket.ssl(self.sock, self.key_file, self.cert_file)
        self.sock = httplib.FakeSocket(self.sock, ssl) 
Example #9
Source File: __init__.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #10
Source File: __init__.py    From Sepia with GNU General Public License v2.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #11
Source File: Utility.py    From p2pool-n with GNU General Public License v3.0 5 votes vote down vote up
def connect(self):
        sock = TimeoutSocket(self.timeout)
        sock.connect((self.host, self.port))
        realsock = getattr(sock.sock, '_sock', sock.sock)
        ssl = socket.ssl(realsock, self.key_file, self.cert_file)
        self.sock = httplib.FakeSocket(sock, ssl) 
Example #12
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(
    sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname
):
    if not disable_validation:
        raise CertificateValidationUnsupported(
            "SSL certificate validation is not supported without "
            "the ssl module installed. To avoid this error, install "
            "the ssl module, or explicity disable validation."
        )
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #13
Source File: __init__.py    From twitter-for-bigquery with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #14
Source File: __init__.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(
    sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname
):
    if not disable_validation:
        raise CertificateValidationUnsupported(
            "SSL certificate validation is not supported without "
            "the ssl module installed. To avoid this error, install "
            "the ssl module, or explicity disable validation."
        )
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #15
Source File: __init__.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(
    sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname
):
    if not disable_validation:
        raise CertificateValidationUnsupported(
            "SSL certificate validation is not supported without "
            "the ssl module installed. To avoid this error, install "
            "the ssl module, or explicity disable validation."
        )
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #16
Source File: __init__.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(
    sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname
):
    if not disable_validation:
        raise CertificateValidationUnsupported(
            "SSL certificate validation is not supported without "
            "the ssl module installed. To avoid this error, install "
            "the ssl module, or explicity disable validation."
        )
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #17
Source File: __init__.py    From alfred-gmail with MIT License 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(
    sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname
):
    if not disable_validation:
        raise CertificateValidationUnsupported(
            "SSL certificate validation is not supported without "
            "the ssl module installed. To avoid this error, install "
            "the ssl module, or explicity disable validation."
        )
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #18
Source File: __init__.py    From googleapps-message-recall with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #19
Source File: __init__.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #20
Source File: __init__.py    From billing-export-python with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #21
Source File: __init__.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #22
Source File: __init__.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(sock, key_file, cert_file, disable_validation,
                                 ca_certs, ssl_version, hostname):
    if not disable_validation:
        raise CertificateValidationUnsupported(
                "SSL certificate validation is not supported without "
                "the ssl module installed. To avoid this error, install "
                "the ssl module, or explicity disable validation.")
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #23
Source File: __init__.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def _ssl_wrap_socket_unsupported(sock, key_file, cert_file, disable_validation,
                                 ca_certs, ssl_version, hostname):
    if not disable_validation:
        raise CertificateValidationUnsupported(
                "SSL certificate validation is not supported without "
                "the ssl module installed. To avoid this error, install "
                "the ssl module, or explicity disable validation.")
    ssl_sock = socket.ssl(sock, key_file, cert_file)
    return httplib.FakeSocket(sock, ssl_sock) 
Example #24
Source File: __init__.py    From earthengine with MIT License 5 votes vote down vote up
def _ssl_wrap_socket(sock, key_file, cert_file,
                         disable_validation, ca_certs):
        if not disable_validation:
            raise CertificateValidationUnsupported(
                    "SSL certificate validation is not supported without "
                    "the ssl module installed. To avoid this error, install "
                    "the ssl module, or explicity disable validation.")
        ssl_sock = socket.ssl(sock, key_file, cert_file)
        return httplib.FakeSocket(sock, ssl_sock) 
Example #25
Source File: connection.py    From canvas with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def proxy_ssl(self):
        host = '%s:%d' % (self.host, self.port)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect((self.proxy, int(self.proxy_port)))
        except:
            raise
        boto.log.debug("Proxy connection: CONNECT %s HTTP/1.0\r\n", host)
        sock.sendall("CONNECT %s HTTP/1.0\r\n" % host)
        sock.sendall("User-Agent: %s\r\n" % UserAgent)
        if self.proxy_user and self.proxy_pass:
            for k, v in self.get_proxy_auth_header().items():
                sock.sendall("%s: %s\r\n" % (k, v))
        sock.sendall("\r\n")
        resp = httplib.HTTPResponse(sock, strict=True, debuglevel=self.debug)
        resp.begin()

        if resp.status != 200:
            # Fake a socket error, use a code that make it obvious it hasn't
            # been generated by the socket library
            raise socket.error(-71,
                               "Error talking to HTTP proxy %s:%s: %s (%s)" %
                               (self.proxy, self.proxy_port, resp.status, resp.reason))

        # We can safely close the response, it duped the original socket
        resp.close()

        h = httplib.HTTPConnection(host)

        if self.https_validate_certificates and HAVE_HTTPS_CONNECTION:
            boto.log.debug("wrapping ssl socket for proxied connection; "
                           "CA certificate file=%s",
                           self.ca_certificates_file)
            key_file = self.http_connection_kwargs.get('key_file', None)
            cert_file = self.http_connection_kwargs.get('cert_file', None)
            sslSock = ssl.wrap_socket(sock, keyfile=key_file,
                                      certfile=cert_file,
                                      cert_reqs=ssl.CERT_REQUIRED,
                                      ca_certs=self.ca_certificates_file)
            cert = sslSock.getpeercert()
            hostname = self.host.split(':', 0)[0]
            if not https_connection.ValidateCertificateHostname(cert, hostname):
                raise https_connection.InvalidCertificateException(
                        hostname, cert, 'hostname mismatch')
        else:
            # Fallback for old Python without ssl.wrap_socket
            if hasattr(httplib, 'ssl'):
                sslSock = httplib.ssl.SSLSocket(sock)
            else:
                sslSock = socket.ssl(sock, None, None)
                sslSock = httplib.FakeSocket(sock, sslSock)

        # This is a bit unclean
        h.sock = sslSock
        return h 
Example #26
Source File: http_core.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def _get_connection(self, uri, headers=None):
    # Check to see if there are proxy settings required for this request.
    proxy = None
    if uri.scheme == 'https':
      proxy = os.environ.get('https_proxy')
    elif uri.scheme == 'http':
      proxy = os.environ.get('http_proxy')
    if not proxy:
      return HttpClient._get_connection(self, uri, headers=headers)
    # Now we have the URL of the appropriate proxy server.
    # Get a username and password for the proxy if required.
    proxy_auth = _get_proxy_auth()
    if uri.scheme == 'https':
      import socket
      if proxy_auth:
        proxy_auth = 'Proxy-authorization: %s' % proxy_auth
      # Construct the proxy connect command.
      port = uri.port
      if not port:
        port = 443
      proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (uri.host, port)
      # Set the user agent to send to the proxy
      user_agent = ''
      if headers and 'User-Agent' in headers:
        user_agent = 'User-Agent: %s\r\n' % (headers['User-Agent'])
      proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent)
      # Find the proxy host and port.
      proxy_uri = Uri.parse_uri(proxy)
      if not proxy_uri.port:
        proxy_uri.port = '80'
      # Connect to the proxy server, very simple recv and error checking
      p_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      p_sock.connect((proxy_url.host, int(proxy_url.port)))
      p_sock.sendall(proxy_pieces)
      response = ''
      # Wait for the full response.
      while response.find("\r\n\r\n") == -1:
        response += p_sock.recv(8192)
      p_status = response.split()[1]
      if p_status != str(200):
        raise ProxyError('Error status=%s' % str(p_status))
      # Trivial setup for ssl socket.
      ssl = socket.ssl(p_sock, None, None)
      fake_sock = httplib.FakeSocket(p_sock, ssl)
      # Initalize httplib and replace with the proxy socket.
      connection = httplib.HTTPConnection(proxy_url.host)
      connection.sock=fake_sock
      return connection
    elif uri.scheme == 'http':
      proxy_url = Uri.parse_uri(proxy)
      if not proxy_url.port:
        proxy_uri.port = '80'
      if proxy_auth:
        headers['Proxy-Authorization'] = proxy_auth.strip()
      return httplib.HTTPConnection(proxy_uri.host, int(proxy_uri.port))
    return None 
Example #27
Source File: http_core.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def _get_connection(self, uri, headers=None):
    # Check to see if there are proxy settings required for this request.
    proxy = None
    if uri.scheme == 'https':
      proxy = os.environ.get('https_proxy')
    elif uri.scheme == 'http':
      proxy = os.environ.get('http_proxy')
    if not proxy:
      return HttpClient._get_connection(self, uri, headers=headers)
    # Now we have the URL of the appropriate proxy server.
    # Get a username and password for the proxy if required.
    proxy_auth = _get_proxy_auth()
    if uri.scheme == 'https':
      import socket
      if proxy_auth:
        proxy_auth = 'Proxy-authorization: %s' % proxy_auth
      # Construct the proxy connect command.
      port = uri.port
      if not port:
        port = 443
      proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (uri.host, port)
      # Set the user agent to send to the proxy
      user_agent = ''
      if headers and 'User-Agent' in headers:
        user_agent = 'User-Agent: %s\r\n' % (headers['User-Agent'])
      proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent)
      # Find the proxy host and port.
      proxy_uri = Uri.parse_uri(proxy)
      if not proxy_uri.port:
        proxy_uri.port = '80'
      # Connect to the proxy server, very simple recv and error checking
      p_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      p_sock.connect((proxy_url.host, int(proxy_url.port)))
      p_sock.sendall(proxy_pieces)
      response = ''
      # Wait for the full response.
      while response.find("\r\n\r\n") == -1:
        response += p_sock.recv(8192)
      p_status = response.split()[1]
      if p_status != str(200):
        raise ProxyError('Error status=%s' % str(p_status))
      # Trivial setup for ssl socket.
      ssl = socket.ssl(p_sock, None, None)
      fake_sock = httplib.FakeSocket(p_sock, ssl)
      # Initalize httplib and replace with the proxy socket.
      connection = httplib.HTTPConnection(proxy_url.host)
      connection.sock=fake_sock
      return connection
    elif uri.scheme == 'http':
      proxy_url = Uri.parse_uri(proxy)
      if not proxy_url.port:
        proxy_uri.port = '80'
      if proxy_auth:
        headers['Proxy-Authorization'] = proxy_auth.strip()
      return httplib.HTTPConnection(proxy_uri.host, int(proxy_uri.port))
    return None 
Example #28
Source File: speedtestnet.py    From SSRSpeed with GNU General Public License v3.0 4 votes vote down vote up
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'
                )