Python HTTP proxy server

from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)

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

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

 

[jacob@tux]$ python proxy.py
/usr/lib/python2.3/site-packages/twisted/web/proxy.py:22: DeprecationWarning: twisted.protocols.http has moved to twisted.web.http. See http://twistedmatrix.com/projects/web.
  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 url_opener.open(url).headers 
    else: 
        print "Usage:", sys.argv[0], "proxy user pass fetchurls..." 

Python Proxy Source 2

import urllib2 
url = 'www.example.com' 
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) 
urllib2.install_opener(opener) 
print urllib2.urlopen("http://www.example.com/folder/page.html") 

Python Proxy Source

import urllib2, urllib 

proxy = urllib2.ProxyHandler({'http': 'http:// 
username:password@proxyurl:proxyport'}) 
auth = urllib2.HTTPBasicAuthHandler() 
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler) 
urllib2.install_opener(opener) 

conn = urllib2.urlopen('http://python.org') 
return_str = conn.read() 
1 154 155 156