如何在Linux centOS中使用python将原始套接字绑定到特定接口? [英] How to bind a raw socket to a specific interface using python in linux centOS?

查看:77
本文介绍了如何在Linux centOS中使用python将原始套接字绑定到特定接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Linux centOS中使用python将原始套接字绑定到特定接口?我有多个接口,例如eth0,eth0:1,eth0:2等

How to bind a raw socket to a specific interface using python in linux centOS? I have multiple interfaces like eth0, eth0:1, eth0:2,etc

推荐答案

您可以使用IP地址来实现

You can do it by using the IP address that corresponds to the desired interface.

import socket

s = socket.socket()
s.bind(('192.168.1.100', 12345))

s = socket.socket()
s.bind(('localhost', 12345))

s = socket.socket()
s.bind(('0.0.0.0', 12345))

上面的前两个将绑定到具有该IP地址的接口。最后一个将绑定到任何接口。您可以使用食谱

The first two above would bind to the interface with that IP address. The last one will bind to any interface. You can obtain the IP address for an interface using this recipe.

这篇关于如何在Linux centOS中使用python将原始套接字绑定到特定接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆