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
-
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
-
@Gijs Thanks. It was the localhost. Working fine now with an ip address.
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