首页 > 解决方案 > Python 请求,为每个请求绑定到不同的源 ip,但未按预期工作

问题描述

我的服务器有两个 NIC ens1ens2.这些 NIC 位于两个不同的 VPC 网络上,连接到同一台服务器(我使用的是 Google Cloud)。我认为ens1是默认的,ens2是次要的。

在下面的代码中,当我传递它的SOURCE_IPIP 地址时,ens1它工作正常。但是当我通过ens2as的 IP 时,SOURCE_IP程序停止并进入等待状态。它没有显示任何错误,也没有工作,只是挂在那里。

我正在传递 NIC 的内部 IP。

 import requests
    from requests_toolbelt.adapters import source
    
    s = requests.Session()
    new_source = source.SourceAddressAdapter('<SOURCE_IP>')
    s.mount('http://', new_source)
    s.mount('https://', new_source)
    rep = s.get("https://httpbin.org/ip")
    print(rep.text)

标签: pythonsocketspython-requests

解决方案


推荐阅读