python - Python 请求,为每个请求绑定到不同的源 ip,但未按预期工作
问题描述
我的服务器有两个 NIC ens1
,ens2.
这些 NIC 位于两个不同的 VPC 网络上,连接到同一台服务器(我使用的是 Google Cloud)。我认为ens1
是默认的,ens2
是次要的。
在下面的代码中,当我传递它的SOURCE_IP
IP 地址时,ens1
它工作正常。但是当我通过ens2
as的 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)
解决方案
推荐阅读
- solr - Solr Atomic 更新更改字段名称
- javascript - 在回调函数内部更改的全局变量的值未反映在函数外部
- python - Yaml 值在 python 3 中不可迭代
- c# - 我们如何在 ASP.NET Core 的自定义属性中访问请求上下文?
- ios - 设置两个变量后如何调用方法
- python - Python中多个while循环退出条件的模式
- visual-studio-code - 如何在 VS Code 任务中直接引用位于 .vscode/extensions/ 扩展名中的文件?
- docusignapi - docusign 支持的云提供商 - 它是否支持 AWS?
- java - 流式传输文件并在读取后移动它们
- go - 在没有互斥体的情况下同时读取或写入时会发生什么