Page 1 of 1

Determine socket type

Posted: Wed Aug 07, 2019 4:53 am
by manseekingknowledge
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?

Re: Determine socket type

Posted: Sat Aug 10, 2019 5:32 am
by jimmo
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.

Re: Determine socket type

Posted: Sun Aug 11, 2019 2:36 am
by manseekingknowledge
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.

Re: Determine socket type

Posted: Sun Aug 11, 2019 6:48 am
by pythoncoder
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.