Python urllib3.connection() Examples

The following are 7 code examples of urllib3.connection(). 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 urllib3 , or try the search function .
Example #1
Source File: requests_wrapper.py    From py-ipfs-http-client with MIT License 6 votes vote down vote up
def _new_conn(self):
		extra_kw = {
			"family": self.family
		}
		if self.source_address:
			extra_kw['source_address'] = self.source_address

		if self.socket_options:
			extra_kw['socket_options'] = self.socket_options

		try:
			dns_host = getattr(self, "_dns_host", self.host)
			conn = create_connection(
				(dns_host, self.port), self.timeout, **extra_kw)
		except socket.timeout:
			raise urllib3.exceptions.ConnectTimeoutError(
				self, "Connection to %s timed out. (connect timeout=%s)" %
				(self.host, self.timeout))
		except OSError as e:
			raise urllib3.exceptions.NewConnectionError(
				self, "Failed to establish a new connection: %s" % e)

		return conn 
Example #2
Source File: patch.py    From aws-xray-sdk-python with Apache License 2.0 5 votes vote down vote up
def _send_request(wrapped, instance, args, kwargs):
    def decompose_args(method, url, body, headers, encode_chunked=False):
        # skip httplib tracing for SDK built-in centralized sampling pollers
        if (('/GetSamplingRules' in args or '/SamplingTargets' in args) and
                type(instance).__name__ == 'botocore.awsrequest.AWSHTTPConnection'):
            return wrapped(*args, **kwargs)

        # Only injects headers when the subsegment for the outgoing
        # calls are opened successfully.
        subsegment = None
        try:
            subsegment = xray_recorder.current_subsegment()
        except SegmentNotFoundException:
            pass
        if subsegment:
            inject_trace_header(headers, subsegment)

        if issubclass(instance.__class__, urllib3.connection.HTTPSConnection):
            ssl_cxt = getattr(instance, 'ssl_context', None)
        elif issubclass(instance.__class__, httplib.HTTPSConnection):
            ssl_cxt = getattr(instance, '_context', None)
        else:
            # In this case, the patcher can't determine which module the connection instance is from.
            # We default to it to check ssl_context but may be None so that the default scheme would be
            # (and may falsely be) http.
            ssl_cxt = getattr(instance, 'ssl_context', None)
        scheme = 'https' if ssl_cxt and type(ssl_cxt).__name__ == 'SSLContext' else 'http'
        xray_url = '{}://{}{}'.format(scheme, instance.host, url)
        xray_data = _XRay_Data(method, instance.host, xray_url)
        setattr(instance, _XRAY_PROP, xray_data)

        # we add a segment here in case connect fails
        return xray_recorder.record_subsegment(
            wrapped, instance, args, kwargs,
            name=get_hostname(xray_data.url),
            namespace='remote',
            meta_processor=http_send_request_processor
        )

    return decompose_args(*args, **kwargs) 
Example #3
Source File: base.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def status(self):
        ret = dict(last_getinfo=self.last_getinfo,
                   connections=[])
        for connection in self._conn.pool.queue:
            if connection is None:
                continue
            ret['connections'].append(connection.status)
        return ret 
Example #4
Source File: base.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self):
        self._down_connections = []  # list of RPC conns that are down
        self._poll_connection = None  # our currently active RPC connection
        self._live_connections = []  # list of live RPC connections
        self._connected = Event()  # An event type status flag 
Example #5
Source File: base.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _monitor_nodes(self):
        remlist = []
        for conn in self._down_connections:
            try:
                conn.getinfo()
            except (urllib3.exceptions.HTTPError, CoinRPCException, ValueError):
                self.logger.warn("RPC connection {} still down!".format(conn.name))
                continue

            self._live_connections.append(conn)
            remlist.append(conn)
            self.logger.info("Now connected to {} RPC Server {}."
                             .format(self.config['currency'], conn.name))

            # if this connection has a higher priority than current
            if self._poll_connection is not None:
                curr_poll = self._poll_connection.config['poll_priority']
                if conn.config['poll_priority'] > curr_poll:
                    self.logger.info("RPC connection {} has higher poll priority than "
                                     "current poll connection, switching..."
                                     .format(conn.name))
                    self._poll_connection = conn
            else:
                self._connected.set()
                self._poll_connection = conn
                self.logger.info("RPC connection {} defaulting poll connection"
                                 .format(conn.name))

        for conn in remlist:
            self._down_connections.remove(conn) 
Example #6
Source File: base.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def down_connection(self, conn):
        """ Called when a connection goes down. Removes if from the list of
        live connections and recomputes a new. """
        if not conn:
            self.logger.warn("Tried to down a NoneType connection")
            return

        if conn in self._live_connections:
            self._live_connections.remove(conn)

        if self._poll_connection is conn:
            # find the next best poll connection
            try:
                self._poll_connection = min(self._live_connections,
                                            key=lambda x: x.config['poll_priority'])
            except ValueError:
                self._poll_connection = None
                self._connected.clear()
                self.logger.error("No RPC connections available for polling!!!")
            else:
                self.logger.warn("RPC connection {} switching to poll_connection "
                                 "after {} went down!"
                                 .format(self._poll_connection.name, conn.name))

        if conn not in self._down_connections:
            self.logger.info("Server at {} now reporting down".format(conn.name))
            self._down_connections.append(conn) 
Example #7
Source File: requests_wrapper.py    From py-ipfs-http-client with MIT License 4 votes vote down vote up
def create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                      source_address=None, socket_options=None,
                      family=socket.AF_UNSPEC):
	host, port = address
	if host.startswith('['):
		host = host.strip('[]')
	err = None

	if not family or family == socket.AF_UNSPEC:
		family = urllib3.util.connection.allowed_gai_family()

	for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
		af, socktype, proto, canonname, sa = res
		sock = None
		try:
			sock = socket.socket(af, socktype, proto)

			# If provided, set socket level options before connecting.
			if socket_options is not None:
				for opt in socket_options:
					sock.setsockopt(*opt)

			if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
				sock.settimeout(timeout)
			if source_address:
				sock.bind(source_address)
			sock.connect(sa)
			return sock
		except OSError as e:
			err = e
			if sock is not None:
				sock.close()
				sock = None

	if err is not None:
		raise err

	raise OSError("getaddrinfo returns an empty list")


# Override the `urllib3` low-level Connection objects that do the actual work
# of speaking HTTP