Python应用程序表现异常,是否缓存了URL参数?

Cha*_*had 2 python google-app-engine

我今天第一次尝试使用Google AppEngine和Python,并设法运行一个简单的示例.它工作正常但发生了一些奇怪的事情:当URL参数值发生变化时,除非我重新启动应用程序,否则它不会被注册.

在下面的示例中,如果我运行:http:// localhost:8080 /?x = hello它将返回'x is hello',但如果我更改X的值,则其新值不会影响输出.

我怀疑有某种内部缓存正在进行,但我不确定.

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

import cgi
form = cgi.FieldStorage()
x = form.getvalue('x')

class MainHandler(webapp.RequestHandler):
    def get(self):
        if x == 'hello':
            self.response.out.write('x is hello')
        else:
            self.response.out.write('x is not hello')

def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

Dan*_*man 5

您将在模块级代码中获取处理程序之外的表单值.显然,这是在第一次请求时首次加载模块时定义的.你应该在get方法中做这个.

  • 实际上,您不希望在webapp(或任何其他框架)中使用`cgi`模块.该框架提供了获取GET变量的方法; 使用它们. (3认同)