aboutsummaryrefslogtreecommitdiff
path: root/urllib3/util/connection.py
diff options
context:
space:
mode:
authorSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 13:19:37 -0700
committerSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 13:19:37 -0700
commit35fb123b995cbbe27d3edd5ed14abc6e56b7ad13 (patch)
treea8d061c933f9e65601e6290c10abd090b79fae26 /urllib3/util/connection.py
parent5f949ee35667a6065ab02a3e7ab8c98c9fcdcaed (diff)
downloadpython-urllib3-35fb123b995cbbe27d3edd5ed14abc6e56b7ad13.tar
python-urllib3-35fb123b995cbbe27d3edd5ed14abc6e56b7ad13.tar.gz
Imported Upstream version 1.8.2
Diffstat (limited to 'urllib3/util/connection.py')
-rw-r--r--urllib3/util/connection.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/urllib3/util/connection.py b/urllib3/util/connection.py
new file mode 100644
index 0000000..8deeab5
--- /dev/null
+++ b/urllib3/util/connection.py
@@ -0,0 +1,45 @@
+from socket import error as SocketError
+try:
+ from select import poll, POLLIN
+except ImportError: # `poll` doesn't exist on OSX and other platforms
+ poll = False
+ try:
+ from select import select
+ except ImportError: # `select` doesn't exist on AppEngine.
+ select = False
+
+def is_connection_dropped(conn): # Platform-specific
+ """
+ Returns True if the connection is dropped and should be closed.
+
+ :param conn:
+ :class:`httplib.HTTPConnection` object.
+
+ Note: For platforms like AppEngine, this will always return ``False`` to
+ let the platform handle connection recycling transparently for us.
+ """
+ sock = getattr(conn, 'sock', False)
+ if sock is False: # Platform-specific: AppEngine
+ return False
+ if sock is None: # Connection already closed (such as by httplib).
+ return False
+
+ if not poll:
+ if not select: # Platform-specific: AppEngine
+ return False
+
+ try:
+ return select([sock], [], [], 0.0)[0]
+ except SocketError:
+ return True
+
+ # This version is better on platforms that support it.
+ p = poll()
+ p.register(sock, POLLIN)
+ for (fno, ev) in p.poll(0.0):
+ if fno == sock.fileno():
+ # Either data is buffered (bad), or the connection is dropped.
+ return True
+
+
+