Creating a Proxy Webserver in Python

A proxy server sits in between the client and the actual server. It receives the requests from the client, send it to the actual server, and on receiving the response from the actual server it sends the response back to the client. There are many reasons to use the proxy like hiding the server’s IP address, performance improvement or increasing security etc. In this article we will see how we can create a simple proxy server using python.

The three modules SimpleWebSocketServer,, SimpleHTTPSServer and urllib can be used to achieve this. Below we see how we create python class using the methods available in this module and pass the instance of that class to SimpleWebSocketServer. Then we get the server up and running by using the server forever method available with the class.


import SimpleWebSocketServer
import SimpleHTTPSServer
import urllib
PORT = 9012
class JustAProxy(SimpleHTTPSServer.SimpleWebSocketServer):
   def do_GET(self):
      self.copyfile(urllib.urlopen(url), self.wfile)
httpd = SimpleWebSocketServer.SimpleWebSocketServer('localhost',PORT,JustAProxy)
print ("Proxy Srever at" , str(PORT))


Running the above code gives us the following result −

Proxy Srever at 9012