문제

I am working on a django app which fetches data from facebook graph api and plots a network graph of it. The app runs fine locally but it gives me an error No JSON object could be decoded when I host it on pythonanywhere. Also, being a newbie to pythonanywhere I don't know how to print the errors on console.

Here is the code:

import urllib,json
from django.shortcuts import render
from main.forms import InputForm
from django.http import HttpResponseRedirect

def main(request):
    if request.method == 'POST': # If the form has been submitted
        input_form = InputForm(request.POST)
        if input_form.is_valid():
            cd = input_form.cleaned_data
            actoken = cd['access_token']
            keyword = cd['keyword']
            url = "https://graph.facebook.com/search?q="+keyword+"&type=post&fields=likes.fields(name).limit(10),comments,message&limit=5&access_token="+actoken
            fetch = urllib.urlopen(url).read()
            print fetch # don't know the response
            data = json.loads(fetch)
            return HttpResponseRedirect('/graph/')
        else:
            input_form = InputForm(request.POST)
            return render(request, 'index.html', {
                'input_form': input_form,
            })
    else:
        input_form = InputForm()
        return render(request, 'index.html', {
            'input_form': input_form,
        })

Here is the stack trace:

Traceback:

File "/home/architv/.virtualenvs/django16/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/architv/fb-network-graph/fbnetworkgraph/main/views.py" in main
  15.             data = simplejson.loads(fetch)
File "/usr/lib/python2.7/json/__init__.py" in loads
  338.         return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py" in decode
  365.         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py" in raw_decode
  383.             raise ValueError("No JSON object could be decoded")

Exception Type: ValueError at /main/
Exception Value: No JSON object could be decoded

EDIT: I went through the server log file as suggested by glenn and found this:

2014-04-05 09:22:22 https://graph.facebook.com/search?q=ukraine&type=post&fields=likes.fields(name).limit(10),comments,message&limit=5&access_token=CAACEdEose0cBAIhZBJSXHTHOoGZCFZC2xbPDNuNKX9NIksZBMQudtzN2A7o4op5ZBkOhYTZAS6JFcn5lQMNY94nhntZAhmA4DZAWuVg0wqHVHI3VQToq9yhHeQZBL75BgaPMNIbUj9XKQ7ICKJeM3R5YAShXjBxPZAcmbfmxcARFQZA0UjartHP3lWpWCR2wRjJ8WwZD
2014-04-05 09:22:22 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ERROR: The requested URL could not be retrieved</title> <style type="text/css"><!--   /*#012 Stylesheet for Squid Error pages#012 Adapted from design by Free CSS Templates#012 http://www.freecsstemplates.org#012 Released for free under a Creative Commons Attribution 2.5 License#012*/#012#012/* Page basics */#012* {#012#011font-family: verdana, sans-serif;#012}#012#012html body {#012#011margin: 0;#012#011padding: 0;#012#011background: #efefef;#012#011font-size: 12px;#012#011color: #1e1e1e;#012}#012#012/* Page displayed title area */#012#titles {#012#011margin-left: 15px;#012#011padding: 10px;#012#011padding-left: 100px;#012#011background: url('http://www.squid-cache.org/Artwork/SN.png') no-repeat left;#012}#012#012/* initial title */#012#titles h1 {#012#011color: #000000;#012}#012#titles h2 {#012#011color: #000000;#012}#012#012/* special event: FTP success page titles */#012#titles ftpsuccess {#012#011backgro
2014-04-05 09:22:22   body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; }  --></style> </head><body id=ERR_UNSUP_REQ> <div id="titles"> <h1>ERROR</h1> <h2>The requested URL could not be retrieved</h2> </div> <hr>  <div id="content"> <p>The following error was encountered while trying to retrieve the URL: <a href="https://graph.facebook.com/search?">https://graph.facebook.com/search?</a></p>  <blockquote id="error"> <p><b>Unsupported Request Method and Protocol</b></p> </blockquote>  <p>Squid does not support all request methods for all access protocols. For example, you can not POST a Gopher request.</p>  <p>Your cache administrator is <a href="mailto:webmaster?subject=CacheErrorInfo%20-%20ERR_UNSUP_REQ&amp;body=CacheHost%3A%20glenn-liveproxy1%0D%0AErrPage%3A%20ERR_UNSUP_REQ%0D%0AErr%3A%20%5Bnone%5D%0D%0ATimeStamp%3A%20Sat,%2005%20Apr%202014%2009%3A21%3A47%20GMT%0D%0A%0D%
도움이 되었습니까?

해결책

There is an incompatibility between some Python url libraries and the squid proxy that we use to filter free account internet access.

Try using requests (I believe the bug is fixed there) or urllib3 instead of urllib.

다른 팁

Elementary, you can't access to Facebook through your Squid configuration and, instead to get a JSON stream, you get an HTML Squid error page. That's all.

This seems to be a limitation of the basic PythonAnywhere accounts:

I suspect you can't do a POST via HTTPS. There are quite a few limitations when connecting through the proxy server. The error message is from the proxy and it seems quite clear.

Hacker accounts have unrestricted internet access which doesn't go through the proxy service at all.

...

There's no way around the proxy unless you have a paid account, that's correct. You can upgrade your account and test it with no risk - if you find it doesn't work and you downgrade again within 30 days you can ask for a full refund of your first month's cost.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top