0

Using this code:

from http import server
class Serv(server.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
server.HTTPServer(('host', 80), Serv).serve_forever()

I have tried using my public IP, which didn't work, my private IP, which only worked from the same network, and localhost, which is my PC only. How can I change the host so when someone connects to my IP, it connects to my website (from my code)? Do I need my router to redirect to my PC? I know host can be my private IP or localhost, are there any other hosts I can use? Edit: I saw an answer in another question that used Flask, I'd like to not use any dependencies as of now.

  • 2
    get it working on same network first by using empty string for default ip. then use router's port forwarding. Some ISPs will intentionally block certain external ports unless you pay them more so try another port to your local port 80. – Abel Sep 27 '21 at 13:15
  • 1
    This is a question about network infrastructure and configuration, not programming. – chepner Sep 27 '21 at 13:25

1 Answers1

0

If you want to make it work from anywhere,

your application should listen on IP address 0.0.0.0

which means (in short) any IPv4 address at all :

from http import server

class Serv(server.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b'Hello, world!')
server.HTTPServer(('', 80), Serv).serve_forever()

If you are in a recent linux/GNU based OS, you may check listening ports with :

ss -ltpn

Note that after that modification, you will depend of any proxy/firewall between you and your server to get your application response.

Etienne Dijon
  • 1,093
  • 5
  • 10
  • Could be that I'm using a public internet right now, but it isn't working when I connect to my public IP using my browser. – MLG Herobrine Sep 27 '21 at 13:46
  • @MLGHerobrine, I've updated my answer, with your code, an empty response will be given for your GET request. just modified it so it prints `Hello world`. It is a working example. If you tried this at home. it is possible you need to configure `NAT` on your `router`. – Etienne Dijon Sep 27 '21 at 13:54
  • Thanks! It worked for a while, and I just remembered this question and made the answer accepted. Edit: this doesn't make it connect between networks, but I now know enough to know I need to use port forwarding – MLG Herobrine Oct 21 '21 at 12:10