0

I'm trying to connect 2 different computers that on different networks, but I got an error:

TimeoutError: [Errno 110] Connection timed out

And sometimes I got this error:

OSError: [Errno 113] No route to host

I wrote the server.py script and started it on the first pc and client.py on the second one.

server.py

import socket
server_socket = socket.socket()
server_socket.bind(("127.0.0.1", 80))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
print ("client_connected [" + client_address[0] + "]")
client_command = client_socket.recv(1024).decode()
print(client_command.encode())

client.py

import socket
client_socket = socket.socket()
client_socket.connect(("server_public_ip_here", 8820))
client_command = input("command: ")
client_socket.send(client_command.encode())
data = client_socket.recv(1024).decode()
print("server: " + data)

I expect to get a connections between the computers but keep getting a TimeoutError or OSError

2
  • It looks like you are listening on port 80 but trying to connect to port 8820. Commented Apr 19, 2019 at 13:07
  • Also, you're binding to 127.0.0.1 and connecting to a different IP. Commented Apr 19, 2019 at 13:08

1 Answer 1

1

if you are on two separate computers you should bind to 0.0.0.0 (to listen to all network connections) or to the server's IP for connections on that IP, and not 127.0.0.1 (localhost). Works if client and server are both in the same machine. Also you should use the same port on client and server.

I test it and it worked:

server.py

import socket
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", 8000))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
print ("client_connected [" + client_address[0] + "]")
client_command = client_socket.recv(1024).decode()
print(client_command.encode())

client.py

import socket
client_socket = socket.socket()
client_socket.connect(("127.0.0.1", 8000)) #or enter ip of server
client_command = input("command: ")
client_socket.send(client_command.encode())
data = client_socket.recv(1024).decode()
print("server: " + data)
Sign up to request clarification or add additional context in comments.

1 Comment

A few notes: 1. Avoid using 8000 as a port or any that start with 80 for that matter, many other server applications use them. 2. Make sure you have set your firewall policies correctly for TCP traffic on the port you are using for both machines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.