Determine socket type

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
manseekingknowledge
Posts: 40
Joined: Sun Oct 29, 2017 5:14 pm

Determine socket type

Post by manseekingknowledge » Wed Aug 07, 2019 4:53 am

In CPython the type of a socket (TCP stream or UDP datagram) can be determined with socket.type:

Code: Select all

import socket

if s.type is socket.SocketKind.SOCK_STREAM:
    # do stuff with TCP socket
elif s.type is socket.SocketKind.SOCK_DGRAM:
    # do stuff with UDP socket
Unfortunately it appears that the "type" attribute does not exist for usockets. Is there some other way I can obtain this information from an already created socket?

User avatar
jimmo
Posts: 409
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Determine socket type

Post by jimmo » Sat Aug 10, 2019 5:32 am

This is not currently implemented. I guess they left it out for space reasons because you'd assume that whatever created the socket either knew what type it was at creation, or it was accept'ed from a known socket type.

If you were interested in implementing this yourself, look at modlwip.c. The underlying lwip_socket_obj_t type has a `type` member. So it would be fairly straightforward to expose this to Python. But it may be easier just to keep track of this in your Python code.

manseekingknowledge
Posts: 40
Joined: Sun Oct 29, 2017 5:14 pm

Re: Determine socket type

Post by manseekingknowledge » Sun Aug 11, 2019 2:36 am

jimmo wrote:
Sat Aug 10, 2019 5:32 am
This is not currently implemented. I guess they left it out for space reasons because you'd assume that whatever created the socket either knew what type it was at creation, or it was accept'ed from a known socket type.

If you were interested in implementing this yourself, look at modlwip.c. The underlying lwip_socket_obj_t type has a `type` member. So it would be fairly straightforward to expose this to Python. But it may be easier just to keep track of this in your Python code.
Yeah, I had already taken the "easy" route. It is just a bit more messy in my code.

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

Re: Determine socket type

Post by pythoncoder » Sun Aug 11, 2019 6:48 am

manseekingknowledge wrote:
Sun Aug 11, 2019 2:36 am
...
Yeah, I had already taken the "easy" route. It is just a bit more messy in my code.
I guess you could subclass socket to create one with a type bound variable or property.
Peter Hinch

Post Reply