问题描述
我正在尝试使用requests
库从公司网络内部的服务器中获取一些数据.我正在使用VPN进入公司的网络,并且还建立了公司代理.我尝试访问的地址仅在该公司网络内可见.
I'm trying to grab some data from a server inside my company's network with the requests
library. I'm using a VPN to get into my company's network and there is also a corporate proxy set up. The address I am trying to access is only visible from within that company network.
import requests
url = "http://some.private.server.net"
r = requests.get(url)
我的环境中同时设置了HTTP_PROXY和HTTPS_PROXY.我正在运行Windows 7.
I have both HTTP_PROXY and HTTPS_PROXY set in my environment. I am running Windows 7.
我从此代码中收到错误502.当我物理连接到公司网络时,它就起作用了,所以我认为问题在于通过VPN而不是通过代理进行连接.我可以使用网络浏览器访问该地址.
I get error 502 from this code. It worked when I was physically connected to the company network so I think the problem is to do with connecting through the VPN rather than the proxy. I can access the address with my web browser.
我对网络了解不多,但是我尝试将请求的源地址更改为VPN网络适配器使用的IP地址.
I don't know much about networking but I tried changing the source address for the requests to be the IP address used by the VPN network adapter.
import requests
from requests_toolbelt.adapters.source import SourceAddressAdapter
s = requests.Session()
ip = "y.y.y.y"
s.mount('http://', SourceAddressAdapter(ip))
s.mount('https://', SourceAddressAdapter(ip))
url = "http://some.private.server.net"
response = s.get(url)
这仍然导致与上述相同的错误(502).
This still led to the same error (502) as above.
更仔细地查看502错误,并且代理服务器返回了消息正在查找的页面的主机名不存在".
Looked more closely at the 502 error and the message "the host name of the page you are looking for does not exist" is being returned by the proxy server.
因此,我尝试将URL替换为尝试访问的服务器的IP地址,但由于网关超时"而获得了502.
Therefore I tried replacing the URL with the IP address of the server I'm trying to reach and instead get a 502 due to "Gateway Timeout".
我可以从命令行ping该IP没问题.
I can ping that IP from the command line no problems.
推荐答案
具有讽刺意味的是,问题是我不需要使用代理来访问该服务器.您可以使用
Ironically the issue was that I needed to not be using the proxy to access this server. You can do this with,
import requests
s = requests.Session()
s.trust_env = False
url = "http://some.private.server.net"
response = s.get(url)
这篇关于通过VPN的Python请求提供了502错误的网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!