Determine type of a socket
Peter Hansen
peter at engcorp.com
Mon Sep 26 12:36:56 EDT 2005
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?
-Peter
More information about the Python-list
mailing list