我有一个网络接口,我为2a00:3344:2000:1300::30/56
子网分配了所有IPv6地址。
我现在想使用n
发出requests
HTTP请求,并使每个请求的源IP在此/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
通过不同的源地址发送不同的请求?