473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Determine socket family at runtime

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
3 2214
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tor Erik Sønvisen | last post by:
Hi How can I determine the type of a socket (TCP or UDP) object? regards tores
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
4
by: Ted | last post by:
Hi all, I am trying to learn C socket programming and I have a small program which is a UP client. The problem is, when I run the program, I get a runtime error - "Invalid Argument" - from a...
10
by: groups.20.thebriguy | last post by:
socket objects have a little quirk. If you try to receive 0 bytes on a blocking socket, they block. That is, if I call recv(0), it blocks (until some data arrives). I think that's wrong, but I...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
1
by: brunoyalgi | last post by:
In socket programming in C++ WIN32 / UNIX / LINUX , we use AF_INET for Address family and PF_NET for protocol family. socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) serv_addr.sin_family = AF_INET; ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.