Determine type of a socket

Peter Hansen peter at engcorp.com
Mon Sep 26 13:59:09 EDT 2005


Reinhold Birkenfeld wrote:
> Peter Hansen wrote:
>>Tor Erik Sønvisen wrote:
>>>How can I determine the type of a socket (TCP or UDP) object?

>>Let's see... what looks good here?
>>
>> >>> u._sock
>><socket object, fd=1916, family=2, type=2, protocol=0>
>> >>> t._sock
>><socket object, fd=1912, family=2, type=1, protocol=0>
>>
>>Maybe that type field?
> 
> Heh. The type field isn't publicly accessible (yet). There is a
> patch on SF for this, in the meanwhile you'll have to parse the
> __repr__ output.

And he might not have to do either, depending on what his situation 
really is.  For example, if his socket is always going to be connected 
(in the case of a TCP socket) I expect checking something about the peer 
would be a quick way to tell the difference, since UDP sockets don't 
shouldn't have peers.  (I'm assuming, not having tested... but I think 
there are probably such approaches that don't involve either _sock or 
repr().  Let's see what the OP reveals.)

-Peter



More information about the Python-list mailing list