tcp client socket bind problem
Marc Christiansen
usenet at solar-empire.de
Mon Mar 10 10:40:10 EDT 2008
natambu at gmail.com 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
More information about the Python-list
mailing list