简体   繁体   English

python socket.connect在虚拟IP上似乎不起作用

[英]python socket.connect does not seem to work on a virtual IP

I have set up some virtual IPs via: 我已经通过以下方式设置了一些虚拟IP:

~# ip link add link eth0 name eth0.1 address 11:22:33:44:55:66 type macvlan
~# ifconfig eth0.1 10.10.0.0/24

I am using the following code to connect from it: 我正在使用以下代码从中进行连接:

sTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sTCP.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, IFACE)
print "PORT s_TCP:" + str(HOST) +":" +str(TCP_PORT)
sTCP.connect((HOST, TCP_PORT))
print "Connected"

this works fine if IFACE is eth0, but it does not get past sTCP.connect from eth0.1 and fails in bindtodevice (as expected) on eth0.2. 如果IFACE为eth0,则此方法工作正常,但它不会从eth0.1越过sTCP.connect,并且在eth0.2上的bindtodevice失败(如预期的那样)。

Why does eth0.1 not work? 为什么eth0.1不起作用? Is this a python problem, or something in the linux network implementation? 这是python问题,还是linux网络实现中的某些问题?

I just tried this on my Fedora 13 system and it worked. 我只是在我的Fedora 13系统上进行了尝试,但它确实有效。 I did have to make some modifications to make it work on my system, hopefully this will give you clues. 我确实需要进行一些修改才能使其在我的系统上运行,希望这会给您一些提示。 Code used: 使用的代码:

### in shell
# Used 00 for first MAC octet to avoid issues with multicast addressing
ip link add link eth0 name eth0.1 address 00:22:33:44:55:66 type macvlan
ifconfig eth0.1 10.1.23.6/25

# python
import socket
HOST = "10.1.23.30"
TCP_PORT = 80
IFACE = "eth0.1"
sTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# switched to socket.SO_BINDTODEVICE since I'm not sure what "IN" referred to
# EDIT: figured out there's another module called IN, but the value is the same (25)
sTCP.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, IFACE)
print "PORT s_TCP:" + str(HOST) +":" +str(TCP_PORT)
sTCP.connect((HOST, TCP_PORT))
print "Connected"

I used tcpdump to prove to myself the packets were coming out of eth0.1. 我使用tcpdump向我自己证明了数据包是从eth0.1中传出的。 Perhaps you're running into a VLAN issue? 也许您遇到了VLAN问题? Run packet captures on the client and server to see what's actually happening on the wire. 在客户端和服务器上运行数据包捕获,以查看网络上实际发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM