Connecting to a SOCKS Proxy within Python

The SocksiPy python module provides a simple way to connect via a SOCKS proxy. Within this article we will show you 2 examples on how to use SocksiPy.


Below allows you to establish a connection via a SOCKS proxy[1].

import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "", 8080)
socket.socket = socks.socksocket

import urllib2
print urllib2.urlopen('').read()


Should you want to use the SOCKS proxy only with urllib2 then the wrapmodule can be used. This replaces a module’s socket library with a SOCKS socket[2].

import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '', 8080)

import urllib2
print urllib2.urlopen('').read()



Rick Donato

Want to become a programming expert?

Here is our hand-picked selection of the best courses you can find online:
Python Zero to Hero course
Python Pro Bootcamp
Bash Scripting and Shell Programming course
Automate with Shell Scripting course
The Complete Web Development Bootcamp course
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial