python连接插座在虚拟IP上似乎不起作用

2024-09-28 01:33:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过以下方式设置了一些虚拟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

我使用以下代码从它连接:

^{pr2}$

如果IFACE是eth0,这很好,但它不会过去sTCP.连接在eth0.2上的bindtodevice(如预期)中失败。在

为什么eth0.1不起作用?这是python问题,还是linux网络实现中的问题?在


Tags: 代码nameipaddaddresstype方式link
1条回答
网友
1楼 · 发布于 2024-09-28 01:33:53

我刚在我的Fedora13系统上试了一下,效果很好。我做了一些修改,使它在我的系统上工作,希望这能给你一些线索。使用的代码:

### 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"

我用tcpdump向自己证明包是从eth0.1发出的。也许你遇到了VLAN问题?在客户机和服务器上运行数据包捕获,以查看网络上实际发生了什么。在

相关问题 更多 >

    热门问题