2012-06-21 87 views
0

我正在嘗試製作一個基本的python應用程序來教導我自己的Google App Engine。我有以下2個文件。如何在我的Google App Engine應用程序中顯示空白文件?

helloworld.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) 

的app.yaml

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

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

然而,當我運行該應用程序並訪問本地主機: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 

回答

-1

可能是因爲缺少HTTP標頭。將此添加到您的請求處理程序中:

self.response.headers['Content-Type'] = 'text/plain' 
1

由於您使用的是python 2.5。您缺少以下內容:

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

if __name__ == "__main__": 
    main() 

有關詳細信息,請參閱Getting Started Guide

+0

謝謝,但我的Python解釋器讀取2.7時,我打開它在終端。 – dangerChihuahua007

+0

但是你的app.yaml將解釋器設置爲2.5,因爲行運行時:python。如果你想使用python 2.7,你需要指定runtime:python27,並且改變腳本行。請參閱本指南https://developers.google.com/appengine/docs/python/python27/using27 –

+0

腳本行應更改爲腳本:helloworld.app。現在你看到空白頁面,因爲它正在執行文件作爲CGI,並且由於沒有什麼可執行的(基本上沒有主要的),你沒有輸出和200 OK。 –

相關問題