Python server only runs on local wifi

Code:

import socket, threading

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind(("my ipv4 from ipconfig", 12))

server.listen(5)

def client_handler(client_socket):
    request = client_socket.recv(100)

    print "[*] Received: " + request

    client_socket.close()

while True:
    client, addr = server.accept()
    print "[*] Accepted connection from: %s:%d" % (addr[0], addr[1])
    servert = threading.Thread(target=client_handler, args=(client,))
    servert.start()

      

So the server is working fine, but if I ask my friend on another network to connect, it doesn't connect. I tried port forwarding from router http://i.stack.imgur.com/wSUir.png (cant post img calls my_reputation <10 explicitly)

I also tried using ip i to get from whatismyip site, but I get the error:

error: [Errno 10049] The requested address is invalid in its context

Any ideas on what I can do to get other people to connect? Thank.

+3


source to share


1 answer


When trying to access your server from outside your local network, you must use 0.0.0.0 as your IP address when creating your socket object. 0.0.0.0 usually means the default route (route to the "rest" of the Internet, except routes on your local network, etc.). If you are using a DCHP allocated IP (in your case, a router), the devices connected to the network (router) only know that your IP address is what you get in the $ ifconfig command, a private IP -address that the client does not know.



+1


source







All Articles