How to make python Requests work via SOCKS proxy
The modern way:
pip install -U 'requests[socks]'
then
import requests
resp = requests.get('http://go.to',
proxies=dict(http='socks5://user:pass@host:port',
https='socks5://user:pass@host:port'))
Can't send requests through socks5 proxy with Python
Create a local server/mock to handle the request using pytest or some other testing framework with responses
library to eliminate variables external to your application/script. I’m quite sure Google will reject requests with empty headers. Also, ensure you installed the correct dependencies to enable SOCKS proxy support in requests (python -m pip install requests[socks]
). Furthermore, if you are making a remote request to connect to your proxy you must change socks5
to socks5h
in your proxies
dictionary.
References
pytest: https://docs.pytest.org/en/6.2.x/
responses: https://github.com/getsentry/responses
requests[socks]: https://docs.python-requests.org/en/master/user/advanced/#socks
Python SOCKS5 proxy client HTTPS
As of requests version 2.10.0, released on 2016-04-29, requests
supports SOCKS.It requires PySocks, which can be installed with pip install pysocks.
import requests
host_url = 'https://example.com'
#Fill in your own proxies' details
proxies={http:'socks5://user:pass@host:port',
https:'socks5://user:pass@host:port'}
#define headers if you will
headers={}
response = requests.get(host_url, headers=headers, proxies=proxies)
Beware, when using a SOCKS proxy, request socks will make HTTP requests with the full URL (e.g., GET example.com HTTP/1.1
rather than GET / HTTP/1.1
) and this behavior may cause problems.
requests via a SOCKs proxy
SOCKS support for requests is still pending. If you want, you can view my Github repository here to see my branch of the Socksipy library. This is the branch that is currently being integrated into requests; it will be some time before requests fully supports it, though.
https://github.com/Anorov/PySocks/
It should work okay with urllib2. Import sockshandler
in your file, and follow the example inside of it. You'll want to create an opener like this:
opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "localhost", 9050))
Then you can use opener.open(url)
and it should tunnel through the proxy.
Python3 - Requests with Sock5 proxy
You can use socks, socket
modules
import socks
import socket
from urllib import request
socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
r = request.urlopen('http://icanhazip.com')
print(r.read()) # check ips
The socks
package can be installed from multiple packages which are forks of socksipy. One particular one that also works on Python3 is PySocks. You can install it, for example, with pip:
pip3 install PySocks
How do I use python Requests with a Putty SOCKS Proxy on Windows?
Requests does not support SOCKS proxies at this time. We're hoping to get support in the underlying urllib3
library, but there's no explicit time-frame on that. It's tracked by this issue.
Edit in 2016: As of Requests 2.10, Requests now has support for SOCKS proxies. You can get the support by installing the socks
extra: pip install requests[socks]
.
Cant seem to get https and socks proxies to work using python requests
Requests
simply does not yet support either SOCKS or HTTPS proxies.
They're working in it, though. See here: https://github.com/kennethreitz/requests/pull/1515
Support for HTTPS proxies has already been merged into the requests 2.0
branch, so if you like you can try that version; be wary though, as it it is currently an unstable branch.
SOCKS proxy support, on the other hand, is still being worked on in the lower-level library, urllib3
: https://github.com/shazow/urllib3/pull/68
Also, regardless of that, you are using the proxies
argument incorrectly. It should be of the form {protocol_of_sites_you_visit: proxy}
, so once support is complete, using a SOCKS5 proxy would actually be more along the lines of {"http": "socks5://127.0.0.1:9050"}
.
Related Topics
Stop/Start/Pause in Python Matplotlib Animation
Python How to Write to a Binary File
Executing Multiple Statements with Postgresql via SQLalchemy Does Not Persist Changes
How to Get Around Declaring an Unused Variable in a for Loop
Group by Pandas Dataframe and Select Latest in Each Group
Zlib.Error: Error -3 While Decompressing: Incorrect Header Check
What Is the Reason for Having '//' in Python
What Is the Advantage of a List Comprehension Over a for Loop
How to Get a Raw, Compiled SQL Query from a SQLalchemy Expression
Python Urllib2 Basic Auth Problem
Which Is Faster in Python: X**.5 or Math.Sqrt(X)
Opencv Error: (-215)Size.Width>0 && Size.Height>0 in Function Imshow
Executing Command Using Paramiko Exec_Command on Device Is Not Working
Writing to MySQL Database with Pandas Using SQLalchemy, To_Sql