douglas rouse
douglas rouse

Reputation: 146

Can't connect to python server from another machine

I am trying to learn about socket programming / networking and have a basic server up and running. Unfortunately I can only connect to it from the machine running the server. I connect from command prompt using telnet localhost 9999. This does not work from a different machine on the same network (as in a different pc which is not running the server). I have tried telnet "my local ip" 9999 to no avail and can't find a solution anywhere. Any help is appreciated. I am running python 3.6.1

Here is my code

import socket
import sys
from _thread import *
import webbrowser

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

host = ""
port = 9999

try:
    s.bind((host,port))
except socket.error as e:
    print (str(e))

s.listen(5)

def threaded_client(conn):
    conn.send(str.encode("welcome, would you like to listen to a song? \n"))

while True:
    reply = ""
    data = conn.recv(2048)
    if data == b's':
        conn.send(str.encode("\n hi\n"))
        webbrowser.open('https://www.youtube.com/watch?v=DLzxrzFCyOs')
    if not data:
        break
    conn.sendall(str.encode(reply))
    print (data)
conn.close()

while True:
    conn, addr = s.accept()
    print("connected to:" +addr[0]+":"+str(addr[1]))
    start_new_thread(threaded_client,(conn,))

when I try to connect this is the message I recieve C:\Users\Douglas Rouse>telnet 127.0.0.1 9999 Connecting To 127.0.0.1...Could not open connection to the host, on port 9999: Connect failed

Upvotes: 0

Views: 6162

Answers (1)

yeputons
yeputons

Reputation: 9238

UPD:

I've just noticed that you try to connect using 127.0.0.1 as an IP address. That address always means "this computer", so you basically try to connect from a machine to itself, which fails, since the server is on a different machine.

You need to find out IP address of the server which is "visible" from the client machine. If they're in the same local network, I'd assume it's in one of the following common forms: 192.168.x.y, 10.0.0.x, 10.x.y.z.

Old answer:

If you use Windows, ensure that neither anti-virus software nor built-in firewall are interfering. Typically both of them block any non-standard communication.

The easiest way to check that is to switch both of them temporarily on both machines. If that helps, try switching them off on the server-side only (should also work). If that helps, add an exception for your application to anti-virus and firewall.

Upvotes: 3

Related Questions