import logging
import socket
-from select import poll, POLLIN
+from httplib import (HTTPConnection, HTTPSConnection, HTTPException,
+ HTTP_PORT, HTTPS_PORT)
+
+from Queue import Queue, Empty, Full
from socket import error as SocketError, timeout as SocketTimeout
+try:
+ from select import poll, POLLIN
+except ImportError: # Doesn't exist on OSX and other platforms
+ from select import select
+ poll = False
try: # Python 3
from http.client import HTTPConnection, HTTPSConnection, HTTPException
from http.client import HTTP_PORT, HTTPS_PORT
except ImportError:
- from http.client import HTTPConnection, HTTPSConnection, HTTPException
- from http.client import HTTP_PORT, HTTPS_PORT
+ from httplib import HTTPConnection, HTTPSConnection, HTTPException
+ from httplib import HTTP_PORT, HTTPS_PORT
try: # Python 3
from queue import Queue, Empty, Full
except ImportError:
- from queue import Queue, Empty, Full
+ from Queue import Queue, Empty, Full
try: # Compiled with SSL?
import ssl
BaseSSLError = None
+from .packages.ssl_match_hostname import match_hostname, CertificateError
from .request import RequestMethods
from .response import HTTPResponse
from .exceptions import (SSLError,
self.headers = headers or {}
# Fill the queue up so that doing get() on it will block properly
- for _ in range(maxsize):
+ for _ in xrange(maxsize):
self.pool.put(None)
# These are mostly for testing and debugging purposes.
:param conn:
``HTTPConnection`` object.
"""
- # poll-based replacement to select([conn.sock], [], [], 0.0)[0]:
+ if not poll:
+ return select([conn.sock], [], [], 0.0)[0]
+
+ # This version is better on platforms that support it.
p = poll()
p.register(conn.sock, POLLIN)
for (fno, ev) in p.poll(0.0):
try:
from http.client import HTTPSConnection
except ImportError:
- from http.client import HTTPSConnection
+ from httplib import HTTPSConnection
from logging import getLogger
from ntlm import ntlm
-
+from __future__ import absolute_import
from . import ssl_match_hostname
MAXSIZE = sys.maxsize
else:
- string_types = str,
- integer_types = (int, int)
- class_types = (type, type)
- text_type = str
+ string_types = basestring,
+ integer_types = (int, long)
+ class_types = (type, types.ClassType)
+ text_type = unicode
binary_type = str
# It's possible to have sizeof(long) != sizeof(Py_ssize_t).
return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
else:
def get_unbound_function(unbound):
- return unbound.__func__
+ return unbound.im_func
def advance_iterator(it):
- return next(it)
+ return it.next()
callable = callable
_add_doc(get_unbound_function,
def b(s):
return s
def u(s):
- return str(s, "unicode_escape")
+ return unicode(s, "unicode_escape")
int2byte = chr
- import io
- StringIO = BytesIO = io.StringIO
+ import StringIO
+ StringIO = BytesIO = StringIO.StringIO
_add_doc(b, """Byte literal""")
_add_doc(u, """Text literal""")
if fp is None:
return
def write(data):
- if not isinstance(data, str):
+ if not isinstance(data, basestring):
data = str(data)
fp.write(data)
want_unicode = False
sep = kwargs.pop("sep", None)
if sep is not None:
- if isinstance(sep, str):
+ if isinstance(sep, unicode):
want_unicode = True
elif not isinstance(sep, str):
raise TypeError("sep must be None or a string")
end = kwargs.pop("end", None)
if end is not None:
- if isinstance(end, str):
+ if isinstance(end, unicode):
want_unicode = True
elif not isinstance(end, str):
raise TypeError("end must be None or a string")
raise TypeError("invalid keyword arguments to print()")
if not want_unicode:
for arg in args:
- if isinstance(arg, str):
+ if isinstance(arg, unicode):
want_unicode = True
break
if want_unicode:
- newline = str("\n")
- space = str(" ")
+ newline = unicode("\n")
+ space = unicode(" ")
else:
newline = "\n"
space = " "
try:
from urllib.parse import urlencode
except ImportError:
- from urllib.parse import urlencode
+ from urllib import urlencode
from .filepost import encode_multipart_formdata
from io import BytesIO
-
from .exceptions import HTTPError
+try:
+ basestring = basestring
+except NameError: # Python 3
+ basestring = (str, bytes)
+
+
log = logging.getLogger(__name__)
self.strict = strict
self._decode_content = decode_content
- self._body = body if body and isinstance(body, str) else None
+ self._body = body if body and isinstance(body, basestring) else None
self._fp = None
self._original_response = original_response