socket.sendto error:



  • greetings,

    when I run the following code:

    #!/usr/bin/python3
    
    ''' udp socket client Silver Moon '''
    
    import socket   #  for sockets
    import sys #  for exit
    
    # create dgram udp socket
    
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # except socket.error:
    except OSError as err:
        print('Failed to create socket')
        sys.exit()
    
    host = 'localhost'
    port = 8888
    
    while True:
        msg = input('Enter message to send : ')
    
        try :
            # Set the whole string and convert encode the string to bytes
            s.sendto(str.encode(msg), (host, port))
    
            # receive data from client (data, addr)
            d = s.recvfrom(1024)
            reply = d[0]
            addr = d[1]
    
            print('Server reply : ' + str(bytes.decode(reply)))  # convert reply to string
        except OSError as err:
        # except Exception as err:
            print('Error Code : ' + str(msg[0]) + ' Message ' + str(msg[1]))
            sys.exit()
    

    this line:

    s.sendto(str.encode(msg), (host, port))
    

    throws the following error:

    ValueError: invalid arguments

    the line works on a full python environment. But it doesn't in micropython.
    Any suggestions?

    iomari



  • @Gijs Thanks. It was the localhost. Working fine now with an ip address.



  • It took me some time, but the issue is with 'localhost', the micropython expects an IP address there. Other than that, it should work fine once you are connected to a network

    Gijs


Log in to reply
 

Pycom on Twitter