Requests: SOCKS proxy support

Created on 25 Jan 2015  路  8Comments  路  Source: psf/requests

I would like to request support for Socket Secure protocol. There exists a fork (https://github.com/dvska/requesocks) with this capability, but it is limited (python 2 only).

Currently the AssertionError: Not supported proxy scheme socks5 is thrown when attempting to connect to a socks5 proxy.

A quick Google search (https://www.google.nl/search?q=socks+proxy+list) illustrates the popularity of the protocol. Even Tor and I2P use it.

Most helpful comment

You need to use the socks5h scheme (stackoverflow):

https://github.com/requests/requests/blob/e3f89bf23c53b98593e4248054661472aacac820/requests/packages/urllib3/contrib/socks.py#L155-L160

Example:

import requests

proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}

response = requests.get('http://nzxj65x32vh2fkhk.onion/all', proxies=proxies)
print(response.text)

response = requests.get('http://zqktlwi4fecvo6ri.onion/', proxies=proxies)
print(response.text)

You'll notice that the first .onion link works while the second (your link) 403s. This is the most probably explanation:

All 8 comments

https://github.com/kennethreitz/requests/issues?q=is%3Aissue+socks+is%3Aclosed would have been a good search before opening this issue as well.

https://github.com/kennethreitz/requests/pull/478 was the only effort anyone made towards including this support in requests over 2 years ago. As of now, https://github.com/shazow/urllib3/pull/486 is still in progress. Until that is finished there is nothing we can do about supporting socks proxies.

If I do

#proxy
        # SOCKS5 proxy for HTTP/HTTPS
        proxies = {
            'http' : "socks5://myproxy:9191",
            'https' : "socks5://myproxy:9191"
        }

        #headers
        headers = {

        }

        url='http://icanhazip.com/'
        res = requests.get(url, headers=headers, proxies=proxies)

I get AssertionError: Not supported proxy scheme socks5

@loretoparisi make sure you have the latest version of requests and also install PySocks. here is an example of using Tor (via a socks5 proxy) with requests: https://github.com/AlJohri/python-tor-examples

How can i open .onion domains with this code?
Now i getting error:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='zqktlwi4fecvo6ri.onion', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd35ba09b10>: Failed to establish a new connection: [Errno -2] Name or service not known',))

You need to use the socks5h scheme (stackoverflow):

https://github.com/requests/requests/blob/e3f89bf23c53b98593e4248054661472aacac820/requests/packages/urllib3/contrib/socks.py#L155-L160

Example:

import requests

proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}

response = requests.get('http://nzxj65x32vh2fkhk.onion/all', proxies=proxies)
print(response.text)

response = requests.get('http://zqktlwi4fecvo6ri.onion/', proxies=proxies)
print(response.text)

You'll notice that the first .onion link works while the second (your link) 403s. This is the most probably explanation:

Wow, it's work, but on URL http://zqktlwi4fecvo6ri.onion (Hidden Wiki) i'm getting 403.
Thanks, next i'll try to understanding by myself

@loretoparisi make sure you have the latest version of requests and also install PySocks. here is an example of using Tor (via a socks5 proxy) with requests: https://github.com/AlJohri/python-tor-examples

Thanks ! Pysocks is what I didn't install, but there is another annoying exception, could you help me?

SOCKSHTTPSConnectionPool(host='www.pinterest.com', port=443):   
Max retries exceeded with url: /search/pins/q=poster%20food&rs=typed&term_meta[]=poster%7Ctyped&term_meta[]=food%7Ctyped
(Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

eromoe picture eromoe  路  3Comments

ReimarBauer picture ReimarBauer  路  4Comments

JimHokanson picture JimHokanson  路  3Comments

8key picture 8key  路  3Comments

iLaus picture iLaus  路  3Comments