Python HTTP proxy server

from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys

class ProxyFactory(http.HTTPFactory):
    protocol = proxy.Proxy

reactor.listenTCP(8080, ProxyFactory())
  • Execute the above Twisted python example script (python You may see a Deprecation Warning that you can ignore for this example.


[jacob@tux]$ python
/usr/lib/python2.3/site-packages/twisted/web/ DeprecationWarning: twisted.protocols.http has moved to twisted.web.http. See
  from twisted.protocols import http
2005/12/06 21:47 EST [-] Log opened.
2005/12/06 21:47 EST [-] __main__.ProxyFactory starting on 8080
2005/12/06 21:47 EST [-] Starting factory <__main__.ProxyFactory instance at 0xb73577ac>
  • Connect your internet browser to the proxy server you just created (localhost:8080) and you have written a simple proxy server using Python and Twisted.

Complicated proxy with urrllib2

For more complicated programs

import urllib2 
def get_proxy_opener(proxyurl, proxyuser, proxypass, proxyscheme="http"): 
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() 
    password_mgr.add_password(None, proxyurl, proxyuser, proxypass) 
    proxy_handler = urllib2.ProxyHandler({proxyscheme: proxyurl}) 
    proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_mgr) 
    return urllib2.build_opener(proxy_handler, proxy_auth_handler) 
if __name__ == "__main__": 
    import sys 
    if len(sys.argv) > 4: 
        url_opener = get_proxy_opener(*sys.argv[1:4]) 
        for url in sys.argv[4:]: 
        print "Usage:", sys.argv[0], "proxy user pass fetchurls..." 

Python Proxy Source 2

import urllib2 
url = '' 
username = 'user' 
password = 'pass' 
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() 
# None, with the "WithDefaultRealm" password manager means 
# that the user/pass will be used for any realm (where 
# there isn't a more specific match). 
password_mgr.add_password(None, url, username, password) 
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) 
opener = urllib2.build_opener(auth_handler) 
print urllib2.urlopen("") 

Python Proxy Source

import urllib2, urllib 

proxy = urllib2.ProxyHandler({'http': 'http:// 
auth = urllib2.HTTPBasicAuthHandler() 
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler) 

conn = urllib2.urlopen('') 
return_str = 
1 154 155 156