Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过python中的指定接口发送用户定义的数据包_Python_Sockets_Ethernet - Fatal编程技术网

通过python中的指定接口发送用户定义的数据包

通过python中的指定接口发送用户定义的数据包,python,sockets,ethernet,Python,Sockets,Ethernet,我的主机上安装了四个以太网卡,我想用python发送一个用户定义的数据包(以太网之上的东西等)。有什么解决办法吗 我已经按照以下代码在C中实现了这一点: if ((sk = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_USER))) < 0) { perror("create socket error\n"); return -1; } // bind the interface like "eth0" memset(&nsa_

我的主机上安装了四个以太网卡,我想用python发送一个用户定义的数据包(以太网之上的东西等)。有什么解决办法吗

我已经按照以下代码在C中实现了这一点:

if ((sk = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_USER))) < 0) {
    perror("create socket error\n");
    return -1;
}

// bind the interface like "eth0"
memset(&nsa_ifr, 0, sizeof(nsa_ifr));
strncpy(nsa_ifr.ifr_name, dev_name, sizeof(nsa_ifr.ifr_name));
if (ioctl(sk, SIOCGIFINDEX, &nsa_ifr) < 0) {
    perror("SIOCGIFINDEX error\n");
    return -1;
}

memset(&nsa_dst, 0, sizeof(nsa_dst));
nsa_dst.sll_family   = AF_PACKET;
nsa_dst.sll_ifindex  = nsa_ifr.ifr_ifindex;
nsa_dst.sll_halen    = ETH_ALEN;
nsa_dst.sll_protocol = htons(ETH_P_USER);

// Then I can send a user defined packet 
n_send = sendto(sk, buf, size, 0, (struct sockaddr *) &nsa_dst, sizeof(nsa_dst));
但是我不知道如何将套接字绑定到python中的命名网络接口, 有什么想法吗?

你说的“我无法将套接字绑定到指定的网络接口”是什么意思?确切的故障模式是什么?它是否在错误的接口上发送数据包?您是否收到错误消息?请在您的帖子中包含失败的Python代码。很抱歉,这里的“failed”一词有误导性,意思是我不知道如何将套接字绑定到指定的网络接口。
    sk = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0801))