usocket UDP and sockaddr questions

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: usocket UDP and sockaddr questions

Post by pfalcon » Fri Nov 20, 2015 4:06 pm

bytes is immutable object, so it's only suitable for read-only arguments, which won't be changed. bytearray is mutable, and is suitable for buffers which will be written to.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

SpotlightKid
Posts: 416
Joined: Wed Apr 08, 2015 5:19 am

Re: usocket UDP and sockaddr questions

Post by SpotlightKid » Fri Nov 20, 2015 4:15 pm

So, I eventually figured out inet_ntop:

Code: Select all

import socket
import struct

INET_ADDRSTRLEN = 16

inet_ntop = getattr(socket, 'inet_ntop', None)
if not inet_ntop:
    import ffilib
    _inet_ntop = ffilib.libc().func("s", "inet_ntop", "iPpi")

    def inet_ntop(a):
        buf = bytearray(INET_ADDRSTRLEN)
        res = _inet_ntop(socket.AF_INET, a, buf, INET_ADDRSTRLEN)
        return res

def get_hostport(sockaddr):
    return inet_ntop(sockaddr[4:8]), struct.unpack('>H', sockaddr[2:4])[0]

SpotlightKid
Posts: 416
Joined: Wed Apr 08, 2015 5:19 am

Re: usocket UDP and sockaddr questions

Post by SpotlightKid » Fri Nov 20, 2015 4:29 pm

pfalcon wrote:bytes is immutable object, so it's only suitable for read-only arguments, which won't be changed. bytearray is mutable, and is suitable for buffers which will be written to.
This seems to tell otherwise:

https://github.com/micropython/micropyt ... /re.py#L68

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: usocket UDP and sockaddr questions

Post by pfalcon » Fri Nov 20, 2015 7:13 pm

This seems to tell otherwise:
Well, that's a hack, don't do it like that ;-).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: usocket UDP and sockaddr questions

Post by pfalcon » Fri Nov 20, 2015 7:17 pm

So, I eventually figured out inet_ntop:
And as it shows, inet_ntop() alone doesn't help. Stuff like sockaddr[4:8] is inherently not portable.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: usocket UDP and sockaddr questions

Post by pfalcon » Sat Nov 21, 2015 1:06 am

I for a long time wanted to post my simple example for working with UPNP (UDP, multicast), now I finally pushed them: https://github.com/pfalcon/micropython- ... aster/upnp . They also demonstrate usage of usocket.sockaddr() function which was added as outcome of discussion in this thread.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
pythoncoder
Posts: 4242
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: usocket UDP and sockaddr questions

Post by pythoncoder » Wed Nov 25, 2015 6:39 am

A Python bytes object is immutable, a bytearray is mutable. Clear enough to this Python coder ;)
Peter Hinch

Post Reply