I know there are already a lot of topics on this problem, but I still couldn't find any good answer to it so here I am.
I'm using Python3 to communicate between a host and a server. Everything worked just fine between two local machines, and I decided to put the server side on a VPS. Since then, i get this error everytime I try to connect to it :
ConnectionRefusedError: [Winerror 10061] No connection could be made because the target machine actively refused it
I disabled the vps firewall, changed the port, the connexion target and everything. I tried to nmap the port and i get this result :
Here is my client code :
import socket
HEADER = 64
PORT = 40000
FORMAT = 'utf-8'
DECONNEXION = "!DECONNEXION"
SERVER = "vps-xxxxxxxx.vps.ovh.net"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def envoyer(msg):
message = msg.encode(FORMAT)
message_longueur = str(len(message)).encode(FORMAT)
message_longueur += b' '*(HEADER-len(message_longueur))
client.send(message_longueur)
client.send(message)
print(client.recv(2048).decode(FORMAT))
def communication():
while (True):
envoyer(input())
communication()
Server :
#!/usr/bin/python3
import socket
import threading
HEADER = 64
PORT = 40000
SERVEUR = socket.getfqdn(socket.gethostname())
ADDR = (SERVEUR, PORT)
FORMAT = 'utf-8'
DECONNEXION = "!DECONNEXION"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print("[NOUVELLE CONNEXION] :", addr)
message=""
connecte = True
while connecte:
longueur_message = conn.recv(HEADER).decode(FORMAT)
if(longueur_message):
longueur_message = int (longueur_message)
message = conn.recv(longueur_message).decode(FORMAT)
print("[", addr, "] : ", message)
conn.send("Message reçu !".encode(FORMAT))
if "!DECONNEXION" in message:
connecte = False
conn.close()
def start():
server.listen()
print("[STATUT] Serveur démarré sur", SERVEUR,":", PORT )
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print("[CONNEXIONS] ", threading.active_count() -1 )
print("[STATUT] Le serveur démarre... ")
start()
But as I said, this code worked locally. Can it be that OVH has a funky firewall of his own that blocks tcp port 40000? Thanks in advance
With
SERVEUR = socket.getfqdn(socket.gethostname())
ADDR = (SERVEUR, PORT)
server.bind(ADDR)
you're telling Python to bind the listening socket to only the interface with the SERVEUR
local address, which might not be correct at all.
Instead, as mentioned in the comments, common options are
'0'
(short for '0.0.0.0'
) to bind to all network interfaces (which is useful to expose a service to the Internet)'127.0.0.1'
to bind to only the loopback network interface (which is useful in many proxying situations)Of course, there are cases where you want to bind to a certain interface only, but the two above are the common cases.