Oded Answer
Oded Answer

Reputation: 13

Python- can't connect to my IP

I started learning about computer networks and I tried using sockets in Python. With a little help from a youtube video, I made a very simple Chatting program between a Server and a Client. When I tried to connect the client with 'localhost', it worked just the way I wanted. But when I tried using the IP address (that I found on findmyip.org, for example lets say 12.123.12.123), It just didn't show any sign of a connection. The Server is:

import socket
import sys

def socket_create():
    try:
        global host
        global port
        global s
        host = ''
        port = 9998
        s = socket.socket()
    except socket.error as msg:
        print "socket creation error bluhbluh"
    print "socket created"

def socket_bind():
    try:
        global host
        global port
        global s
        print "Binding socket to port"
        s.bind((host,port))
        s.listen(5)
    except socket.error as msk:
        print "socket binding error"
        socket_bind()


def socket_accept():
    conn,address = s.accept()
    print "connection has been established"
    print address[0]
    send_msg(conn)
    conn.close()

def send_msg(conn):
    while 1:
        mess=raw_input()
        if mess == "quit":
            conn.send(mess)
            conn.close()
            s.close()
            sys.exit()
        if len(mess)>0:
            conn.send(mess)
            resp=conn.recv(1024)
            print resp

def main():
    socket_create()
    socket_bind()
    socket_accept()

main()

The Client is:

import socket
import os
import sys

s = socket.socket()
host = '12.123.12.123' #replace with 'localhost' for a working version! :D 
port = 9998
s.connect((host,port))

while 1:
    data = s.recv(1024)
    print data
    if data=="quit":
        s.close()
    else:
        mess=raw_input()
        s.send(mess)

I Had this problem before, and from what I can remember, I didn't manage to fix it then aswell.. I tried port forwarding (at least I THINK I did it right) I hope you could help me Thank you in advance :)

Upvotes: 1

Views: 3074

Answers (1)

Wayne Foux
Wayne Foux

Reputation: 135

You issue is likely related to the network. The IP you posted is what is called a routable IP, the IP exposed to the internet. It normally gets assigned to the WAN side of your ISP provided modem or a router. Machines like your PC will normally live on the LAN side of the network device and will likely be assigned a non-routable IP, these cannot be seen from the internet.

When you go to a site that shows your IP it is showing your routable IP, not the one the network device assigned to your machine. To see that address you can type 'ipconfig' in a command window on Windows or 'ifconfig' in a terminal shell on linux and mac. You should see in that output a list of one or more interfaces which contain IP addresses. One will likely have the address of 127.0.0.1 which is equal to localhost and is known as your loopback address. You may see another one starting with 10. or 192. . That address is likely the one assigned to you by the network device. You can try that address and see if your program works.

Upvotes: 1

Related Questions