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


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

sockets: bind to external interface

Started byHans Georg Schaathun <hg@schaathun.net>
First post2011-04-25 20:37 +0100
Last post2011-04-26 11:24 +0200
Articles 12 — 5 participants

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


Contents

  sockets: bind to external interface Hans Georg Schaathun <hg@schaathun.net> - 2011-04-25 20:37 +0100
    Re: sockets: bind to external interface Chris Angelico <rosuav@gmail.com> - 2011-04-26 05:49 +1000
      Re: sockets: bind to external interface Jean-Paul Calderone <calderone.jeanpaul@gmail.com> - 2011-04-25 13:13 -0700
      Re: sockets: bind to external interface Hans Georg Schaathun <hg@schaathun.net> - 2011-04-25 21:14 +0100
        Re: sockets: bind to external interface Hans Georg Schaathun <hg@schaathun.net> - 2011-04-25 21:24 +0100
          Re: sockets: bind to external interface Chris Angelico <rosuav@gmail.com> - 2011-04-26 07:21 +1000
        Re: sockets: bind to external interface Chris Angelico <rosuav@gmail.com> - 2011-04-26 06:30 +1000
          Re: sockets: bind to external interface Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-25 23:18 +0200
            Re: sockets: bind to external interface Chris Angelico <rosuav@gmail.com> - 2011-04-26 07:50 +1000
            Re: sockets: bind to external interface Hans Georg Schaathun <hg@schaathun.net> - 2011-04-26 06:20 +0100
        Re: sockets: bind to external interface Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-25 23:14 +0200
    Re: sockets: bind to external interface Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-04-26 11:24 +0200

#3990 — sockets: bind to external interface

FromHans Georg Schaathun <hg@schaathun.net>
Date2011-04-25 20:37 +0100
Subjectsockets: bind to external interface
Message-ID<k4vg88-0tt.ln1@svn.schaathun.net>
Is there a simple way to find the external interface and bind a
socket to it, when the hostname returned by socket.gethostname()
maps to localhost?

What seems to be the standard ubuntu configuration lists the local
hostname with 127.0.0.1 in /etc/hosts.  (I checked this on two ubuntu
boxen, on only one of which I am root.)  Thus, the standard solution
of binding to whatever socket.gethostname() returns does not work.

Has anyone found a simple solution that can be administered without
root privileges?  I mean simpler than passing the ip address 
manually :-)

TIA
-- 
:-- Hans Georg

[toc] | [next] | [standalone]


#3991

FromChris Angelico <rosuav@gmail.com>
Date2011-04-26 05:49 +1000
Message-ID<mailman.815.1303760950.9059.python-list@python.org>
In reply to#3990
On Tue, Apr 26, 2011 at 5:37 AM, Hans Georg Schaathun <hg@schaathun.net> wrote:
> Has anyone found a simple solution that can be administered without
> root privileges?  I mean simpler than passing the ip address
> manually :-)

You can run 'ifconfig' without being root, so there must be a way. At
very worst, parse ifconfig's output.

The way you talk of "the" external interface, I'm assuming this
computer has only one. Is there a reason for not simply binding to
INADDR_ANY aka 0.0.0.0? Do you specifically need to *not* bind to
127.0.0.1?

Chris Angelico

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


#3992

FromJean-Paul Calderone <calderone.jeanpaul@gmail.com>
Date2011-04-25 13:13 -0700
Message-ID<8ebb3b18-b983-4ff6-97eb-c062813d11c7@d12g2000vbz.googlegroups.com>
In reply to#3991
On Apr 25, 3:49 pm, Chris Angelico <ros...@gmail.com> wrote:
> On Tue, Apr 26, 2011 at 5:37 AM, Hans Georg Schaathun <h...@schaathun.net> wrote:
>
> > Has anyone found a simple solution that can be administered without
> > root privileges?  I mean simpler than passing the ip address
> > manually :-)
>
> You can run 'ifconfig' without being root, so there must be a way. At
> very worst, parse ifconfig's output.
>
> The way you talk of "the" external interface, I'm assuming this
> computer has only one. Is there a reason for not simply binding to
> INADDR_ANY aka 0.0.0.0? Do you specifically need to *not* bind to
> 127.0.0.1?
>
> Chris Angelico

Binding to 0.0.0.0 is usually the right thing to do.  The OP should
probably do that unless he has some particular reason for doing
otherwise.  The comment about "the standard solution of binding to
whatever socket.gethostname() returns" suggests that perhaps he wasn't
aware that actually the standard solution is to bind to 0.0.0.0.

However, the system stack can usually be tricked into revealing some
more information this way:

    >>> import socket
    >>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    >>> s.connect(('1.2.3.4', 1234))
    >>> s.getsockname()
    ('192.168.1.148', 47679)

Jean-Paul

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


#3993

FromHans Georg Schaathun <hg@schaathun.net>
Date2011-04-25 21:14 +0100
Message-ID<ra1h88-e0u.ln1@svn.schaathun.net>
In reply to#3991
On Tue, 26 Apr 2011 05:49:07 +1000, Chris Angelico
  <rosuav@gmail.com> wrote:
:  You can run 'ifconfig' without being root, so there must be a way. At
:  very worst, parse ifconfig's output.

Of course, but I am not sure that's simpler than the manual solution.
Especially since there is more than one version of ifconfig ...

:  The way you talk of "the" external interface, I'm assuming this
:  computer has only one. Is there a reason for not simply binding to
:  INADDR_ANY aka 0.0.0.0?

Ah.  That's what I really wanted.  Thanks a lot.  I wonder why that
was not mentioned in the tutorial I used ...

-- 
:-- Hans Georg

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


#3994

FromHans Georg Schaathun <hg@schaathun.net>
Date2011-04-25 21:24 +0100
Message-ID<2s1h88-r3v.ln1@svn.schaathun.net>
In reply to#3993
On Mon, 25 Apr 2011 21:14:51 +0100, Hans Georg Schaathun
  <hg@schaathun.net> wrote:
: :  The way you talk of "the" external interface, I'm assuming this
: :  computer has only one. Is there a reason for not simply binding to
: :  INADDR_ANY aka 0.0.0.0?
: 
:  Ah.  That's what I really wanted.  Thanks a lot.  I wonder why that
:  was not mentioned in the tutorial I used ...

Hmmm.  socket.INADDR_ANY is an integer and bind insists on a string
for the hostname (Python 2.6).  Is there any use for the integer
constant?  "0.0.0.0" does exactly what I wanted though.  Thanks again.

-- 
:-- Hans Georg

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


#4000

FromChris Angelico <rosuav@gmail.com>
Date2011-04-26 07:21 +1000
Message-ID<mailman.818.1303766478.9059.python-list@python.org>
In reply to#3994
On Tue, Apr 26, 2011 at 6:24 AM, Hans Georg Schaathun <hg@schaathun.net> wrote:
> Hmmm.  socket.INADDR_ANY is an integer and bind insists on a string
> for the hostname (Python 2.6).  Is there any use for the integer
> constant?  "0.0.0.0" does exactly what I wanted though.  Thanks again.

Apologies - I've done most of my sockets programming in C, where the
integer constant is applicable. 0.0.0.0 means the exact same thing.

Chris Angelico

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


#3995

FromChris Angelico <rosuav@gmail.com>
Date2011-04-26 06:30 +1000
Message-ID<mailman.816.1303763462.9059.python-list@python.org>
In reply to#3993
On Tue, Apr 26, 2011 at 6:14 AM, Hans Georg Schaathun <hg@schaathun.net> wrote:
> :  The way you talk of "the" external interface, I'm assuming this
> :  computer has only one. Is there a reason for not simply binding to
> :  INADDR_ANY aka 0.0.0.0?
>
> Ah.  That's what I really wanted.  Thanks a lot.  I wonder why that
> was not mentioned in the tutorial I used ...

If you don't care what port you use, you don't need to bind at all.
That may be why it's not mentioned - the classic TCP socket server
involves bind/listen/accept, and the classic TCP client has just
connect; bind/connect is a lot less common.

Incidentally, interfaces don't have to correspond 1:1 to network
cards. At work, we have a system of four IP addresses for each server,
even though it has only one NIC - it's used for traffic management and
routing. Binding to a specific address is sometimes important there.

Chris Angelico

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


#3999

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-04-25 23:18 +0200
Message-ID<ip4oed$gcc$2@r03.glglgl.eu>
In reply to#3995
Am 25.04.2011 22:30, schrieb Chris Angelico:

> If you don't care what port you use, you don't need to bind at all.
> That may be why it's not mentioned - the classic TCP socket server
> involves bind/listen/accept, and the classic TCP client has just
> connect; bind/connect is a lot less common.

That is right, but I cannot see where he mentions the "direction" of the 
socket. My fist thought was that he tries to have a server socket...

(BTW: bind can be omitted on server sockets as well; listen() seems to 
includes a bind(('', 0)) if not called explicitly before. In this case, 
the port is assigned randomly. Can be useful in some cases, where the 
port number is not fixed...)


> Incidentally, interfaces don't have to correspond 1:1 to network
> cards. At work, we have a system of four IP addresses for each server,
> even though it has only one NIC - it's used for traffic management and
> routing. Binding to a specific address is sometimes important there.

If you use IPv6 and activate the privacy extensions (in order to 
periodically create a new IP address), a NIC will have even more 
addresses - the ones which aren't used any longer will be kept for a 
certain time on order not to kill any existing connections.


Thomas

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


#4003

FromChris Angelico <rosuav@gmail.com>
Date2011-04-26 07:50 +1000
Message-ID<mailman.820.1303768243.9059.python-list@python.org>
In reply to#3999
On Tue, Apr 26, 2011 at 7:18 AM, Thomas Rachel
<nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
wrote:
> Am 25.04.2011 22:30, schrieb Chris Angelico:
>
>> If you don't care what port you use, you don't need to bind at all.
>> That may be why it's not mentioned - the classic TCP socket server
>> involves bind/listen/accept, and the classic TCP client has just
>> connect; bind/connect is a lot less common.
>
> That is right, but I cannot see where he mentions the "direction" of the
> socket. My fist thought was that he tries to have a server socket...
>
> (BTW: bind can be omitted on server sockets as well; listen() seems to
> includes a bind(('', 0)) if not called explicitly before. In this case, the
> port is assigned randomly. Can be useful in some cases, where the port
> number is not fixed...)

Yes; for FTP data sockets, it doesn't matter what the port is, as long
as you tell the other end. Same as you can bind/connect, you can
not-bind and listen/accept. This is why I'm glad the socket subsystem
allows unusual behaviours (I've used bind/connect in a few places).
Give the programmer the tools and let him do what he chooses!

Chris Angelico

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


#4024

FromHans Georg Schaathun <hg@schaathun.net>
Date2011-04-26 06:20 +0100
Message-ID<391i88-s9.ln1@svn.schaathun.net>
In reply to#3999
On Mon, 25 Apr 2011 23:18:05 +0200, Thomas Rachel
  <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> wrote:
:  That is right, but I cannot see where he mentions the "direction" of the 
:  socket. My fist thought was that he tries to have a server socket...

Quite right.  I thought it was implied by the need to bind :-)
Sorry for the lack of detail.

-- 
:-- Hans Georg

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


#3998

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-04-25 23:14 +0200
Message-ID<ip4o7g$gcc$1@r03.glglgl.eu>
In reply to#3993
Am 25.04.2011 22:14 schrieb Hans Georg Schaathun:

> On Tue, 26 Apr 2011 05:49:07 +1000, Chris Angelico
>    <rosuav@gmail.com>  wrote:

> :  The way you talk of "the" external interface, I'm assuming this
> :  computer has only one. Is there a reason for not simply binding to
> :  INADDR_ANY aka 0.0.0.0?
>
> Ah.  That's what I really wanted.  Thanks a lot.  I wonder why that
> was not mentioned in the tutorial I used ...

Generally, it seems better to use '' instead of '0.0.0.0' in this case 
in order to stay compatible with other address families, especially INET6.


Thomas

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


#4028

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2011-04-26 11:24 +0200
Message-ID<mailman.829.1303809867.9059.python-list@python.org>
In reply to#3990
Hans Georg Schaathun wrote:
> Is there a simple way to find the external interface and bind a
> socket to it, when the hostname returned by socket.gethostname()
> maps to localhost?
>
> What seems to be the standard ubuntu configuration lists the local
> hostname with 127.0.0.1 in /etc/hosts.  (I checked this on two ubuntu
> boxen, on only one of which I am root.)  Thus, the standard solution
> of binding to whatever socket.gethostname() returns does not work.
>
> Has anyone found a simple solution that can be administered without
> root privileges?  I mean simpler than passing the ip address 
> manually :-)
>
> TIA
>   
Hi,

Use the address 0.0.0.0

JM

[toc] | [prev] | [standalone]


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


csiph-web