Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用hrequests指定哪个网口发送请求该怎么写? #55

Open
tzbts opened this issue Sep 9, 2024 · 0 comments
Open

用hrequests指定哪个网口发送请求该怎么写? #55

tzbts opened this issue Sep 9, 2024 · 0 comments

Comments

@tzbts
Copy link

tzbts commented Sep 9, 2024

用hrequests指定哪个网口发送请求该怎么写?

比如使用requests指定网口发送请求:

class BindToInterfaceAdapter(HTTPAdapter):
def init(self, iface, *args, **kwargs):
self.iface = iface
super().init(*args, **kwargs)

def init_poolmanager(self, *args, **kwargs):
    kwargs['socket_options'] = [
        (socket.SOL_SOCKET, socket.SO_BINDTODEVICE, self.iface.encode())
    ]
    return super().init_poolmanager(*args, **kwargs)

def fetch_main_page():

选择你希望使用的网卡,比如 eth1

interface = 'eno4'

创建一个会话,绑定到指定的网卡接口

session = requests.Session()
session.mount('http://', BindToInterfaceAdapter(interface))
session.mount('https://', BindToInterfaceAdapter(interface))
resp = session.get(url=url, timeout=30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant