使用python绑定连接到网络接口卡

使用python绑定连接到网络接口卡,第1张

概述我正在尝试连接/绑定到特定的网络接口卡(NIC)或无线网络接口.例如,当我创建套接字时,我想使用网络名称(例如’wlan0’或’eth0′)进行连接,而不是使用IP地址. 在JAVA中,我可以使用以下代码轻松完成此 *** 作: //Initializing command socket//String networkCard = "wlan0"; //or could be "eth0", etc. 我正在尝试连接/绑定到特定的网络接口卡(NIC)或无线网络接口.例如,当我创建套接字时,我想使用网络名称(例如’wlan0’或’eth0′)进行连接,而不是使用IP地址.
在JAVA中,我可以使用以下代码轻松完成此 *** 作:
//Initializing command socket//String networkCard = "wlan0"; //or Could be "eth0",etc.NetworkInterface nif = NetworkInterface.getByname(networkCard);Enumeration<InetAddress> nifAddresses = nif.getInetAddresses();// IP address of robot connected to NIC       SocketAddress sockaddr = new InetSocketAddress("192.168.1.100",80);sock = new Socket();// bind to the specific NIC card which is connected to a specific robotsock.bind(new InetSocketAddress(nifAddresses.nextElement(),0));sock.connect(sockaddr,10000);

我想把它翻译成Python,但我很难过.
有关如何做到这一点的任何建议?

我正在使用sockopt和AF_CAN,但没有任何工作.

非常感谢你!!!

解决方法 实际上答案很简单.它类似于IDx在之前的回答中所做的:
def findConnectedRobot():'''Finds which robots are connected to the computer and returns theaddresses of the NIC they are connected to'''robot_address = []  # stores NIC addressimport netifaces# get the List of availble NIC'sfor card in netifaces.interfaces():    try:        # get all NIC addresses        temp = netifaces.ifaddresses(\                card)[netifaces.AF_INET][0]['addr']        temp2 = temp.split('.')        # see if address matches common address given to NIC when        # NIC is connected to a robot        if temp2[0] == '192' and int(temp2[3]) < 30:            print('appending address: ' + temp)            robot_address.append(temp)    except BaseException:        passreturn robot_address

在我得到“机器人地址”后,我可以像普通插座一样绑定/连接它们.

谢谢您的帮助!

总结

以上是内存溢出为你收集整理的使用python绑定/连接到网络接口卡全部内容,希望文章能够帮你解决使用python绑定/连接到网络接口卡所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1205002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存