Python 无法将请求绑定(bind)到网络接口(interface)

标签 python python-2.7 ssl openssl ssl-certificate

我无法使用请求代理通过我的本地 ip 连接到 https 192.168.1.55是我本地的ip

如果我隐藏代理的 https 行,它可以工作,但我知道它实际上并没有使用代理

import requests

ipAddress = '192.168.1.55:80'
proxies = {
  "http": "%s" % ipAddress,
  #"https": "%s" % ipAddress,
}

url = 'https://www.google.com'
res = requests.get(url, proxies=proxies)
print res

结果:响应[200]

import requests

ipAddress = '192.168.1.55:80'
proxies = {
  "http": "%s" % ipAddress,
  "https": "%s" % ipAddress,
}

url = 'https://www.google.com'
res = requests.get(url, proxies=proxies)
print res

结果:

requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): 最大重试次数超过 url:/(由 ProxyError('无法连接到代理。', error('Tunnel连接失败:400 错误请求',)))

我也尝试过支持 HTTPS 协议(protocol)的外部 VPN 服务器,即使取消隐藏 https 代理线路,它也可以工作

我有多个 IP,想在请求中使用指定的 IP。 我在使用 python 2.7 的 Win 10 上运行它,我怀疑这是由于 SSL 问题,尚未在这里确认专业知识。 (我想我没有正确处理 SSL) 我尝试了很多方法来处理 SSL,到目前为止没有成功。

最佳答案

您可以尝试将 requests 绑定(bind)到选定的适配器/IP,但首先安装 requests_toolbelt

pip install requests_toolbelt

然后

import requests
from requests_toolbelt.adapters.source import SourceAddressAdapter

# default binding
response = requests.get('https://ip.tyk.nu/').text
print(response)

# bind to 192.168.1.55
session = requests.Session()
session.mount('http://', SourceAddressAdapter('192.168.1.55'))
session.mount('https://', SourceAddressAdapter('192.168.1.55'))
response = session.get('https://ip.tyk.nu/').text
print(response)

关于Python 无法将请求绑定(bind)到网络接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53556884/

相关文章:

python-2.7 - 安装 Numeric(旧 numpy)和旧版本的 Python(例如 Py 2.4)

python - 非常基本的 Python 文本冒险

python - Bottle 框架 : how to return datetime in JSON response

python - Spark - 操作数据框中的特定列值(删除字符)

python子进程一个一个运行

python - PyMySQL 和 OrderedDict

ssl - Cnames 和 ssl - 如果两个名称都有 ssl 好吗?

perl - 是否有支持 SSL/TLS 的核心 Perl 模块?

适用于 Windows 7 的 Python GObject 自省(introspection)

ssl - nginx ssl 拒绝不匹配的 server_name 请求