[Python] Python: Sockets not working over LAN

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I recently came back from vacation, and my basic python 2 socket server is now unable to communicate with clients over LAN. The server is on a mac, and the client is my raspberry pi or my windows 7 machine. I have simplified the server and client code here to give an example:

    Server

    import socket
    from thread import *

    HOST = socket.gethostname()

    print HOST

    PORT = input ("Enter the PORT number (1 - 10,000)")



    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    print "Socket Created"


    s.bind((HOST, PORT))

    print "Socket Bind Complete"

    s.listen(10)
    print "Socket now listening"


    #Sending message to connected client
    #This only takes strings (words


    while True:
    #Wait to accept a connection - blocking call
    connection, addr = s.accept()
    print "Connection Established!"

    connection.send("Welcome to the server. Type something and hit enter\n")

    #loop so that function does not terminate and the thread does not end
    while True:

    #Receiving from client
    data = connection.recv(1024)
    if not data:
    break
    connection.sendall(data)
    print data
    connection.close()
    s.close()


    Client

    import socket #for sockets
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "Socket Created"

    #Get host and port info to connect
    host = raw_input("HOST >>> ")
    port = 2468
    s.connect((host, port))


    while True:
    #Send some data to the remote server
    message = raw_input(">>> ")

    #set the whole string
    s.sendall(message)


    reply = s.recv(1024)
    print reply


    Question

    What is going on here? I am getting the local IP, but the scripts are still not able to communicate. Could it be an issue with the OS's?


    MORE INFO


    1. Pinging

      a. I was able to ping the PI from my Mac terminal:

      PING raspberrypi (67.63.55.3): 56 data bytes
      64 bytes from 67.63.55.3: icmp_seq=0 ttl=240 time=17.434 ms
      64 bytes from 67.63.55.3: icmp_seq=1 ttl=240 time=18.180 ms
      64 bytes from 67.63.55.3: icmp_seq=2 ttl=240 time=22.046 ms
      64 bytes from 67.63.55.3: icmp_seq=3 ttl=240 time=25.124 ms
      64 bytes from 67.63.55.3: icmp_seq=4 ttl=240 time=31.773 ms


      b. My PI was not able to find the Mac as a host. I will see what I can do to remedy this.

      c. My PC was able to PING my mac. My Mac was able to ping my PC


    2. Firewall

    My Mac's Firewall is off. I will check on the [Raspberry Pi Stackexchange Site] to see if the PI has a firewall.

    I will add more info once I test my windows machine

    Continue reading...

Compartilhe esta Página