Python & GAE - StringType write() 参数类型解析



运行dev_appserver.py .时,尝试访问http://localhost:8080时出现以下错误:

Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
self.write(data)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions 2.7/lib/python2.7/wsgiref/handlers.py", line 204, in write
assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string

我已经搜索过了,它似乎回到了我的app.yaml文件,根据这些链接:

  • GAE断言错误的SO问题
  • 俄语问题网站相同的信息

我只是不知道如何调试它。下面是我的app.yaml文件和main.py文件。我是GAE平台的新手,任何帮助都将不胜感激。

app.yaml文件:

application: gqtimer
version: 1-56
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /favicon.ico
static_files: static/images/favicon.ico
upload: static/images/favicon.ico
- url: /_ah/login_required
script: main.py
- url: /static
static_dir: static
- url: /player.*
script: main.py
login: required
- url: /stat.*
script: main.py
login: required
- url: .*
script: main.py
libraries:
- name: django
version: "1.11"

main.py文件:

#!/usr/bin/env python
#
import config
import os
import sys
# Force sys.path to have our own directory first, so we can import from it.
sys.path.insert(0, config.APP_ROOT_DIR)
sys.path.insert(1, os.path.join(config.APP_ROOT_DIR, 'externals'))
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.dist import use_library
use_library('django', '1.2')

from handlers import error, timer, do_openid_login

def main():
application = webapp.WSGIApplication([('/', timer.ExportHandler),
('/_ah/login_required', do_openid_login.OpenIdLoginHandler),
('/player/([-w]+)', timer.PlayerHandler),
('/player/([-w]+)/archives', timer.ArchivesHandler),
('/stat/([-w]+)', timer.StatHandler),
('/stat/([-w]+)/delete', timer.StatDeleteHandler),                                          
# If we make it this far then the page we are looking
# for does not exist
('/.*', error.Error404Handler),
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
main()

实际上,app.yaml文件可能没有正确映射应用程序代码。您需要:

  • application变量置于main()函数之外,使其成为main.py模块中的全局变量(也可以将其重命名为app,只是为了与官方惯例和文档示例保持一致(
  • app.yamlhandlers中的script: main.py语句替换为main.application(或main.app,如果如上所述重命名(-这是对上述全局变量的引用。从Handlers元素表中的script行:

脚本:指令必须是python导入路径,例如,指向WSGI应用程序的package.module.app。使用Python模块路径的脚本:指令的最后一个组件是模块中全局变量的名称:该变量必须是WSGI应用程序,通常按惯例称为应用程序

我还建议显式地将app.yaml作为参数传递给dev_appserver.py,而不是应用程序的目录(在您的情况下为.(-偶尔自动检测的行为不会像预期的那样。这也是运行多个服务和/或使用dispatch.yaml文件进行路由的唯一方法,因此这是一个好习惯。

相关内容

最新更新