Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89717 > unrolled thread

Am I missing something here? ipaddress vs socket

Started bythe.loeki@gmail.com
First post2015-05-01 02:42 -0700
Last post2015-05-01 12:31 +0200
Articles 8 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Am I missing something here? ipaddress vs socket the.loeki@gmail.com - 2015-05-01 02:42 -0700
    Re: Am I missing something here? ipaddress vs socket Chris Angelico <rosuav@gmail.com> - 2015-05-01 20:11 +1000
      Re: Am I missing something here? ipaddress vs socket Ronald van Zantvoort <the.loeki@gmail.com> - 2015-05-01 03:41 -0700
        Re: Am I missing something here? ipaddress vs socket Chris Angelico <rosuav@gmail.com> - 2015-05-01 22:06 +1000
          Re: Am I missing something here? ipaddress vs socket Ronald van Zantvoort <the.loeki@gmail.com> - 2015-05-01 06:37 -0700
            Re: Am I missing something here? ipaddress vs socket Chris Angelico <rosuav@gmail.com> - 2015-05-01 23:46 +1000
              Re: Am I missing something here? ipaddress vs socket Ronald van Zantvoort <the.loeki@gmail.com> - 2015-05-01 06:48 -0700
    Re: Am I missing something here? ipaddress vs socket Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2015-05-01 12:31 +0200

#89717 — Am I missing something here? ipaddress vs socket

Fromthe.loeki@gmail.com
Date2015-05-01 02:42 -0700
SubjectAm I missing something here? ipaddress vs socket
Message-ID<f46cdd6e-5c94-4370-a368-cce0bd68c997@googlegroups.com>
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

[toc] | [next] | [standalone]


#89718

FromChris Angelico <rosuav@gmail.com>
Date2015-05-01 20:11 +1000
Message-ID<mailman.6.1430475067.3347.python-list@python.org>
In reply to#89717
On Fri, May 1, 2015 at 7:42 PM,  <the.loeki@gmail.com> 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

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

[toc] | [prev] | [next] | [standalone]


#89721

FromRonald van Zantvoort <the.loeki@gmail.com>
Date2015-05-01 03:41 -0700
Message-ID<c8a5569c-66aa-4b96-829e-36307d6947cb@googlegroups.com>
In reply to#89718
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.

[toc] | [prev] | [next] | [standalone]


#89724

FromChris Angelico <rosuav@gmail.com>
Date2015-05-01 22:06 +1000
Message-ID<mailman.8.1430481980.3347.python-list@python.org>
In reply to#89721
On Fri, May 1, 2015 at 8:41 PM, Ronald van Zantvoort
<the.loeki@gmail.com> wrote:
> 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?

You could ask. At very worst, you'll find someone explain to you why
it shouldn't be done. I suggest using the python-ideas mailing list;
people there are usually reasonably friendly, though occasionally you
find a caustic response to a dumb idea like "Python should use braces
for indentation" :)

>> 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.
>>
> Well you've already got socket.create_connection(), which (I think) does perform that lookup.

Yeah, I went looking for it and came across that, but wasn't sure if
that was it.

Now I look more closely (ie: look at the actual source code), I see
that it probably is the function I was thinking of. Might need a docs
patch to make it clear that this iterates over getaddrinfo() and
attempts them all in succession.

ChrisA

[toc] | [prev] | [next] | [standalone]


#89735

FromRonald van Zantvoort <the.loeki@gmail.com>
Date2015-05-01 06:37 -0700
Message-ID<8a3d28f9-52d4-48e3-9da3-dc64dd773d16@googlegroups.com>
In reply to#89724
On Friday, 1 May 2015 14:06:34 UTC+2, Chris Angelico  wrote:
> On Fri, May 1, 2015 at 8:41 PM, Ronald van Zantvoort wrote:
> > 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?
> 
> You could ask. At very worst, you'll find someone explain to you why
> it shouldn't be done. I suggest using the python-ideas mailing list;
> people there are usually reasonably friendly, though occasionally you
> find a caustic response to a dumb idea like "Python should use braces
> for indentation" :)

There are no dumb ideas, just dumb people ;)

But seriously, I'll go see over there, thanks :) It's just that I'm amazed that this isn't in there yet; it's 2 simple properties added in for convenience and IMHO very Pythonic to be able to do aforementioned code.

Thanks again pplz :)

[toc] | [prev] | [next] | [standalone]


#89736

FromChris Angelico <rosuav@gmail.com>
Date2015-05-01 23:46 +1000
Message-ID<mailman.17.1430487966.3347.python-list@python.org>
In reply to#89735
On Fri, May 1, 2015 at 11:37 PM, Ronald van Zantvoort
<the.loeki@gmail.com> wrote:
> But seriously, I'll go see over there, thanks :) It's just that I'm amazed that this isn't in there yet; it's 2 simple properties added in for convenience and IMHO very Pythonic to be able to do aforementioned code.
>

There are lots of things that are so obvious they get added with
little argument, but someone still has to think of them :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#89737

FromRonald van Zantvoort <the.loeki@gmail.com>
Date2015-05-01 06:48 -0700
Message-ID<0bf34717-3a90-48da-a1a8-05b4ebd504c5@googlegroups.com>
In reply to#89736
On Friday, 1 May 2015 15:46:32 UTC+2, Chris Angelico  wrote:
> On Fri, May 1, 2015 at 11:37 PM, Ronald van Zantvoort wrote:
> > But seriously, I'll go see over there, thanks :) It's just that I'm amazed that this isn't in there yet; it's 2 simple properties added in for convenience and IMHO very Pythonic to be able to do aforementioned code.
> >
> 
> There are lots of things that are so obvious they get added with
> little argument, but someone still has to think of them :)
> 
> ChrisA

Hehehe right you are. I've posted an idea to python-ideas, we'll see how it goes from there.
Thanks again!

[toc] | [prev] | [next] | [standalone]


#89720

FromAlain Ketterlin <alain@dpt-info.u-strasbg.fr>
Date2015-05-01 12:31 +0200
Message-ID<87r3r07foa.fsf@dpt-info.u-strasbg.fr>
In reply to#89717
the.loeki@gmail.com writes:

> 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

Check the value of the "version" attribute. Or use socket.getaddrinfo(),
which is more convenient if all you need is an address parser.

-- Alain.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web