8000 Progress: New branch holding some of the changes from @foxx adding so… · github3py/urllib3@8289989 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8289989

Browse files
committed
Progress: New branch holding some of the changes from @foxx adding socks-proxy support. (urllib3#68)
1 parent 38fe6f0 commit 8289989

File tree

6 files changed

+599
-0
lines changed

6 files changed

+599
-0
lines changed

CONTRIBUTORS.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ In chronological order:
3939
* brandon-rhodes <http://rhodesmill.org/brandon>
4040
* Design review, bugfixes, test coverage.
4141

42+
* Cal Leeming <cal.leeming@simplicitymedialtd.co.uk>
43+
* SOCKS4/5 proxy support
44+
4245
* [Your name or handle] <[email or website]>
4346
* [Brief summary of your changes]

test/test_proxy.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "Cal Leeming"
4+
__maintainer__ = "Cal Leeming"
5+
__credits__ = ["Cal Leeming", ]
6+
__email__ = "cal.leeming@simplicitymedialtd.co.uk"
7+
8+
9+
def fix_import_path():
10+
import os
11+
import sys
12+
CURRENT_DIR = os.path.realpath(os.path.dirname(__file__))
13+
IMPORT_PATH = "%s/../" % ( CURRENT_DIR, )
14+
sys.path.append(IMPORT_PATH)
15+
16+
fix_import_path()
17+
import urllib3
18+
import sys
19+
import os
20+
21+
proxys = [
22+
"http://192.168.56.1:8888",
23+
"socks4://192.168.56.1:8889",
24+
"socks5://192.168.56.1:8889"
25+
]
26+
for x in proxys:
27+
print "TEST Proxy: %s" % ( x, )
28+
pool = urllib3.PoolManager(proxy_url = x)
29+
req = pool.request('GET', 'https://www.google.co.uk/')
30+
assert req.data.count("google_favicon_128.png"), "string detect failed"
31+
print "HTTPS OK"
32+
33+
req = pool.request('GET', 'http://www.google.co.uk/')
34+
assert req.data.count("google_favicon_128.png"), "string detect failed"
35+
print "HTTP OK"
36+
37+
print "---"
38+
39+
print "TEST: No proxy via PoolManager"
40+
pool = urllib3.PoolManager()
41+
req = pool.request('GET', 'https://www.google.co.uk/')
42+
assert req.data.count("google_favicon_128.png"), "string detect failed"
43+
print "HTTPS OK"
44+
45+
req = pool.request('GET', 'http://www.google.co.uk/')
46+
assert req.data.count("google_favicon_128.png"), "string detect failed"
47+
print "HTTP OK"
48+
49+
print "---"
50+
51+
print "TEST: No proxy via connection_from_url"
52+
_rq = urllib3.connection_from_url('https://www.google.co.uk/')
53+
req = _rq.request("GET", "/")
54+
assert req.data.count("google_favicon_128.png"), "string detect failed"
55+
print "HTTPS OK"
56+
57+
_rq = urllib3.connection_from_url('http://www.google.co.uk/')
58+
req = _rq.request("GET", "/")
59+
assert req.data.count("google_favicon_128.png"), "string detect failed"
60+
print "HTTP OK"

urllib3/connection.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# urllib3/connection.py
2+
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
3+
#
4+
# This module is part of urllib3 and is released under
5+
# the MIT License: http://www.opensource.org/licenses/mit-license.php
6+
7+
8+
# XXX: This needs module to be rewritten and utilized from connectionpool.py
9+
10+
11+
def create_proxy_socket(self):
12+
if self.proxy_url:
13+
# Extract the proxy components
14+
proxy_scheme, proxy_host, proxy_port = get_host(self.proxy_url)
15+
16+
# Detect the proxy scheme
17+
if proxy_scheme == 'socks4':
18+
self.sock = socksipy.socks.create_connection(
19+
(self.host, self.port),
20+
self.timeout,
21+
proxy_type = socksipy.socks.PROXY_TYPE_SOCKS4,
22+
proxy_host = proxy_host,
23+
proxy_port = proxy_port
24+
)
25+
26+
elif proxy_scheme == 'socks5':
27+
self.sock = socksipy.socks.create_connection(
28+
(self.host, self.port),
29+
self.timeout,
30+
proxy_type = socksipy.socks.PROXY_TYPE_SOCKS5,
31+
proxy_host = proxy_host,
32+
proxy_port = proxy_port
33+
)
34+
35+
elif proxy_scheme == 'http':
36+
self.sock = socket.create_connection(
37+
(proxy_host, proxy_port),
38+
self.timeout
39+
)
40+
41+
# Trigger the native proxy support in httplib/urllib2
42+
self._tunnel_host = self.host
43+
self._tunnel_port = self.port
44+
self._tunnel()
45+
46+
else:
47+
raise AssertionError("bad proxy scheme: %r" % ( proxy_scheme, ) )
48+
49+
# Wrapped HTTPConnection object
50+
class HTTPConnection(_HTTPConnection):
51+
_tunnel_host = None
52+
53+
# Bit hacky, but it works
54+
create_proxy_socket = create_proxy_socket
55+
56+
def connect(self):
57+
if self.proxy_url:
58+
self.create_proxy_socket()
59+
60+
else:
61+
self.sock = socket.create_connection(
62+
(self.host,self.port),
63+
self.timeout
64+
)
65+
66+
## Connection objects (extension of httplib)
67+
class HTTPSConnection(_HTTPSConnection):
68+
# Bit hacky, but it works
69+
create_proxy_socket = create_proxy_socket:

urllib3/packages/socksipy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import socks
2+
import socksipyhandler

0 commit comments

Comments
 (0)
0