[Python] Python socket can not connect on two separated machines

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I used socket library in python , and made two simple projects which are server.py and client.py , then I executed them to server.exe and client.exe When i run both (server.exe and client.exe) on my computer , it works fine , but when i run them on two separated computer , client can not connect to the server which run on another computer !! Here is my codes :

    Server side :

    #server codes
    import socket

    #make socket for server
    server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    #use our ipv4 for making server on port 9000 , and show it on the screen
    ip = socket.gethostbyname(socket.gethostname())
    server.bind((ip,9000))
    server.listen(1)
    print(f"The server ipv4 , give it to client to connect to the server -> {ip}")

    #wait for client
    print("Waiting for client ...")
    client_socket = server.accept()[0]

    #send hello to client
    client_socket.send(b"hello")

    #close server and client
    client_socket.close()
    server.close()
    print("Done !!")


    Client side :

    #client codes
    import socket

    #get server computer ipv4 from client
    server_ip = input("Please enter server ipv4 here : ")

    #make client socket
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.connect((server_ip,9000))

    #receive data from server (b"hello" which sent from the server)
    Print(client.recv(5).decode('utf-8'))

    #close client
    client.close()
    print("Done !!")


    There is no more exception in these two codes but just one exception appear on client side when I am trying to connect client to the server which run on another computer as I said above

    The exception which appear on client side say that client socket attempted to connect to the server , but it could not make connection to the server !! and it said about timeout connection . I searched alot and watch lots of videos which make the same projects for testing socket in python and it works for them , but not for me :( I allowed client and server in firewall on two computers , also disabled firewall , but it did not work at all , and the same exception appeared. I used netstat -a (cmd command) on computer which server was running on it , and saw that the server socket was listening on port 9000.

    Nothing seems to work , any help will be appreciated (sorry for my English)

    Continue reading...

Compartilhe esta Página