Determine type of a socket

T

Tor Erik Sønvisen

Hi

How can I determine the type of a socket (TCP or UDP) object?

regards tores
 
P

Peter Hansen

Tor said:
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:
['__class__', .... '_sock', 'accept', 'bind', 'close'
, 'connect', 'connect_ex', 'dup', 'fileno', 'getpeername',
....'setblocking', 'setsockopt', 'settimeout', 'shutdown']

Let's see... what looks good here?
<socket object, fd=1912, family=2, type=1, protocol=0>

Maybe that type field?

-Peter
 
R

Reinhold Birkenfeld

Peter said:
Tor said:
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:
['__class__', .... '_sock', 'accept', 'bind', 'close'
, 'connect', 'connect_ex', 'dup', 'fileno', 'getpeername',
...'setblocking', 'setsockopt', 'settimeout', 'shutdown']

Let's see... what looks good here?
<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
 
P

Peter Hansen

Reinhold said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,006
Latest member
TerranceCo

Latest Threads

Top