Python requests through VPN giving 502 Bad Gateway

Question:

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)

I have both HTTP_PROXY and HTTPS_PROXY set in my environment. I am running Windows 7.

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.

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)

This still led to the same error (502) as above.

EDIT: 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.

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”.

I can ping that IP from the command line no problems.

Asked By: James Elderfield

||

Answers:

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)
Answered By: James Elderfield