gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / requests / packages / urllib3 / util / connection.py @ 564
History | View | Annotate | Download (3.3 KB)
1 |
from __future__ import absolute_import |
---|---|
2 |
import socket |
3 |
try:
|
4 |
from select import poll, POLLIN |
5 |
except ImportError: # `poll` doesn't exist on OSX and other platforms |
6 |
poll = False
|
7 |
try:
|
8 |
from select import select |
9 |
except ImportError: # `select` doesn't exist on AppEngine. |
10 |
select = False
|
11 |
|
12 |
|
13 |
def is_connection_dropped(conn): # Platform-specific |
14 |
"""
|
15 |
Returns True if the connection is dropped and should be closed.
|
16 |
|
17 |
:param conn:
|
18 |
:class:`httplib.HTTPConnection` object.
|
19 |
|
20 |
Note: For platforms like AppEngine, this will always return ``False`` to
|
21 |
let the platform handle connection recycling transparently for us.
|
22 |
"""
|
23 |
sock = getattr(conn, 'sock', False) |
24 |
if sock is False: # Platform-specific: AppEngine |
25 |
return False |
26 |
if sock is None: # Connection already closed (such as by httplib). |
27 |
return True |
28 |
|
29 |
if not poll: |
30 |
if not select: # Platform-specific: AppEngine |
31 |
return False |
32 |
|
33 |
try:
|
34 |
return select([sock], [], [], 0.0)[0] |
35 |
except socket.error:
|
36 |
return True |
37 |
|
38 |
# This version is better on platforms that support it.
|
39 |
p = poll() |
40 |
p.register(sock, POLLIN) |
41 |
for (fno, ev) in p.poll(0.0): |
42 |
if fno == sock.fileno():
|
43 |
# Either data is buffered (bad), or the connection is dropped.
|
44 |
return True |
45 |
|
46 |
|
47 |
# This function is copied from socket.py in the Python 2.7 standard
|
48 |
# library test suite. Added to its signature is only `socket_options`.
|
49 |
def create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
50 |
source_address=None, socket_options=None): |
51 |
"""Connect to *address* and return the socket object.
|
52 |
|
53 |
Convenience function. Connect to *address* (a 2-tuple ``(host,
|
54 |
port)``) and return the socket object. Passing the optional
|
55 |
*timeout* parameter will set the timeout on the socket instance
|
56 |
before attempting to connect. If no *timeout* is supplied, the
|
57 |
global default timeout setting returned by :func:`getdefaulttimeout`
|
58 |
is used. If *source_address* is set it must be a tuple of (host, port)
|
59 |
for the socket to bind as a source address before making the connection.
|
60 |
An host of '' or port 0 tells the OS to use the default.
|
61 |
"""
|
62 |
|
63 |
host, port = address |
64 |
if host.startswith('['): |
65 |
host = host.strip('[]')
|
66 |
err = None
|
67 |
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): |
68 |
af, socktype, proto, canonname, sa = res |
69 |
sock = None
|
70 |
try:
|
71 |
sock = socket.socket(af, socktype, proto) |
72 |
|
73 |
# If provided, set socket level options before connecting.
|
74 |
# This is the only addition urllib3 makes to this function.
|
75 |
_set_socket_options(sock, socket_options) |
76 |
|
77 |
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT: |
78 |
sock.settimeout(timeout) |
79 |
if source_address:
|
80 |
sock.bind(source_address) |
81 |
sock.connect(sa) |
82 |
return sock
|
83 |
|
84 |
except socket.error as e: |
85 |
err = e |
86 |
if sock is not None: |
87 |
sock.close() |
88 |
sock = None
|
89 |
|
90 |
if err is not None: |
91 |
raise err
|
92 |
|
93 |
raise socket.error("getaddrinfo returns an empty list") |
94 |
|
95 |
|
96 |
def _set_socket_options(sock, options): |
97 |
if options is None: |
98 |
return
|
99 |
|
100 |
for opt in options: |
101 |
sock.setsockopt(*opt) |