我不确定为什么在向表单提交任何查询时,在 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.