简体   繁体   English

Python无法将请求绑定到网络接口

[英]Python cannot bind requests to network interface

I cannot use request proxy to connect to https with my local ip 192.168.1.55 is my local ip 我无法使用请求代理通过本地IP 192.168.1.55连接到https是我的本地IP

if i hide the https line for proxy, it works, but i know that it is not actually using the proxies 如果我隐藏了代理的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

Result: Response [200] 结果:响应[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

Result: 结果:

requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error('Tunnel connection failed: 400 Bad Request',))) requests.exceptions.ProxyError:HTTPSConnectionPool(host ='www.google.com',端口= 443):url超过了最大重试次数:/(由ProxyError((无法连接到代理。)引起,错误('隧道连接失败: 400错误的请求',)))

I also tried external VPN server which support HTTPS protocol, even the https proxy line is un-hide, it will work 我还尝试了支持HTTPS协议的外部VPN服务器,即使https代理行未隐藏,它也能正常工作

I have multiple IP and would like to use specified ip with the request. 我有多个IP,并且想在请求中使用指定的ip。 I am running it on Win 10 with python 2.7, and I suspect it is due to the SSL problem, yet to confirm with expertise here. 我正在python 2.7的Win 10上运行它,我怀疑这是由于SSL问题引起的,请在此处进行确认。 (i think i didnt deal with the SSL properly) I tried a lot of ways to deal with the SSL, no luck so far. (我认为我没有正确处理SSL)我尝试了许多方法来处理SSL,但到目前为止还没有碰到运气。

you can try this to bind requests to selected adapter/IP but first install requests_toolbelt 您可以尝试使用此方法将requests绑定到选定的适配器/ IP,但首先安装requests_toolbelt

pip install requests_toolbelt

then 然后

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM