Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/urllib3/util/connection.py
811 views
1
from __future__ import absolute_import
2
import socket
3
from .wait import NoWayToWaitForSocketError, wait_for_read
4
from ..contrib import _appengine_environ
5
6
7
def is_connection_dropped(conn): # Platform-specific
8
"""
9
Returns True if the connection is dropped and should be closed.
10
11
:param conn:
12
:class:`httplib.HTTPConnection` object.
13
14
Note: For platforms like AppEngine, this will always return ``False`` to
15
let the platform handle connection recycling transparently for us.
16
"""
17
sock = getattr(conn, "sock", False)
18
if sock is False: # Platform-specific: AppEngine
19
return False
20
if sock is None: # Connection already closed (such as by httplib).
21
return True
22
try:
23
# Returns True if readable, which here means it's been dropped
24
return wait_for_read(sock, timeout=0.0)
25
except NoWayToWaitForSocketError: # Platform-specific: AppEngine
26
return False
27
28
29
# This function is copied from socket.py in the Python 2.7 standard
30
# library test suite. Added to its signature is only `socket_options`.
31
# One additional modification is that we avoid binding to IPv6 servers
32
# discovered in DNS if the system doesn't have IPv6 functionality.
33
def create_connection(
34
address,
35
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
36
source_address=None,
37
socket_options=None,
38
):
39
"""Connect to *address* and return the socket object.
40
41
Convenience function. Connect to *address* (a 2-tuple ``(host,
42
port)``) and return the socket object. Passing the optional
43
*timeout* parameter will set the timeout on the socket instance
44
before attempting to connect. If no *timeout* is supplied, the
45
global default timeout setting returned by :func:`getdefaulttimeout`
46
is used. If *source_address* is set it must be a tuple of (host, port)
47
for the socket to bind as a source address before making the connection.
48
An host of '' or port 0 tells the OS to use the default.
49
"""
50
51
host, port = address
52
if host.startswith("["):
53
host = host.strip("[]")
54
err = None
55
56
# Using the value from allowed_gai_family() in the context of getaddrinfo lets
57
# us select whether to work with IPv4 DNS records, IPv6 records, or both.
58
# The original create_connection function always returns all records.
59
family = allowed_gai_family()
60
61
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
62
af, socktype, proto, canonname, sa = res
63
sock = None
64
try:
65
sock = socket.socket(af, socktype, proto)
66
67
# If provided, set socket level options before connecting.
68
_set_socket_options(sock, socket_options)
69
70
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
71
sock.settimeout(timeout)
72
if source_address:
73
sock.bind(source_address)
74
sock.connect(sa)
75
return sock
76
77
except socket.error as e:
78
err = e
79
if sock is not None:
80
sock.close()
81
sock = None
82
83
if err is not None:
84
raise err
85
86
raise socket.error("getaddrinfo returns an empty list")
87
88
89
def _set_socket_options(sock, options):
90
if options is None:
91
return
92
93
for opt in options:
94
sock.setsockopt(*opt)
95
96
97
def allowed_gai_family():
98
"""This function is designed to work in the context of
99
getaddrinfo, where family=socket.AF_UNSPEC is the default and
100
will perform a DNS search for both IPv6 and IPv4 records."""
101
102
family = socket.AF_INET
103
if HAS_IPV6:
104
family = socket.AF_UNSPEC
105
return family
106
107
108
def _has_ipv6(host):
109
""" Returns True if the system can bind an IPv6 address. """
110
sock = None
111
has_ipv6 = False
112
113
# App Engine doesn't support IPV6 sockets and actually has a quota on the
114
# number of sockets that can be used, so just early out here instead of
115
# creating a socket needlessly.
116
# See https://github.com/urllib3/urllib3/issues/1446
117
if _appengine_environ.is_appengine_sandbox():
118
return False
119
120
if socket.has_ipv6:
121
# has_ipv6 returns true if cPython was compiled with IPv6 support.
122
# It does not tell us if the system has IPv6 support enabled. To
123
# determine that we must bind to an IPv6 address.
124
# https://github.com/urllib3/urllib3/pull/611
125
# https://bugs.python.org/issue658327
126
try:
127
sock = socket.socket(socket.AF_INET6)
128
sock.bind((host, 0))
129
has_ipv6 = True
130
except Exception:
131
pass
132
133
if sock:
134
sock.close()
135
return has_ipv6
136
137
138
HAS_IPV6 = _has_ipv6("::1")
139
140