socket.sendto error:

  • greetings,

    when I run the following code:

    ''' udp socket client Silver Moon '''
    import socket   #  for sockets
    import sys #  for exit
    # create dgram udp socket
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # except socket.error:
    except OSError as err:
        print('Failed to create socket')
    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]))

    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?


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

  • Global Moderator

    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


Log in to reply

Pycom on Twitter