如何为EXECIFIC网络接口创建传出套接字?

时间:2022-09-06 12:22:25

I have a server with two different network interfaces, each with a different IP address. How can I create a socket so it'll go out a specific IP address?

我有一个具有两个不同网络接口的服务器,每个接口都有不同的IP地址。如何创建一个套接字以便它能够输出特定的IP地址?

I'd prefer a python example, but the question is language agnostic, so shoot away.

我更喜欢一个python示例,但问题是语言不可知,所以拍摄。

EDIT: Please don't give me "You can't" as an answer. I mean, it is a computer. I can do anything I like to it, for example - I can programatically disable the one interface I don't want on the fly. I'm looking for something prettier.

编辑:请不要给我“你不能”作为答案。我的意思是,它是一台电脑。我可以做任何我喜欢的事情,例如 - 我可以以编程方式禁用我不想要的一个界面。我正在寻找更漂亮的东西。

3 个解决方案

#1


12  

You can certainly bind a socket to a specific device.

您当然可以将套接字绑定到特定设备。

I don't know how to do it in python, but using the berkeley socket api (in C) you need to call setsockopt(), using the option SO_BINDTODEVICE.

我不知道如何在python中做到这一点,但是使用berkeley socket api(在C中)你需要使用选项SO_BINDTODEVICE来调用setsockopt()。

You pass in an interface descriptor, which is of type struct ifreq. Ideally you would get the contents of the interface descriptor by using ioctl(), and requesting SIOCGIFINDEX - passing the name of the interface (eg. eth0) as an argument.

传入一个接口描述符,类型为struct ifreq。理想情况下,您将通过使用ioctl()获取接口描述符的内容,并请求SIOCGIFINDEX - 将接口的名称(例如,eth0)作为参数传递。


edit: Just did a quick search and found this documentation of the socket methods in python. setsockopt() is amongst them.

编辑:刚刚快速搜索并在python中找到了socket文件的这个文档。 setsockopt()就是其中之一。

#2


11  

Just a little note - what I really needed is to bind to a specific IP, and just for the sake of completeness, the solution is to bind the socket after creation. Source in python:

只需要注意一点 - 我真正需要的是绑定到特定的IP,并且为了完整起见,解决方案是在创建后绑定套接字。 python中的源代码:

import socket
s = socket.socket()
s.bind(("127.0.0.1", 0))
s.connect(("321.12.131.432", 80))

#3


1  

import socket
s = socket.socket()
s.bind((get_ip_address('eth0'), 0))

from Quora

#1


12  

You can certainly bind a socket to a specific device.

您当然可以将套接字绑定到特定设备。

I don't know how to do it in python, but using the berkeley socket api (in C) you need to call setsockopt(), using the option SO_BINDTODEVICE.

我不知道如何在python中做到这一点,但是使用berkeley socket api(在C中)你需要使用选项SO_BINDTODEVICE来调用setsockopt()。

You pass in an interface descriptor, which is of type struct ifreq. Ideally you would get the contents of the interface descriptor by using ioctl(), and requesting SIOCGIFINDEX - passing the name of the interface (eg. eth0) as an argument.

传入一个接口描述符,类型为struct ifreq。理想情况下,您将通过使用ioctl()获取接口描述符的内容,并请求SIOCGIFINDEX - 将接口的名称(例如,eth0)作为参数传递。


edit: Just did a quick search and found this documentation of the socket methods in python. setsockopt() is amongst them.

编辑:刚刚快速搜索并在python中找到了socket文件的这个文档。 setsockopt()就是其中之一。

#2


11  

Just a little note - what I really needed is to bind to a specific IP, and just for the sake of completeness, the solution is to bind the socket after creation. Source in python:

只需要注意一点 - 我真正需要的是绑定到特定的IP,并且为了完整起见,解决方案是在创建后绑定套接字。 python中的源代码:

import socket
s = socket.socket()
s.bind(("127.0.0.1", 0))
s.connect(("321.12.131.432", 80))

#3


1  

import socket
s = socket.socket()
s.bind((get_ip_address('eth0'), 0))

from Quora