sockets: bind to external interface

Discussion in 'Python' started by Hans Georg Schaathun, Apr 25, 2011.

  1. 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
    Hans Georg Schaathun, Apr 25, 2011
    #1
    1. Advertising

  2. On Tue, Apr 26, 2011 at 5:37 AM, Hans Georg Schaathun <> 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
    Chris Angelico, Apr 25, 2011
    #2
    1. Advertising

  3. On Apr 25, 3:49 pm, Chris Angelico <> wrote:
    > On Tue, Apr 26, 2011 at 5:37 AM, Hans Georg Schaathun <> 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
    Jean-Paul Calderone, Apr 25, 2011
    #3
  4. On Tue, 26 Apr 2011 05:49:07 +1000, Chris Angelico
    <> 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
    Hans Georg Schaathun, Apr 25, 2011
    #4
  5. On Mon, 25 Apr 2011 21:14:51 +0100, Hans Georg Schaathun
    <> 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
    Hans Georg Schaathun, Apr 25, 2011
    #5
  6. On Tue, Apr 26, 2011 at 6:14 AM, Hans Georg Schaathun <> 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
    Chris Angelico, Apr 25, 2011
    #6
  7. Am 25.04.2011 22:14 schrieb Hans Georg Schaathun:

    > On Tue, 26 Apr 2011 05:49:07 +1000, Chris Angelico
    > <> 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
    Thomas Rachel, Apr 25, 2011
    #7
  8. 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
    Thomas Rachel, Apr 25, 2011
    #8
  9. On Tue, Apr 26, 2011 at 6:24 AM, Hans Georg Schaathun <> 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
    Chris Angelico, Apr 25, 2011
    #9
  10. On Tue, Apr 26, 2011 at 7:18 AM, Thomas Rachel
    <>
    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
    Chris Angelico, Apr 25, 2011
    #10
  11. On Mon, 25 Apr 2011 23:18:05 +0200, Thomas Rachel
    <> 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
    Hans Georg Schaathun, Apr 26, 2011
    #11
  12. 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
    Jean-Michel Pichavant, Apr 26, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,593
    mr2_93
    Oct 2, 2005
  2. Replies:
    7
    Views:
    1,505
    Steve W. Jackson
    Oct 11, 2005
  3. Johannes Eble
    Replies:
    1
    Views:
    1,299
    Grant Edwards
    Jul 16, 2003
  4. Aaron Gray

    C++ Sockets interface

    Aaron Gray, Aug 4, 2005, in forum: C++
    Replies:
    5
    Views:
    344
    Chen Ming
    Aug 9, 2005
  5. Replies:
    0
    Views:
    589
Loading...

Share This Page