|
42 | 42 | __docformat__ = 'restructuredtext en'
|
43 | 43 |
|
44 | 44 |
|
45 |
| -if sys.version < '2.6': |
46 |
| - |
47 |
| - class TimeoutMixin: |
48 |
| - """Helper mixin to add timeout before socket connection""" |
49 |
| - |
50 |
| - # taken from original python2.5 httplib source code with timeout setting added |
51 |
| - def connect(self): |
52 |
| - """Connect to the host and port specified in __init__.""" |
53 |
| - msg = "getaddrinfo returns an empty list" |
54 |
| - for res in socket.getaddrinfo(self.host, self.port, 0, |
55 |
| - socket.SOCK_STREAM): |
56 |
| - af, socktype, proto, canonname, sa = res |
57 |
| - try: |
58 |
| - self.sock = socket.socket(af, socktype, proto) |
59 |
| - if self.debuglevel > 0: |
60 |
| - print("connect: (%s, %s)" % (self.host, self.port)) |
61 |
| - |
62 |
| - # setting socket timeout |
63 |
| - self.sock.settimeout(self.timeout) |
64 |
| - |
65 |
| - self.sock.connect(sa) |
66 |
| - except socket.error as msg: |
67 |
| - if self.debuglevel > 0: |
68 |
| - print('connect fail:', self.host, self.port) |
69 |
| - if self.sock: |
70 |
| - self.sock.close() |
71 |
| - self.sock = None |
72 |
| - continue |
73 |
| - break |
74 |
| - if not self.sock: |
75 |
| - raise socket.error(msg) |
76 |
| - |
77 |
| - _HTTPConnection = HTTPConnection |
78 |
| - _HTTPSConnection = HTTPSConnection |
79 |
| - |
80 |
| - class HTTPConnection(TimeoutMixin, _HTTPConnection): |
81 |
| - def __init__(self, *a, **k): |
82 |
| - timeout = k.pop('timeout', None) |
83 |
| - _HTTPConnection.__init__(self, *a, **k) |
84 |
| - self.timeout = timeout |
85 |
| - |
86 |
| - class HTTPSConnection(TimeoutMixin, _HTTPSConnection): |
87 |
| - def __init__(self, *a, **k): |
88 |
| - timeout = k.pop('timeout', None) |
89 |
| - _HTTPSConnection.__init__(self, *a, **k) |
90 |
| - self.timeout = timeout |
91 |
| - |
92 |
| - |
93 | 45 | if sys.version < '2.7':
|
94 | 46 |
|
95 | 47 | from httplib import CannotSendHeader, _CS_REQ_STARTED, _CS_REQ_SENT
|
|
0 commit comments