tcp client socket bind problem

Discussion in 'Python' started by natambu@gmail.com, Mar 10, 2008.

  1. Guest

    I have a linux box with multiple ip addresses. I want to make my
    python client connect from one of the ip addresses. Here is my code,
    no matter what valid information I put in the bind it always comes
    from the default ip address on the server. Am I doing something wrong?


    -------------
    #!/usr/bin/python

    import socket

    host = "server"
    port = 1190

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    sock.bind(("<ipalias>",0))
    sock.connect((host, port))
    -------------
    , Mar 10, 2008
    #1
    1. Advertising

  2. wrote:
    > I have a linux box with multiple ip addresses. I want to make my
    > python client connect from one of the ip addresses. Here is my code,
    > no matter what valid information I put in the bind it always comes
    > from the default ip address on the server. Am I doing something wrong?
    >
    > -------------
    > #!/usr/bin/python
    >
    > import socket
    >
    > host = "server"
    > port = 1190
    >
    > sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >
    > sock.bind(("<ipalias>",0))
    > sock.connect((host, port))
    > -------------


    Looks good to me. Just to verify it, I added 127.1.2.3 as an address to
    lo (ip addr add 127.1.2.3/8 dev lo broadcast 127.255.255.255), and...

    >>> import socket
    >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >>> sock.bind(("127.1.2.3",0))
    >>> sock.connect(("127.0.0.1",80))


    In another shell:
    tolot:~> lsof -c python -a -i -nP
    COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
    [...]
    python 1287 tolot 3u IPv4 3553610 TCP 127.1.2.3:38329->127.0.0.1:80 (ESTABLISHED)

    Looks correct. This is using Linux 2.6.23. So, if you're doing something
    wrong, it's nothing obvious to me.

    Marc
    Marc Christiansen, Mar 10, 2008
    #2
    1. Advertising

  3. Guest

    On Mar 10, 9:40 am, Marc Christiansen <> wrote:
    > wrote:
    > > I have a linux box with multiple ip addresses. I want to make my
    > > python client connect from one of the ip addresses. Here is my code,
    > > no matter what valid information I put in the bind it always comes
    > > from the default ip address on the server. Am I doing something wrong?

    >
    > > -------------
    > > #!/usr/bin/python

    >
    > > import socket

    >
    > > host = "server"
    > > port = 1190

    >
    > > sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    >
    > > sock.bind(("<ipalias>",0))
    > > sock.connect((host, port))
    > > -------------

    >
    > Looks good to me. Just to verify it, I added 127.1.2.3 as an address to
    > lo (ip addr add 127.1.2.3/8 dev lo broadcast 127.255.255.255), and...
    >
    >  >>> import socket
    >  >>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >  >>> sock.bind(("127.1.2.3",0))
    >  >>> sock.connect(("127.0.0.1",80))
    >
    > In another shell:
    > tolot:~> lsof -c python -a -i -nP
    > COMMAND  PID  USER   FD   TYPE  DEVICE SIZE NODE NAME
    > [...]
    > python  1287 tolot    3u  IPv4 3553610       TCP 127.1.2.3:38329->127.0.0.1:80 (ESTABLISHED)


    help string:
    Bind the socket to a local address. For IP sockets, the address is a
    pair (host, port); the host must refer to the local host.

    docs:
    Bind the socket to address.

    Wikipedia:
    Before a socket may accept incoming connections, it must be bound.

    PHP.net:
    Binds the name given in address to the socket described by socket .
    This has to be done before a connection is be established using
    socket_connect() or socket_listen().
    This function must be used on the socket before socket_connect().
    , Mar 10, 2008
    #3
  4. Tim Roberts Guest

    wrote:
    >
    >On Mar 10, 9:40 am, Marc Christiansen <> wrote:
    >> wrote:
    >> > I have a linux box with multiple ip addresses. I want to make my
    >> > python client connect from one of the ip addresses. Here is my code,
    >> > no matter what valid information I put in the bind it always comes
    >> > from the default ip address on the server. Am I doing something wrong?

    >...
    >
    >help string:
    >Bind the socket to a local address. For IP sockets, the address is a
    >pair (host, port); the host must refer to the local host.
    >
    >docs:
    >Bind the socket to address.
    >
    >Wikipedia:
    >Before a socket may accept incoming connections, it must be bound.
    >
    >PHP.net:
    >Binds the name given in address to the socket described by socket .
    >This has to be done before a connection is be established using
    >socket_connect() or socket_listen().
    >This function must be used on the socket before socket_connect().


    That's all true. So what was your point? How does this help the original
    poster?
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Mar 11, 2008
    #4
  5. Guest

    On Mar 11, 2:19 am, Tim Roberts <> wrote:
    > wrote:
    >
    > >On Mar 10, 9:40 am, Marc Christiansen <> wrote:
    > >> wrote:
    > >> > I have a linux box with multiple ip addresses. I want to make my
    > >> > python client connect from one of the ip addresses. Here is my code,
    > >> > no matter what valid information I put in the bind it always comes
    > >> > from the default ip address on the server. Am I doing something wrong?

    > >...

    >
    > >help string:
    > >Bind the socket to a local address.  For IP sockets, the address is a
    > >pair (host, port); the host must refer to the local host.

    >
    > >docs:
    > >Bind the socket to address.

    >
    > >Wikipedia:
    > >Before a socket may accept incoming connections, it must be bound.

    >
    > >PHP.net:
    > >Binds the name given in address to the socket described by socket .
    > >This has to be done before a connection is be established using
    > >socket_connect() or socket_listen().
    > >This function must be used on the socket before socket_connect().

    >
    > That's all true.  So what was your point?  How does this help the original
    > poster?


    Confidence-- a second opinion of what the docs say. Then, something
    of a criticism of the docs.
    , Mar 12, 2008
    #5
  6. On Sun, 09 Mar 2008 22:21:59 -0700, natambu wrote:

    > I have a linux box with multiple ip addresses. I want to make my python
    > client connect from one of the ip addresses. Here is my code, no matter
    > what valid information I put in the bind it always comes from the
    > default ip address on the server. Am I doing something wrong?
    >
    >
    > -------------
    > #!/usr/bin/python
    >
    > import socket
    >
    > host = "server"
    > port = 1190
    >
    > sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    >
    > sock.bind(("<ipalias>",0))
    > sock.connect((host, port))
    > -------------


    I'd've expected that to work.

    What does your routing table look like? You can view it with "netstat -
    nr". I believe when a Linux network interface is ifconfig'd, the kernel
    will add a route for that interface based on the netmask in the ifconfig,
    but you could conceivably have another route that is broader overriding
    the specific route.
    Dan Stromberg, Mar 12, 2008
    #6
    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. Tor Erik
    Replies:
    12
    Views:
    8,817
    Rhamphoryncus
    Sep 5, 2006
  2. Tiger
    Replies:
    5
    Views:
    958
    Dave Thompson
    May 1, 2006
  3. Ajith
    Replies:
    0
    Views:
    516
    Ajith
    May 2, 2007
  4. TsanChung
    Replies:
    5
    Views:
    2,735
    Antoninus Twink
    Oct 18, 2008
  5. TsanChung
    Replies:
    3
    Views:
    1,813
    Antoninus Twink
    Oct 18, 2008
Loading...

Share This Page