Corporate proxy and connect using urllib

There are instances when you are restricted to always use proxy servers to connect to internet. Let’s assume a need to distribute some code that will read a proxy user and password from a on-prem corporate address and then use it to access any sites on the internet. This is a valid scenario in corporate world.

Today we will write a very short blog post to address this scenario. This will be written in python 3.x. This is more of a code reference for me than a real blog.

The Code

Anyway, let’s start the drill. In this case we are also assuming no access to pip – so we will not use any third party libraries like requests. We will be just using the built-ins.

from urllib.request import urlopen, Request, ProxyHandler, build_opener, install_opener
from urllib.error import HTTPError

PROXY_HOST = "web.proxy.com"
PROXY_USER = "my%40email.com"
PROXY_PASS = ""

def functionUsingProxy(url2call):
	http_request = Request(url2call, headers={ "Accept" : "application/json" })
	host_ep = PROXY_USER + ":" + PROXY_PASS + "@" + PROXY_HOST
	proxy_support = ProxyHandler({ "http" : host_ep })
	opener = build_opener(proxy_support)
	install_opener(opener)
	print("Proxy setup complete...")
	try:
		with urlopen(http_request) as response:
			print(response.status)
			print(response.read().decode())
		except HTTPError as e:
			print(e.status)
			print(e.reason)
			print(e.headers.get_content_type())

So, here I have put the proxy host, user and password as constants. But ideally you can take it from an accessible place (maybe encrypted in DB). The first thing we do is to create an opener and subsequently install it (Line #13). With this in place, every call will use the proxy to make http calls.

Conclusion

As promised, this is a very short blog. This small piece of code can be used whenever a proxy is needed for connecting to the internet APIs. Ciao for now!