usocket UDP and sockaddr questions
Re: usocket UDP and sockaddr questions
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/
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/
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: usocket UDP and sockaddr questions
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]
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: usocket UDP and sockaddr questions
This seems to tell otherwise: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.
https://github.com/micropython/micropyt ... /re.py#L68
Re: usocket UDP and sockaddr questions
Well, that's a hack, don't do it like that .This seems to tell otherwise:
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/
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/
Re: usocket UDP and sockaddr questions
And as it shows, inet_ntop() alone doesn't help. Stuff like sockaddr[4:8] is inherently not portable.So, I eventually figured out inet_ntop:
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/
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/
Re: usocket UDP and sockaddr questions
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/
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/
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: usocket UDP and sockaddr questions
A Python bytes object is immutable, a bytearray is mutable. Clear enough to this Python coder
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.