Am I missing something here? ipaddress vs socket

Ronald van Zantvoort the.loeki at gmail.com
Fri May 1 06:41:43 EDT 2015


Hi Chris & Alain, thanks for  responding :)

On Friday, 1 May 2015 12:11:20 UTC+2, Chris Angelico  wrote:
> On Fri, May 1, 2015 at 7:42 PM,  Ronald van Zantvoort wrote:
> > Hi all,
> >
> > Given the following code:
> >
> > import ipaddress
> > import socket
> >
> > ip = ipaddress.ip_address(mystring)
> > sock_family = ip.????
> > socket = socket.socket(sock_family, socket.SOCK_STREAM)
> >
> > Am I crazy or is this undoable?
> >
> > sock.AF_INET == 2
> > sock.AF_INET6 == 10
> > ip.version == 4 or 6
> 
> Are you trying to look up a name to get an address? Or just look up an
> address? The easiest way would be to use a ternary if:
> 
> sock_family = sock.AF_INET if ip.version == 4 else sock.AF_INET6

mystring is already an IP address, sorry for not mentioning that.
The point is that ternary if; I've been scouring around the internet and there's a million different variations on the same idea. 
I'm completely flabberghasted it's not a simple property within the object though, e.g. ip.sock_family.

Was this a deliberate decision by the Python devs or can I try an RFE for that?

> But you may find it convenient to use a dedicated function for
> establishing a connection, which could look up an AAAA or A record for
> a name, then proceed through all addresses, attempting connections in
> turn. I'm fairly sure one exists in Python, but I can't right now
> remember the name.
> 
> ChrisA

Well you've already got socket.create_connection(), which (I think) does perform that lookup.



More information about the Python-list mailing list