in Programming

Proxy

Using a Mac behind a proxy is a no issue if you use native applications.

Everything changes when you try to use bash scripts or Python code, so I decided to summarize here (as a memo for a ‘future me’) how is possible to use a proxy in different scenarios.

Bash

If you want to use a proxy in bash command line or script, just add this lines to the ‘.bash_profile’ file in your home directory:

case $HOSTNAME in
    (MYHOSTNAME) 	export http_proxy=http://user:pwd@host:port;
				export https_proxy=$http_proxy;
                  		export HTTP_PROXY=$http_proxy;
                  		export HTTPS_PROXY=$http_proxy;
  		  		apm config set http-proxy $http_proxy;
		  		apm config set https-proxy $http_proxy;;
esac

As you can see all you need to do is define several variables for https and https.
I haven’t find any technical explanation but I found useful define the variables both uppercase and lowercase.

Then I also configure apm (for the famous NodeJs packet manager).

I decided also tu use a case to configure the proxy only on a particular machine (my Mac at work), so I have just one ‘.bash_profile’ to maintain for every Mac.

PIP behind a proxy

If you use PIP (the most famous Python package manager), you have some chance that it reads the $http_proxy variable but if you want you can force it by the command line:

sudo pip --proxy=http://user:pwd@host:port install awscli

You can also create an alias and put it in the same case as above:

alias='pip --proxy=http://user:pwd@host:port'

REQUESTS module

Now, let’s talk about python modules.
The most famous module for dealing with http requests is ‘requests’.

So, here how you can use it:

proxies = {'https':'https://user:pwd@host:port','http':'http://user:pwd@host:port' }
resp = requests.get('http://www.google.com', proxies=proxies).json()

Just define a dictionary with protocols as ket and proxies as values, then pass it to the call.
In this case it will return a JSON.

URLLIB

Another fundamental library to handle http requests i ‘urllib’, and that’s the code:

import urllib
proxies = {'https':'https://user:pwd@host:port','http':'http://user:pwd@host:port' }
urllib.urlopen("http://www.google.com", proxies=proxies)

URLLIB2

… the same with urllib2, different syntax, same output

import urllib2
proxies = {'https':'https://user:pwd@host:port','http':'http://user:pwd@host:port' }
proxy = urllib2.ProxyHandler(proxies)
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://www.google.com')
return_str = conn.read()

NOTES

1 – As you can see all the required modules use a dictionary with the same syntax, useful if you have to mix different calls to different libraries.

2 – If you want to use the proxy only on a specified hostname (like I do in the ‘.bash_profile’), in Python you can get the hostname using:

import socket
socket.gethostname().strip()