Using Python’s urllib2 or Requests with a SOCKS5 proxy

Just a Python 2.7 code sample showing how to make requests using Python’s urllib2 or Requests through a SOCKS proxy.

We’ll be using PySocks:

pip install pysocks
import socket
import socks # you need to install pysocks (see above)

# Configuration
SOCKS5_PROXY_HOST = '1.2.3.4'
SOCKS5_PROXY_PORT = 1234

# Remove this if you don't plan to "deactivate" the proxy later
default_socket = socket.socket

# Set up a proxy
socks.set_default_proxy(socks.SOCKS5, SOCKS5_PROXY_HOST, SOCKS5_PROXY_PORT)
socket.socket = socks.socksocket

# - If you use urllib2:
import urllib2
print urllib2.urlopen('http://icanhazip.com/', timeout=24).read() # outputs proxy IP

# - If you use requests (pip install requests):
import requests
print requests.get('http://icanhazip.com/', timeout=24).text # outputs proxy IP

# Use this only if you plan to make requests without any proxies later
socket.socket = default_socket

# Make a request normally without a proxy; this will output your own public IP
print urllib2.urlopen('http://icanhazip.com/', timeout=24).read()

Please note that 90% of free proxies you’ll find in the Interwebs are barely or not working at all. Don’t test your code with a free proxy, unless you know for sure that the proxy itself works fine.

This entry was posted in Programming and tagged . Bookmark the permalink.

3 Responses to Using Python’s urllib2 or Requests with a SOCKS5 proxy

  1. Basanth says:

    Thanks. Worked for me. Before seeing your solution, I was trying to use the requesocks module; but somehow I could never get that to work. But using socket and socks like your suggestion worked

  2. David Sterry says:

    I have a script that uses requests to get and post data but also serves up a flask app. I’d like the requests to go over Tor but the flask app needs to be served locally, not over Tor. Can you see a way to enable this use case?

  3. Pingback: Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *