0

I am trying to follow this example and translating it to python3 in order to create a proxy server to be able to access some http webpages through it.

I'm running my script in an AWS EC2 where I have opened port 80.

I run the code below, go to firefox and type X.X.X.X/www.aena.es (Xs are the public ip of my EC2 and aena is a spanish http webpage).

I can get my browser to show the html code of the web page I am asking for. But not the web page itself. Is there an easy way to do this?

os.system('sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination ' + local_ip + ':8888')
os.system('sudo iptables -A FORWARD -p tcp --dport 80 -j ACCEPT')

PORT = 8888

class Proxy(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        full_path = 'http:/' + self.path
        print(full_path)
        req = urllib.request.urlopen(full_path)
        self.copyfile(req, self.wfile)

httpd = socketserver.ForkingTCPServer((local_ip, PORT), Proxy)
print("serving at port ", PORT)
httpd.serve_forever()
edumugi
  • 31
  • 6
  • *"it says"* ...the browser says this, or the program output? You should probably log `self.path` as it may or may not contain what you assume, and it would be helpful to see if the code actually gets to that point. Well-engineered sites will see what you are doing and prevent you from doing it, but "the connection was reset" does not seem like an especially likely outcome. – Michael - sqlbot Feb 19 '19 at 17:10
  • The browser said this. But that was a problem with the network of my working place.. I am going to edit my post to point to the actual problem. I did what you said. I printed self.path and it was working fine. What I have realised is that I can get my browser to show the html code of the web page I am asking for. But not the web page itself. I am quite new with this and I guess is not as easy as I thought. Probably I would need to serve the webpage myself with nginx or something similar. Am I right? – edumugi Feb 20 '19 at 09:05

0 Answers0