python - 使用请求发送具有不同源 IP 的 HTTP 请求
问题描述
我有一个网络接口,我从2a00:3344:2000:1300::30/56
子网中分配了所有 IPv6 地址。
我现在想发出n
HTTP 请求,并让每个请求的源 IP 在此子网requests
内有所不同。/56
使用自定义传输适配器来实现这一点,例如
import requests
from urllib3.poolmanager import PoolManager
class SourceIPManager(requests.adapters.HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(
num_pools=connections, maxsize=maxsize,
block=block, source_address=('2a00:f48:2000:1300::30', 710))
s = requests.Session()
a = SourceIPManager()
s.mount('http://', a)
r = s.get('http://www.google.de')
仅当我设置source_address
为2a00:f48:2000:1300::30
(如果这是ip addr show dev eth0
显示的主 IP)时才有效。指定2a00:f48:2000:1300::31
会产生以下错误:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.google.de', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbf23601a20>: Failed to establish a new connection: [Errno -9] Address family for hostname not supported',))
即使接口也分配了这个 IP 地址。
我在做什么错,如何使用不同的源地址发送不同的请求requests
?
解决方案
推荐阅读
- numpy - 如何在 Tensorflow 中编码输入图像的随机旋转变换?
- python - 如何通过xpath提取html dom中文本节点的文本?
- jquery - 从 DropDown 为表单中的输入标签添加 ID
- ruby - 使用 Ruby/Nokogiri 从嵌套的 xml 文件中获取值
- javascript - setDate 方法返回一个字符串
- c# - DataGrid 不会显示下一页的数据
- ubuntu - 在 Docker 18.03 的 WSL 中将卷挂载到容器
- html - Bootstrap 4多级导航栏,我怎样才能让它在第三层向右下降?
- javascript - 新模块添加后反应原生中的 ESLint 错误(等待/异步)
- javascript - 在上下文文件中反应上下文 API / Bind 函数?