0

我正在尝试制作一个基本的 python 应用程序来自学 Google App Engine。我有以下2个文件。

你好世界.py:

import cgi
import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("""
            <html>
            <body>
            <form action="/sign" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Sign Guestbook"></div>
            </form>
            </body>
            </html>""")


class Guestbook(webapp2.RequestHandler):
    def post(self):
        self.response.out.write('<html><body>You wrote:<pre>')
        self.response.out.write(cgi.escape(self.request.get('content')))
        self.response.out.write('</pre></body></html>')

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/sign', Guestbook)],
                              debug=True)

应用程序.yaml

application: helloworld
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: helloworld.py

但是,当我运行此应用程序并在 Google Chrome 中访问 localhost:8080 时,会显示一个空白页面,而不是我期望的表单。为什么?我的日志中显示了 200 条回复。

*** Running dev_appserver with the following flags:
    --admin_console_server= --port=8080
Python command: /usr/bin/python2.7
/Users/davidfaux/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/search/search.py:232: UserWarning: DocumentOperationResult._code is deprecated. Use OperationResult._code instead.
  'Use OperationResult.%s instead.' % (name, name))
/Users/davidfaux/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/search/search.py:232: UserWarning: DocumentOperationResult._CODES is deprecated. Use OperationResult._CODES instead.
  'Use OperationResult.%s instead.' % (name, name))
WARNING  2012-06-21 04:30:38,064 rdbms_mysqldb.py:74] The rdbms API is not available because the MySQLdb library could not be loaded.
Warning: You are using a Python runtime (2.7) that is more recent than the production runtime environment (2.5). Your application may use features that are not available in the production environment and may not work correctly when deployed to production.
WARNING  2012-06-21 04:30:38,204 dev_appserver.py:3423] Could not initialize images API; you are likely missing the Python "PIL" module. ImportError: No module named _imaging
INFO     2012-06-21 04:30:38,220 dev_appserver_multiprocess.py:647] Running application dev~helloworld on port 8080: http://localhost:8080
INFO     2012-06-21 04:30:38,221 dev_appserver_multiprocess.py:649] Admin console is available at: http://localhost:8080/_ah/admin
INFO     2012-06-21 04:31:00,561 dev_appserver.py:2904] "GET / HTTP/1.1" 200 -
INFO     2012-06-21 04:31:00,758 dev_appserver.py:2904] "GET /favicon.ico HTTP/1.1" 200 -
INFO     2012-06-21 04:31:02,091 dev_appserver.py:2904] "GET / HTTP/1.1" 200 -
INFO     2012-06-21 04:31:02,180 dev_appserver.py:2904] "GET /favicon.ico HTTP/1.1" 200 -
INFO     2012-06-21 04:31:04,580 dev_appserver.py:2904] "GET /favicon.ico HTTP/1.1" 200 
4

2 回答 2

1

由于您使用的是 python 2.5。您缺少以下内容:

from google.appengine.ext.webapp.util import run_wsgi_app
...
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

有关详细信息,请参阅入门指南

于 2012-06-21T20:36:02.437 回答
-1

可能是您缺少 HTTP 标头。在您的请求处理程序中添加:

self.response.headers['Content-Type'] = 'text/plain'
于 2012-06-21T20:24:03.900 回答