Skip to content

Commit

Permalink
Fix HTTPConnection strict-related DeprecationWarning in Py3. Resolves #…
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Feb 21, 2014
1 parent 7d50b9f commit 181cf09
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
47 changes: 32 additions & 15 deletions urllib3/connection.py
Expand Up @@ -4,6 +4,7 @@
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import sys
import socket
from socket import timeout as SocketTimeout

Expand Down Expand Up @@ -38,6 +39,7 @@ class BaseSSLError(BaseException):
ConnectTimeoutError,
)
from .packages.ssl_match_hostname import match_hostname
from .packages import six
from .util import (
assert_fingerprint,
resolve_cert_reqs,
Expand All @@ -53,27 +55,40 @@ class BaseSSLError(BaseException):


class HTTPConnection(_HTTPConnection, object):
"""
Based on httplib.HTTPConnection but provides an extra constructor
backwards-compatibility layer between older and newer Pythons.
"""

default_port = port_by_scheme['http']

# By default, disable Nagle's Algorithm.
tcp_nodelay = 1

def __init__(self, *args, **kw):
if six.PY3: # Python 3
kw.pop('strict', None)

if sys.version_info < (2, 7): # Python 2.6 and earlier
kw.pop('source_address', None)
self.source_address = None

_HTTPConnection.__init__(self, *args, **kw)

def _new_conn(self):
""" Establish a socket connection and set nodelay settings on it
:return: a new socket connection
"""
try:
conn = socket.create_connection(
(self.host, self.port),
self.timeout,
self.source_address,
)
except AttributeError: # Python 2.6
conn = socket.create_connection(
(self.host, self.port),
self.timeout,
)
extra_args = []
if self.source_address: # Python 2.7+
extra_args.append(self.source_address)

conn = socket.create_connection(
(self.host, self.port),
self.timeout,
*extra_args
)
conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,
self.tcp_nodelay)
return conn
Expand All @@ -95,10 +110,12 @@ class HTTPSConnection(HTTPConnection):
def __init__(self, host, port=None, key_file=None, cert_file=None,
strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None):
try:
HTTPConnection.__init__(self, host, port, strict, timeout, source_address)
except TypeError: # Python 2.6
HTTPConnection.__init__(self, host, port, strict, timeout)

HTTPConnection.__init__(self, host, port,
strict=strict,
timeout=timeout,
source_address=source_address)

self.key_file = key_file
self.cert_file = cert_file

Expand Down
6 changes: 1 addition & 5 deletions urllib3/connectionpool.py
Expand Up @@ -170,13 +170,9 @@ def _new_conn(self):
log.info("Starting new HTTP connection (%d): %s" %
(self.num_connections, self.host))

extra_params = {}
if not six.PY3: # Python 2
extra_params['strict'] = self.strict

conn = self.ConnectionCls(host=self.host, port=self.port,
timeout=self.timeout.connect_timeout,
**extra_params)
strict=self.strict)
if self.proxy is not None:
# Enable Nagle's algorithm for proxies, to avoid packet
# fragmentation.
Expand Down

0 comments on commit 181cf09

Please sign in to comment.