open url from pythonanywhere

Posted on

Question :

open url from pythonanywhere

This code works well on my local machine, but when I upload and run it on it gives me this error.

My Code:

url = ""
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
                'Accept-Encoding': 'none',
                'Accept-Language': 'en-US,en;q=0.8',
                'Connection': 'keep-alive'}
         req = urllib2.Request(url, headers=hdr)
         opener = urllib2.build_opener()
         openedReq =, timeout=300)

The error:

Traceback (most recent call last):
File "/home/GehadAbdallah/", line 135, in openApi
    openedReq =, timeout=300)
  File "/usr/lib/python2.7/", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

P.S. i’m working on python 2.7

Answer #1:

Free accounts on PythonAnywhere are restricted to a whitelist of sites, http/https only, and access goes via a proxy. There’s more info here:

PythonAnywhere wiki: “why do I get a 403 forbidden error when opening a url?”

Answered By: hwjp

Answer #2:

I recently used urllib2 with a flask project on pythonanywhere using their free account to access an api at

This might be helpful,

def fundingByState():
    urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler({'http': 'proxy.server:3128'})))
    donors_choose_url = ""
    response = urllib2.urlopen(donors_choose_url)
    json_response = json.load(response)
    return json.dumps(json_response)

This does work.

Answered By: Shane G

Answer #3:

If you’re using paid account but still get this error message
try this pythonanywhere_forums

To me, I have to delete the console then restart a new one.

Answered By: Windsooon

Leave a Reply

Your email address will not be published.