2016-04-18 12 views
1

Google App Engine(gae)にPythonコードがあり、Thomson Reuters Open Calais APIをリクエストしています。データとしてプレーンテキストを送信します。テキストに対応するタグを持つJSONオブジェクト。残念ながら、私は取得していますすべてのエラー500は、GAEのコンソールに次のエラーメッセージとともに、次のとおりです。(Thomson Reuters Open Calais APIにPythonリクエストを提出する方法

enter image description here

上記のエラーがWERKZEUGで場所を取るように見える、PythonのWSGIユーティリティライブラリであるものは何でもWSGIは意味する!)。エラーは117 here行で発生するので、これは文字セットがないことを意味しますが、私はPythonコードのどこで文字セットを設定すべきかわかりません。

この問題を解決するお手伝いがあれば、本当にありがたく思います。ここで

は私のPythonコードです:

from google.appengine.api import urlfetch 
from flask import Flask, request 

app = Flask(__name__) 

calais_url = "https://api.thomsonreuters.com/permid/calais" 

@app.route('/', methods=['POST']) 
def main(): 
    data = "The 'In' camp holds a one-point lead ahead of Britain's June 23 referendum on whether the country should remain in the European Union, according to an online opinion poll by YouGov released on Friday.The poll found support for 'In' stood at 40 percent, while 39 percent intended to vote 'Out', 16 percent were undecided and 5 percent did not intend to vote.The poll of 3,371 people was conducted between April 12 and 14 and the results were similar to those seen in other recent YouGov polls. The previous one, conducted on April 11-12, found 'In' and 'Out' were tied on 39 percent with 17 percent undecided." 
    headers = {'X-AG-Access-Token' : 'my_secret_key', 'Content-Type' : 'text/raw', 'outputFormat' : 'application/json', 'omitOutputtingOriginalText' : 'true'} 
    try: 
     sendArticleText(data, headers) 
    except Exception ,e: 
     return 'Error in connect ' , e 

def sendArticleText(_data, _headers): 
    response = urlfetch.fetch(calais_url, payload=_data , method=POST, headers=_headers, deadline=60) 
    content = response.text 
    if response.status_code == 200: 
     return content.json() 

@app.errorhandler(404) 
def page_not_found(e): 
    """Return a custom 404 error.""" 
    return 'Sorry, Nothing at this URL.', 404 

@app.errorhandler(500) 
def page_not_found(e): 
    """Return a custom 500 error.""" 
    return 'Sorry, unexpected error:\n{}'.format(e), 500 

は、事前にありがとうございます。

答えて

1

これは、mainからタプルを返すために発生します。ビュー関数がタプルを返す場合、フラスコはタプルの第2メンバが例外値ではなくhttpステータスコードであると予測します。これは、あなたが本当のエラーを見ることができるようになります

return 'Error in connect: {}'.format(e) 

:代わりに

return 'Error in connect ' , e 

ライン15の

は次のようなものでなければなりません。

+0

あなたは@notapresentありがとうございます。あなたのヒントは非常に役立ちます。どうやら、urlfetchによって返されるレスポンスには 'text'属性がありません! –

+0

コンテンツの代わりに直接応答を返すようにsendArticleText()関数を変更しました。これで、gaeコンソールに次のエラーメッセージが表示されます。[http:// http:// http://www.windowsvista.com/ ://imgur.com/0JxCrHc)。これを引き起こしていることはわかりません。 –

0

最終的には動作しました!

Google Cloud PlatformのShobhitに感謝し、彼らの助けを借りてくれません。ここで

は、最終的なコードです:

from google.appengine.api import urlfetch 
from flask import Flask, request 

app = Flask(__name__) 

calais_url = "https://api.thomsonreuters.com/permid/calais" 
headers = {'X-AG-Access-Token' : 'my_secret_key', 'Content-Type' : 'text/raw', 'outputFormat' : 'application/json', 'omitOutputtingOriginalText' : 'true'} 

def sendArticleText(_data, _headers): 
    response = urlfetch.Fetch(calais_url, payload=_data , method=urlfetch.POST, headers=_headers, deadline=80) 
    content = response.content 
    return content 

@app.route('/', methods=['POST']) 
def main(): 
    request.get_data() 
    article = request.data 
    try: 
     result = sendArticleText(article, headers) 
    except Exception ,e: 
     return 'Error in connect: {}'.format(e) 
    return result 

@app.errorhandler(404) 
def page_not_found(e): 
    """Return a custom 404 error.""" 
    return 'Sorry, Nothing at this URL.', 404 

@app.errorhandler(500) 
def page_not_found(e): 
    """Return a custom 500 error.""" 
    return 'Sorry, unexpected error:\n {}'.format(e), 500 
関連する問題