Python six.moves.http_client.HTTPConnection() Examples

The following are 30 code examples of six.moves.http_client.HTTPConnection(). 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 six.moves.http_client , or try the search function .
Example #1
Source File: runner.py    From canari3 with GNU General Public License v3.0 6 votes vote down vote up
def remote_canari_transform_runner(host, base_path, transform, entities, parameters, limits, is_ssl=False):
    c = http_client.HTTPSConnection(host) if is_ssl else http_client.HTTPConnection(host)

    m = MaltegoTransformRequestMessage()

    for e in entities:
        m += e

    for p in parameters:
        m += p

    m += limits

    msg = MaltegoMessage(message=m).render(encoding='utf-8')
    path = re.sub(r'/+', '/', '/'.join([base_path, transform]))

    if is_debug_exec_mode():
        sys.stderr.write("Sending following message to {}{}:\n{}\n\n".format(host, path, msg))

    c.request('POST', path, msg, headers={'Content-Type': 'application/xml'})

    return c.getresponse() 
Example #2
Source File: limits.py    From manila with Apache License 2.0 6 votes vote down vote up
def check_for_delay(self, verb, path, username=None):
        body = jsonutils.dumps({"verb": verb, "path": path})
        headers = {"Content-Type": "application/json"}

        conn = http_client.HTTPConnection(self.limiter_address)

        if username:
            conn.request("POST", "/%s" % (username), body, headers)
        else:
            conn.request("POST", "/", body, headers)

        resp = conn.getresponse()

        if 200 >= resp.status < 300:
            return None, None

        return resp.getheader("X-Wait-Seconds"), resp.read() or None

    # Note: This method gets called before the class is instantiated,
    # so this must be either a static method or a class method.  It is
    # used to develop a list of limits to feed to the constructor.
    # This implementation returns an empty list, since all limit
    # decisions are made by a remote server. 
Example #3
Source File: scheduler.py    From pymesos with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_version(self, master):
        if master is not None:
            conn = None
            host, port = master.split(':', 2)
            port = int(port)
            try:
                conn = HTTPConnection(host, port, timeout=self._timeout)
                conn.request('GET', '/version')
                resp = conn.getresponse()
                if resp.status < 200 or resp.status >= 300:
                    return

                return json.loads(resp.read().decode('utf-8'))['version']
            except Exception:
                logger.exception('Error')
                pass
            finally:
                if conn:
                    conn.close() 
Example #4
Source File: server_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testDataPaths_disableAllCaching(self):
    """Test the format of the /data/runs endpoint."""
    for path in ('/data/runs',
                 '/data/logdir',
                 '/data/scalars?run=run1&tag=simple_values',
                 '/data/scalars?run=run1&tag=simple_values&format=csv',
                 '/data/images?run=run1&tag=image',
                 '/data/individualImage?run=run1&tag=image&index=0',
                 '/data/audio?run=run1&tag=audio',
                 '/data/run_metadata?run=run1&tag=test%20run'):
      connection = http_client.HTTPConnection(
          'localhost', self._server.server_address[1])
      connection.request('GET', path)
      response = connection.getresponse()
      self.assertEqual(response.status, 200, msg=path)
      self.assertEqual(response.getheader('Expires'), '0', msg=path)
      response.read()
      connection.close() 
Example #5
Source File: test_http.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_http_over_https(self):
        if self.scheme != 'https':
            return self.skip('skipped (not running HTTPS)... ')

        # Try connecting without SSL.
        conn = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
        conn.putrequest('GET', '/', skip_host=True)
        conn.putheader('Host', self.HOST)
        conn.endheaders()
        response = conn.response_class(conn.sock, method='GET')
        try:
            response.begin()
            self.assertEqual(response.status, 400)
            self.body = response.read()
            self.assertBody('The client sent a plain HTTP request, but this '
                            'server only speaks HTTPS on this port.')
        except socket.error:
            e = sys.exc_info()[1]
            # "Connection reset by peer" is also acceptable.
            if e.errno != errno.ECONNRESET:
                raise 
Example #6
Source File: cloudstack.py    From cloudbase-init with Apache License 2.0 6 votes vote down vote up
def _password_client(self, body=None, headers=None, decode=True):
        """Client for the Password Server."""
        port = CONF.cloudstack.password_server_port
        with contextlib.closing(http_client.HTTPConnection(
                self._metadata_host, port, timeout=TIMEOUT)) as connection:
            try:
                connection.request("GET", "/", body=body, headers=headers)
                response = connection.getresponse()
            except http_client.HTTPException as exc:
                LOG.error("Request failed: %s", exc)
                raise

            content = response.read()
            if decode:
                content = encoding.get_as_string(content)

            if response.status != 200:
                raise http_client.HTTPException(
                    "%(status)s %(reason)s - %(message)r",
                    {"status": response.status, "reason": response.reason,
                     "message": content})

        return content 
Example #7
Source File: publisher.py    From PerfKitBenchmarker with Apache License 2.0 6 votes vote down vote up
def _CreateDB(self):
    """This method is idempotent. If the DB already exists it will simply
    return a 200 code without re-creating it.
    """
    successful_http_request_codes = [200, 202, 204]
    header = {'Content-type': 'application/x-www-form-urlencoded',
              'Accept': 'text/plain'}
    params = urllib.parse.urlencode(
        {'q': 'CREATE DATABASE ' + self.influx_db_name})
    conn = httplib.HTTPConnection(self.influx_uri)
    conn.request('POST', '/query?' + params, headers=header)
    response = conn.getresponse()
    conn.close()
    if response.status in successful_http_request_codes:
      logging.debug('Success! %s DB Created', self.influx_db_name)
    else:
      logging.error('%d Request could not be completed due to: %s',
                    response.status, response.reason)
      raise httplib.HTTPException 
Example #8
Source File: test_middleware_auth.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def ping_barbican(token_id):
    headers = {'X_AUTH_TOKEN': token_id, 'X_IDENTITY_STATUS': 'Confirmed'}
    connection = http_client.HTTPConnection(host, port, timeout=timeout)
    connection.request(method, path, None, headers)
    response = connection.getresponse().read()
    connection.close()
    return response 
Example #9
Source File: ipc.py    From spavro with Apache License 2.0 5 votes vote down vote up
def __init__(self, host, port, req_resource='/'):
    self.req_resource = req_resource
    self.conn = httplib.HTTPConnection(host, port)
    self.conn.connect()

  # read-only properties 
Example #10
Source File: THttpClient.py    From SOLO with GNU General Public License v3.0 5 votes vote down vote up
def open(self):
        if self.scheme == 'http':
            self.__http = http_client.HTTPConnection(self.host, self.port)
        elif self.scheme == 'https':
            self.__http = http_client.HTTPSConnection(self.host, self.port) 
Example #11
Source File: testing.py    From cheroot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_connection(self):
        name = '{interface}:{port}'.format(
            interface=self._interface,
            port=self._port,
        )
        conn_cls = (
            http_client.HTTPConnection
            if self.server_instance.ssl_adapter is None else
            http_client.HTTPSConnection
        )
        return conn_cls(name) 
Example #12
Source File: __init__.py    From girder_worker with Apache License 2.0 5 votes vote down vote up
def __init__(self, url, headers={}):
        self._url = url

        """
        Uses HTTP chunked transfer-encoding to stream a request body to a
        server. Unfortunately requests does not support hooking into this logic
        easily, so we use the lower-level httplib module.
        """
        self._closed = False

        parts = urllib.parse.urlparse(self._url)
        if parts.scheme == 'https':
            ssl_context = ssl.create_default_context()
            conn = httplib.HTTPSConnection(parts.netloc, context=ssl_context)
        else:
            conn = httplib.HTTPConnection(parts.netloc)

        try:
            url = parts.path
            if parts.query is not None:
                url = '%s?%s' % (url, parts.query)
            conn.putrequest('POST',
                            url, skip_accept_encoding=True)

            for header, value in headers.items():
                conn.putheader(header, value)

            conn.putheader('Transfer-Encoding', 'chunked')

            conn.endheaders()  # This actually flushes the headers to the server
        except Exception:
            sys.stderr.write('HTTP connection to "%s" failed.\n' % self._url)
            conn.close()
            raise

        self.conn = conn 
Example #13
Source File: webtest.py    From cheroot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _Conn(self):
        """Return HTTPConnection or HTTPSConnection based on self.scheme.

        * from http.client.
        """
        cls_name = '{scheme}Connection'.format(scheme=self.scheme.upper())
        return getattr(http_client, cls_name) 
Example #14
Source File: webtest.py    From cheroot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _open_url_once(
    url, headers=None, method='GET', body=None,
    host='127.0.0.1', port=8000, http_conn=http_client.HTTPConnection,
    protocol='HTTP/1.1', ssl_context=None,
):
    """Open the given HTTP resource and return status, headers, and body."""
    headers = cleanHeaders(headers, method, body, host, port)

    # Allow http_conn to be a class or an instance
    if hasattr(http_conn, 'host'):
        conn = http_conn
    else:
        kw = {}
        if ssl_context:
            kw['context'] = ssl_context
        conn = http_conn(interface(host), port, **kw)
    conn._http_vsn_str = protocol
    conn._http_vsn = int(''.join([x for x in protocol if x.isdigit()]))
    if not six.PY2 and isinstance(url, bytes):
        url = url.decode()
    conn.putrequest(
        method.upper(), url, skip_host=True,
        skip_accept_encoding=True,
    )
    for key, value in headers:
        conn.putheader(key, value.encode('Latin-1'))
    conn.endheaders()
    if body is not None:
        conn.send(body)
    # Handle response
    response = conn.getresponse()
    s, h, b = shb(response)
    if not hasattr(http_conn, 'host'):
        # We made our own conn instance. Close it.
        conn.close()
    return s, h, b 
Example #15
Source File: test_middleware_auth.py    From barbican with Apache License 2.0 5 votes vote down vote up
def ping_barbican(token_id):
    headers = {'X_AUTH_TOKEN': token_id, 'X_IDENTITY_STATUS': 'Confirmed'}
    connection = http_client.HTTPConnection(host, port, timeout=timeout)
    connection.request(method, path, None, headers)
    response = connection.getresponse().read()
    connection.close()
    return response 
Example #16
Source File: util.py    From guildai with Apache License 2.0 5 votes vote down vote up
def _HTTPConnection(scheme, netloc, timeout):
    from six.moves import http_client

    if scheme == "http":
        return http_client.HTTPConnection(netloc, timeout=timeout)
    elif scheme == "https":
        return http_client.HTTPSConnection(netloc, timeout=timeout)
    else:
        raise ValueError("unsupported scheme '%s' - must be 'http' or 'https'" % scheme) 
Example #17
Source File: xmlrpc.py    From odoo-rpc-client with Mozilla Public License 2.0 5 votes vote down vote up
def make_connection(self, host):
            # -- Based on original python code --
            #
            # return an existing connection if possible.  This allows
            # HTTP/1.1 keep-alive.
            if self._connection and host == self._connection[0]:
                return self._connection[1]

            # create a HTTP connection object from a host descriptor
            chost, self._extra_headers, x509 = self.get_host_info(host)
            # store the host argument along with the connection object
            self._connection = host, httplib.HTTPConnection(
                chost, timeout=self.timeout)
            return self._connection[1] 
Example #18
Source File: xmlrpc.py    From odoo-rpc-client with Mozilla Public License 2.0 5 votes vote down vote up
def make_connection(self, host):
            # -- Based on original python code --
            #
            # return an existing connection if possible.  This allows
            # HTTP/1.1 keep-alive.
            if self._connection and host == self._connection[0]:
                return self._connection[1]

            # create a HTTP connection object from a host descriptor
            chost, self._extra_headers, x509 = self.get_host_info(host)
            # store the host argument along with the connection object
            self._connection = host, httplib.HTTPConnection(
                chost, timeout=self.timeout)
            return self._connection[1] 
Example #19
Source File: _thrift_api.py    From impyla with Apache License 2.0 5 votes vote down vote up
def open(self):
    if self.scheme == 'http':
      self.__http = http_client.HTTPConnection(self.host, self.port,
                                               timeout=self.__timeout)
    elif self.scheme == 'https':
      self.__http = http_client.HTTPSConnection(self.host, self.port,
                                                key_file=self.keyfile,
                                                cert_file=self.certfile,
                                                timeout=self.__timeout,
                                                context=self.context)
    if self.using_proxy():
      self.__http.set_tunnel(self.realhost, self.realport,
                             {"Proxy-Authorization": self.proxy_auth}) 
Example #20
Source File: connection.py    From pylxd with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        http_client.HTTPConnection.__init__(self, *args, **kwargs) 
Example #21
Source File: test_refleaks.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_threadlocal_garbage(self):
        if platform.system() == 'Darwin':
            self.skip('queue issues; see #1474')
        success = itertools.count()

        def getpage():
            host = '%s:%s' % (self.interface(), self.PORT)
            if self.scheme == 'https':
                c = HTTPSConnection(host)
            else:
                c = HTTPConnection(host)
            try:
                c.putrequest('GET', '/')
                c.endheaders()
                response = c.getresponse()
                body = response.read()
                self.assertEqual(response.status, 200)
                self.assertEqual(body, b'Hello world!')
            finally:
                c.close()
            next(success)

        ITERATIONS = 25

        ts = [
            threading.Thread(target=getpage)
            for _ in range(ITERATIONS)
        ]

        for t in ts:
            t.start()

        for t in ts:
            t.join()

        self.assertEqual(next(success), ITERATIONS) 
Example #22
Source File: test_http.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def make_connection(self):
        if self.scheme == 'https':
            return HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
        else:
            return HTTPConnection('%s:%s' % (self.interface(), self.PORT)) 
Example #23
Source File: test_http.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_no_content_length(self):
        # "The presence of a message-body in a request is signaled by the
        # inclusion of a Content-Length or Transfer-Encoding header field in
        # the request's message-headers."
        #
        # Send a message with neither header and no body. Even though
        # the request is of method POST, this should be OK because we set
        # request.process_request_body to False for our handler.
        c = self.make_connection()
        c.request('POST', '/no_body')
        response = c.getresponse()
        self.body = response.fp.read()
        self.status = str(response.status)
        self.assertStatus(200)
        self.assertBody(b'Hello world!')

        # Now send a message that has no Content-Length, but does send a body.
        # Verify that CP times out the socket and responds
        # with 411 Length Required.
        if self.scheme == 'https':
            c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
        else:
            c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))

        # `_get_content_length` is needed for Python 3.6+
        with mock.patch.object(
                c,
                '_get_content_length',
                lambda body, method: None,
                create=True):
            # `_set_content_length` is needed for Python 2.7-3.5
            with mock.patch.object(c, '_set_content_length', create=True):
                c.request('POST', '/')

        response = c.getresponse()
        self.body = response.fp.read()
        self.status = str(response.status)
        self.assertStatus(411) 
Example #24
Source File: testing.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def get_connection(self):
        name = '{interface}:{port}'.format(
            interface=self._interface,
            port=self._port,
        )
        conn_cls = (
            http_client.HTTPConnection
            if self.server_instance.ssl_adapter is None else
            http_client.HTTPSConnection
        )
        return conn_cls(name) 
Example #25
Source File: webtest.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def _Conn(self):
        """Return HTTPConnection or HTTPSConnection based on self.scheme.

        * from http.client.
        """
        cls_name = '{scheme}Connection'.format(scheme=self.scheme.upper())
        return getattr(http_client, cls_name) 
Example #26
Source File: webtest.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def _open_url_once(
    url, headers=None, method='GET', body=None,
    host='127.0.0.1', port=8000, http_conn=http_client.HTTPConnection,
    protocol='HTTP/1.1', ssl_context=None,
):
    """Open the given HTTP resource and return status, headers, and body."""
    headers = cleanHeaders(headers, method, body, host, port)

    # Allow http_conn to be a class or an instance
    if hasattr(http_conn, 'host'):
        conn = http_conn
    else:
        kw = {}
        if ssl_context:
            kw['context'] = ssl_context
        conn = http_conn(interface(host), port, **kw)
    conn._http_vsn_str = protocol
    conn._http_vsn = int(''.join([x for x in protocol if x.isdigit()]))
    if not six.PY2 and isinstance(url, bytes):
        url = url.decode()
    conn.putrequest(
        method.upper(), url, skip_host=True,
        skip_accept_encoding=True,
    )
    for key, value in headers:
        conn.putheader(key, value.encode('Latin-1'))
    conn.endheaders()
    if body is not None:
        conn.send(body)
    # Handle response
    response = conn.getresponse()
    s, h, b = shb(response)
    if not hasattr(http_conn, 'host'):
        # We made our own conn instance. Close it.
        conn.close()
    return s, h, b 
Example #27
Source File: publisher.py    From PerfKitBenchmarker with Apache License 2.0 5 votes vote down vote up
def _WriteData(self, data):
    successful_http_request_codes = [200, 202, 204]
    params = data
    header = {"Content-type": "application/octet-stream"}
    conn = httplib.HTTPConnection(self.influx_uri)
    conn.request('POST', '/write?' + 'db=' + self.influx_db_name, params,
                 headers=header)
    response = conn.getresponse()
    conn.close()
    if response.status in successful_http_request_codes:
      logging.debug('Writing samples to publisher: writing samples.')
    else:
      logging.error('%d Request could not be completed due to: %s %s',
                    response.status, response.reason, data)
      raise httplib.HTTPException 
Example #28
Source File: THttpClient.py    From thrift with GNU Lesser General Public License v3.0 5 votes vote down vote up
def open(self):
        if self.scheme == 'http':
            self.__http = http_client.HTTPConnection(self.host, self.port)
        elif self.scheme == 'https':
            self.__http = http_client.HTTPSConnection(self.host, self.port) 
Example #29
Source File: client.py    From manila with Apache License 2.0 5 votes vote down vote up
def request(self, url, method='GET', body=None, headers=None):
        _headers = {'Content-Type': 'application/json'}
        _headers.update(headers or {})

        parsed_url = parse.urlparse(url)
        port = parsed_url.port
        hostname = parsed_url.hostname
        scheme = parsed_url.scheme

        if scheme == 'http':
            conn = http_client.HTTPConnection(hostname,
                                              port=port)
        elif scheme == 'https':
            conn = http_client.HTTPSConnection(hostname,
                                               port=port)
        else:
            raise OpenStackApiException("Unknown scheme: %s" % url)

        relative_url = parsed_url.path
        if parsed_url.query:
            relative_url = relative_url + "?" + parsed_url.query
        LOG.info("Doing %(method)s on %(relative_url)s",
                 {"method": method, "relative_url": relative_url})
        if body:
            LOG.info("Body: %s", body)

        conn.request(method, relative_url, body, _headers)
        response = conn.getresponse()
        return response 
Example #30
Source File: scheduler.py    From pymesos with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_conn(self):
        if not self.connected:
            return None

        if self._conn is not None:
            return self._conn

        host, port = self.master.split(':', 2)
        port = int(port)
        self._conn = HTTPConnection(host, port, timeout=self._timeout)
        return self._conn