It is common practice for organizations and companies to make use of proxy servers to connect to services outside their network. This can cause problems for users of the ChEMBL web services who sit behind a proxy server. So to help those users who have asked, we provide the following quick guide, which demonstrates how to access ChEMBL web services via a proxy.
Most software libraries respect proxy settings from environmental variables. You can set the proxy variable once, normally HTTP_PROXY and then use that variable to set other related proxy environment variables:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export HTTP_PROXY="http://10.10.1.10:3128" | |
export HTTPS_PROXY=$HTTP_PROXY |
Or if you have different proxies responsible for different protocols:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export HTTP_PROXY=10.10.1.10:3128 | |
export HTTPS_PROXY=10.10.1.11:1080 | |
export FTP_PROXY=10.10.1.10:3128 |
On Windows, this would be:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set http_proxy=10.10.1.10:3128 | |
set https_proxy=10.10.1.11:1080 | |
set ftp_proxy=10.10.1.10:3128 |
If you are accessing the ChEMBL web services programmatically and you prefer not to clutter your environment, you can consider adding the proxy settings to your scripts. Here are some python based recipes:
1. Official ChEMBL client library
If you are working in a python based environment, we recommend you to use our client library (chembl_webresource_client), for accessing ChEMBL web services. It already offers many advantages over accessing the ChEMBL web services directly and handling proxies is yet another. All you need to do is configure proxies once and you are done:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python modules used for API access... | |
from chembl_webresource_client import * | |
from chembl_webresource_client.settings import Settings | |
# Set proxies: | |
Settings.Instance().PROXIES = { | |
"http" : "http://10.10.1.10:3128", | |
"https" : "https://10.10.1.11:1080", | |
"ftp" : "ftp://10.10.1.10:3128" | |
} | |
# Then use the client normally: | |
compounds = CompoundResource() | |
record = compounds.get('CHEMBL554') |
2. Python requests library
If you decide to use requests, you have to add 'proxies' parameter to every 'get' and 'post' function call:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
chembl_url = "https://www.ebi.ac.uk/chemblws/compounds/{}" | |
proxyDict = { | |
"http" : "http://10.10.1.10:3128", | |
"https" : "https://10.10.1.11:1080, | |
"ftp" : "ftp://10.10.1.10:3128" | |
} | |
r = json.load(requests.get(chembl_url.format('CHEMBL554'), proxies=proxyDict))[u'compounds'] |
3. Python urllib2 library
Finally, in the lowest level library, 'urllib2' you can set a ProxyHandler and register it to URL opener:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import json | |
import proxy_opener | |
chembl_url = "https://www.ebi.ac.uk/chemblws/compounds/{}" | |
proxy = urllib2.ProxyHandler({'http': 'http://myuserid:mypassword@myproxyserver:port','https':'http://myuserid:mypassword@myproxyserver:port'}) | |
opener = urllib2.build_opener(proxy) | |
urllib2.install_opener(opener) | |
tmp = json.load(urllib2.urlopen(chembl_url.format('CHEMBL554')))[u'compounds'] | |
We would like to thank Dr. Christine Rudolph for the idea and providing code snippets.
Comments