By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

Determine socket family at runtime

P: n/a
Hi there,
since the socket.socket.family attribute has been introduced only in
Python 2.5 and I need to have my application to be backward compatible
with Python 2.3 and 2.4 I'd like to know how could I determine the
family of a socket.socket instance which may be AF_INET or AF_INET6.
Is there some kind of getsockopt() directive I could use?
For now I've been able to determine the family by using:

# self.socket = a connected socket.socket instance
ip, port = self.socket.getsockname()[0:2]
af = socket.getaddrinfo(ip, port)[0][0]

....but I'd like to know if some other solution is preferable.

Thanks.
--- Giampaolo
http://code.google.com/p/pyftpdlib
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
2008/5/4, Giampaolo Rodola' <gn****@gmail.com>:
For now I've been able to determine the family by using:

# self.socket = a connected socket.socket instance
ip, port = self.socket.getsockname()[0:2]
af = socket.getaddrinfo(ip, port)[0][0]

...but I'd like to know if some other solution is preferable.
Nope, there is none. Using getaddrinfo() to check address family
is the de facto standard.

--
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
Jun 27 '08 #2

P: n/a
On Sun, 04 May 2008 08:49:55 -0700, Giampaolo Rodola' wrote:
Hi there,
since the socket.socket.family attribute has been introduced only in
Python 2.5 and I need to have my application to be backward compatible
with Python 2.3 and 2.4 I'd like to know how could I determine the
family of a socket.socket instance which may be AF_INET or AF_INET6.
Is there some kind of getsockopt() directive I could use?
For now I've been able to determine the family by using:

# self.socket = a connected socket.socket instance
ip, port = self.socket.getsockname()[0:2]
af = socket.getaddrinfo(ip, port)[0][0]

...but I'd like to know if some other solution is preferable.

Thanks.
--- Giampaolo
http://code.google.com/p/pyftpdlib
Ciao,

what about wrapping the socket type and adding a 'family' attribute
to the base socket class? Something like:

class SocketWrapper(socket.socket): def __init__(self, family, type, proto=0): socket.socket.__init__(self, family, type, proto) self.family = family

then you have just to create the sockets with SocketWrapper insetead of
socket.socket. For the rest of your code it would not matter, and then you
are sure to always have a .family attribute.

Ciao
----
FB
Jun 27 '08 #3

P: n/a
On 4 Mag, 19:18, Francesco Bochicchio <bock...@virgilio.itwrote:
On Sun, 04 May 2008 08:49:55 -0700, Giampaolo Rodola' wrote:
Hi there,
since the socket.socket.family attribute has been introduced only in
Python 2.5 and I need to have my application to be backward compatible
with Python 2.3 and 2.4 I'd like to know how could I determine the
family of a socket.socket instance which may be AF_INET or AF_INET6.
Is there some kind of getsockopt() directive I could use?
For now I've been able to determine the family by using:
# self.socket = a connected socket.socket instance
ip, port = self.socket.getsockname()[0:2]
af = socket.getaddrinfo(ip, port)[0][0]
...but I'd like to know if some other solution is preferable.
Thanks.
--- Giampaolo
http://code.google.com/p/pyftpdlib

Ciao,

what about wrapping the socket type and *adding a 'family' attribute
to the base socket class? Something like:

class SocketWrapper(socket.socket): * *def __init__(self, family, type, proto=0): * * * * * * socket.socket.__init__(self, family, type, proto) * * * * * * *self.family = family

then you have just to create the sockets with SocketWrapper insetead of
socket.socket. For the rest of your code it would not matter, and then you
are sure to always have a .family attribute.

Ciao
----
FB- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
Ciao,
I don't think that it would be possible since the socket I'm talking
about is returned by socket.socket.accept().
--- Giampaolo
http://code.google.com/p/pyftpdlib
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.