0

Trying to build a simple server:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            self.send_response(200)
            self.send_header('Content-type',    'text/html')
            self.end_headers()

            self.wfile.write("It works!")

            return

        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)

try:
    server = HTTPServer(('localhost', 8080), MyHandler)
    print 'started httpserver...'
    server.serve_forever()
except KeyboardInterrupt:
    print '^C received, shutting down server'
    server.socket.close()

Well, it works on 127.0.0.1:8080, but now I want to access it through public network like my ip:8080, what should I do?

Edit: HTTPServer(('0.0.0.0', 8080), MyHandler) does not work for me, any idea why?

I'm on win 7 ultimate 64bit, python 2.7.3

falsetru
  • 357,413
  • 63
  • 732
  • 636
Shane
  • 4,875
  • 12
  • 49
  • 87

1 Answers1

1

Specify '0.0.0.0' or '' (empty string) to make the server accept connections from anywhere.

server = HTTPServer(('0.0.0.0', 8080), MyHandler)

The address ('0.0.0.0', 8080) is passed to socket.bind. 0.0.0.0 is used to bind to any local network interfaces.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @Shane, Are you inside NAT? If so, you need to configure your router to do port forwarding. – falsetru Dec 30 '14 at 16:07
  • @Shane, What do you get if you issue the following command in command line: `ipconfig | findstr IPv4` – falsetru Dec 30 '14 at 16:17
  • This does seem to be the problem! I'm behind a router but do not have admin rights, is there any other way to bypass it? – Shane Dec 30 '14 at 16:26
  • @Shane, If you have a machine (under your control) with public IP, you can do tunneling. – falsetru Dec 30 '14 at 16:33
  • @Shane, BTW, you should be able to access your machine using internal IP (The IP you can get using `ipconfig ...`) – falsetru Dec 30 '14 at 16:33
  • Thanks, actually I do have a machine with public IP(behind LAN) fully under my control, just looking for a simple solution to communicate with other machine, would you please explain more on this? – Shane Dec 31 '14 at 01:54
  • @Shane, Please search for `ssh tunneling` (if the machine have ssh installed), or just `tunneling` otherwise. – falsetru Dec 31 '14 at 01:55
  • Thanks! But is there a way to build a simple server/client app fully through python? Actually I need the app on my machine to be able to receive some commands from another machine and do something – Shane Dec 31 '14 at 02:03
  • @Shane, Ah, if you have a machien with public IP, how about running the python script on the machine? If you don't want to run the script on public-ip machine, tunneling is only option as far as I know. You may can do tunneling in Python, but it will not be easier than using existing tools. – falsetru Dec 31 '14 at 02:15
  • yeah I do want to run simple python scripts on both machines(behind LAN) to communicate with each other, how do you do that? – Shane Dec 31 '14 at 02:36