in Programming


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.


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;;

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'


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('', 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.


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("", proxies=proxies)


… 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)
conn = urllib2.urlopen('')
return_str =


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