Determine type of a socket
Reinhold Birkenfeld
reinhold-birkenfeld-nospam at wolke7.net
Mon Sep 26 13:09:07 EDT 2005
Peter Hansen wrote:
> Tor Erik Sønvisen wrote:
>> How can I determine the type of a socket (TCP or UDP) object?
>
> In what context? Do you have some code that gets passed a socket object
> but it could have been created with either SOCK_STREAM or SOCK_DGRAM?
> And you want a way of determining by looking just at the object passed
> in which type it is? (I could make other guesses, but let's start with
> that... ;-) )
>
> How about this:
>
> >>> import socket
> >>> t = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> >>> u = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> >>> dir(t)
> ['__class__', .... '_sock', 'accept', 'bind', 'close'
> , 'connect', 'connect_ex', 'dup', 'fileno', 'getpeername',
> ...'setblocking', 'setsockopt', 'settimeout', 'shutdown']
>
> 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.
Reinhold
More information about the Python-list
mailing list