本当に単純な Python Web サービスを実行しようとしています。Flickr API を使用して「photo.search」を作成し、タイトルと写真の URL を返します。
この Web サービスは、Google App Engine でホストされます。
これは私が実際に今持っているものです:
import webapp2
from urllib import urlencode, urlopen
from xml.dom import minidom
from google.appengine.api import urlfetch
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
data = _doget('flickr.photos.search', text='to_search', per_page='2')
self.response.write('Ok')
def _prepare_params(params):
for (key, value) in params.items():
if isinstance(value, list):
params[key] = ','.join([item for item in value])
return params
def _doget(method, auth=False, **params):
params = _prepare_params(params)
url = 'http://flickr.com/services/rest/?api_key=%s&method=%s&%s%s'% \
('stackoverflow_key', method, urlencode(params),
_get_auth_url_suffix(method, auth, params))
result = urlfetch.fetch(url)
minidom.parse(result.content)
return result
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
私は Python の初心者なので、大きな間違いをした場合は申し訳ありません :) いくつかのチュートリアルとサンプル コードを試しましたが、うまくいきませんでした。
インポートで 500 サーバー エラーが発生しています
Error: Server Error
The server encountered an error and could not complete your request.
If the problem persists, please report your problem and mention this error message and the query that caused it.
何が問題なのか誰にも教えてもらえますか?
誰かがトリックを行ったサンプルコードを持っていれば、いいかもしれません.
お時間をいただきありがとうございます。:D
編集 :
わかりました、コードを変更しました。テスト用に、以前よりもはるかに簡単になりました。
import webapp2
from urllib import urlencode, urlopen
from xml.dom import minidom
from google.appengine.ext.webapp.util import run_wsgi_app
import hashlib
import os
HOST = 'http://api.flickr.com'
API = '/services/rest'
API_KEY = 'my_key'
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
data = _doget('flickr.photos.search', auth=False, text='to_search', per_page='1')
if data:
self.response.write(data)
else:
self.response.write('Error')
def _doget(method, **params):
url = '%s%s/?api_key=%s&method=%s&%s&format=json'% \
(HOST, API, API_KEY, method, urlencode(params))
res = urlfetch.fetch(url).content
# Flickr JSON api returns not valid JSON, which wrapped with "jsonFlickrApi(...)", so we get rid of it.
if 'jsonFlickrApi(' in res:
return res[14:-1]
return json.loads(res)
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
この URL をコピーして貼り付けると、完全に機能します。私の目的は、flickr が返すデータを返すことです。しかし、まだ機能していません。プリントも表示されません:(