473,703 Members | 4,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine socket family at runtime

Hi there,
since the socket.socket.f amily 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.get sockname()[0:2]
af = socket.getaddri nfo(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 2233
2008/5/4, Giampaolo Rodola' <gn****@gmail.c om>:
For now I've been able to determine the family by using:

# self.socket = a connected socket.socket instance
ip, port = self.socket.get sockname()[0:2]
af = socket.getaddri nfo(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.f amily 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.get sockname()[0:2]
af = socket.getaddri nfo(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(s ocket.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...@virgil io.itwrote:
On Sun, 04 May 2008 08:49:55 -0700, Giampaolo Rodola' wrote:
Hi there,
since the socket.socket.f amily 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.get sockname()[0:2]
af = socket.getaddri nfo(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(s ocket.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.a ccept().
--- 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
1364
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
4228
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 it seems to stay in the application scope, but the paranoid side of me thinks I may be missing something... any confirmation would be helpful.
4
4839
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 call to sendto. I was hoping that if someone has the time they could take a look at my code posted below and let me know what i'm doing wrong?
10
5614
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 don't want to argue that. I would like to create a subclass of socket that fixes the problem. Ideally, something like: class new_socket(socket): def recv( self, bufsize, flags=0 ):
7
16938
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 write an helper method that will tell if the socket is connected or not , but it's not working good continue to tell me that the socket is connectedeven if the other party already call shutdown(both) + close , or , even if the other party close the...
3
4302
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3163
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
1
45021
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; socket( PF_INET, SOCK_STREAM, IPPROTO_IP ) serv_addr.sin_family = PF_INET; I would like to know where we need to use AF_NET & PF_NET respectively?
0
8761
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9262
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9018
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2463
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.