Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    socket.sendto error:

    Scheduled Pinned Locked Moved Pymakr
    3 Posts 2 Posters 15 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I Offline
      Ibrahim Salim Omari
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • G Offline
        Gijs
        last edited by

        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

        I 1 Reply Last reply Reply Quote 0
        • I Offline
          Ibrahim Salim Omari @Gijs
          last edited by

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

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom