Forums

urllib2 not working on free service

Hi,

Is there any reason that the free PythonAnywhere account would not allow urllib2? I am trying to grab data from the donorschoose.org api and graph it using DC.js.

The site works fine locally on my machine. The project is at https://github.com/shanegibney/donorsChooseCIproject3.

The site is deploying on pythonanywhere, I can see the homepage but no graphs are inserted.

Site at http://shane.pythonanywhere.com/

You can read about the whitelist and why we have it here

api.donorschoose.org looks like an excellent candidate for inclusion in the whitelist. So I have added it for you.

Be aware that you may need to modify your code to use the proxy instead of trying to make a direct request.

Hi Glenn,

Thanks for including the donorschoose.org api.

I am not sure how to modify the code to use the proxy, but I will look into it.

Thanks for your help,

Shane

PS please delete the spam post nehaSINGH posted above, thanks

Useful information about proxies here

https://help.pythonanywhere.com/pages/403ForbiddenError/

This might be useful for someone,

urllib2.install_opener(
    urllib2.build_opener(
        urllib2.ProxyHandler({'http': '127.0.0.1'})
    )
)
urllib2.urlopen('http://www.google.com')

found on Stackoverflow http://stackoverflow.com/questions/1450132/proxy-with-urllib2

Excellent, this works great with flask,

@app.route('/funding')
def fundingByState():
    urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler({'http': 'proxy.server:3128'})))
    donors_choose_url = "http://api.donorschoose.org/common/json_feed.html?historical=true&APIKey=DONORSCHOOSE"
    response = urllib2.urlopen(donors_choose_url)
    json_response = json.load(response)
    return json.dumps(json_response)

Thanks

Great! Many thanks for the code example, hopefully others will find it useful.