|
59 | 59 | from errno import EOPNOTSUPP, EINVAL, EAGAIN
|
60 | 60 | from io import BytesIO
|
61 | 61 | from os import SEEK_CUR
|
| 62 | +import os |
62 | 63 | from collections import Callable
|
63 | 64 | from base64 import b64encode
|
64 | 65 |
|
| 66 | + |
| 67 | +if os.name == 'nt': |
| 68 | + try: |
| 69 | + import win_inet_pton |
| 70 | + import socket |
| 71 | + except ImportError: |
| 72 | + raise ImportError('To run PySocks under windows you need to install win_inet_pton') |
| 73 | +else: |
| 74 | + import socket |
| 75 | + |
| 76 | + |
65 | 77 | PROXY_TYPE_SOCKS4 = SOCKS4 = 1
|
66 | 78 | PROXY_TYPE_SOCKS5 = SOCKS5 = 2
|
67 | 79 | PROXY_TYPE_HTTP = HTTP = 3
|
@@ -179,29 +191,29 @@ def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
|
179 | 191 | try:
|
180 | 192 | sock = socksocket(family, socket_type, proto)
|
181 | 193 |
|
182 |
| - if socket_options is not None: |
| 194 | + if socket_options: |
183 | 195 | for opt in socket_options:
|
184 | 196 | sock.setsockopt(*opt)
|
185 | 197 |
|
186 | 198 | if isinstance(timeout, (int, float)):
|
187 | 199 | sock.settimeout(timeout)
|
188 | 200 |
|
189 |
| - if proxy_type is not None: |
| 201 | + if proxy_type: |
190 | 202 | sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns,
|
191 | 203 | proxy_username, proxy_password)
|
192 |
| - if source_address is not None: |
| 204 | + if source_address: |
193 | 205 | sock.bind(source_address)
|
194 | 206 |
|
195 | 207 | sock.connect((remote_host, remote_port))
|
196 | 208 | return sock
|
197 | 209 |
|
198 |
| - except socket.error as e: |
| 210 | + except (socket.error, ProxyConnectionError) as e: |
199 | 211 | err = e
|
200 |
| - if sock is not None: |
| 212 | + if sock: |
201 | 213 | sock.close()
|
202 | 214 | sock = None
|
203 | 215 |
|
204 |
| - if err is not None: |
| 216 | + if err: |
205 | 217 | raise err
|
206 | 218 |
|
207 | 219 | raise socket.error("gai returned empty list.")
|
|
0 commit comments