1

Google AppEngine でこの単純なコードをホストすると、クエリがフォームに送信されたときにサーバー エラーが返される理由がわかりません。コードがなくても問題なく動作するため、問題は html = urllib2.urlopen(" http://google.com/search?q= " + q).read() 行にあるようです。

import webapp2
import urllib2


form="""
<form action="/process">
    <input name="q">
    <input type="submit">
</form>
"""


class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)


class ProcessHandler(webapp2.RequestHandler):
    def get(self):
        q = self.request.get("q")
        html = urllib2.urlopen("http://google.com/search?q=" + q).read()
        self.response.out.write(html)


app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/process', ProcessHandler)],
                               debug=True)

これは返されるエラーです:

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.
4

2 に答える 2

1

おそらく www.google.com は、この種の直接接続を受け入れず、特定のユーザー エージェントからの接続をキャンセルします。単純な python 環境では、ユーザー エージェント文字列を変更できますが、Google アプリ エンジンを介してそれを行うことはできないと思います。

于 2013-10-19T13:24:53.417 に答える