Python Sockets Timeout Error 10060 Over the Internet

Viewed 52

I have been following this tutorial to send messages between a Windows (8.1) PC and a Windows VPS: https://www.techwithtim.net/tutorials/socket-programming/

According to the tutorial to make the scripts work over the internet all that is needed is to change the line below to the public IP of the server (which I am running on the PC).

SERVER = socket.gethostbyname(socket.gethostname())

Everything works ok when I run the scripts on my local network but when I try to use them remotely I get this error:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

There isn't much out there on how to solve it but I have tried disabling the firewalls on the PC and VPS. This didn't help.

I then tried changing the IP address in the server script to 0.0.0.0 as some have suggested but this hasn't helped either.

Can anyone see the issue?

Here's the server script:

import socket
import threading

HEADER = 64
PORT = 5050
SERVER = "0.0.0.0" #socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"

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


def handle_client(conn, addr):
    print(f"[NEW CONNECTION] {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(FORMAT)
            if msg == DISCONNECT_MESSAGE:
                connected = False

            print(f"[{addr}] {msg}")
            conn.send("Msg received".encode(FORMAT))

    conn.close()


def start():
    server.listen()
    print(f"[LISTENING] Server is listening on {SERVER}")
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")


print("[STARTING] server is starting...")
start()

And here's the client script:

import socket

HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = "xxxxxxxxxxxx"
ADDR = (SERVER, PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)

def send(msg):
    message = msg.encode(FORMAT)
    msg_length = len(message)
    send_length = str(msg_length).encode(FORMAT)
    send_length += b' ' * (HEADER - len(send_length))
    client.send(send_length)
    client.send(message)
    print(client.recv(2048).decode(FORMAT))

send("Hello World!")
input()
send("Hello Everyone!")
input()
send("Hello Tim!")

send(DISCONNECT_MESSAGE)
0 Answers
Related