问题描述
我想在我的GAE代码中使用 main()
函数(注意:下面的代码仅仅是一个更大程序的最小范例,因此需要 main()
)。
如果我使用下面的代码,它按预期执行:
import webapp2
class GetHandler(webapp2.RequestHandler):
def get self):
self.response.headers ['Content-Type'] ='text / plain'
self.response.write('in GET')
class SetHandler (webapp2.RequestHandler):
def get(self):
self.response.headers ['Content-Type'] ='text / plain'
self.response.write('in SET')
app = webapp2.WSGIApplication([
('/ get',GetHandler),
('/ set',SetHandler),
], debug = True)
其中 app.yaml
是:
运行时:python27
api_version:1
threadsafe:true
处理程序:
- url:/.*
script:main.app
然而,我不知道如何实现 main()
函数,并且仍然有 app
就像它在顶部的代码中一样。即,以下内容:
#with main()
import webapp2
class GetHandler (webapp2.RequestHandler):
def get(self):
self.response.headers ['Content-Type'] ='text / plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers ['Content-Type'] ='text / plain '
self.response.write('in SET')
def main():
app = webapp2.WSGIApplication([
('/ get', GetHandler),
('/ set',SetHandler),
],debug = True)
if __name__ =='__main__':
main()
给出 http:// localhost:8080 / get:
$ dev_appserver.py。
INFO 2016-10-17 11:29:30,962 devappserver2.py:769]跳过SDK更新检查。
INFO 2016-10-17 11:29:31,059 api_server.py:205]启动API服务器:http:// localhost:45865
INFO 2016-10-17 11:29:31,069调度程序。 py:197]启动模块default运行在:http:// localhost:8080
INFO 2016-10-17 11:29:31,073 admin_server.py:116]启动管理服务器:http:// localhost :8000
错误2016-10-17 11:29:37,461 wsgi.py:263]
追溯(最近一次调用最后一次):
文件/home/.../sdk/平台/ google_appengine / google / appengine / runtime / wsgi.py,第240行,处理
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
文件/home/.../sdk /platform/google_appengine/google/appengine/runtime/wsgi.py,第302行,位于_LoadHandler
引发错误
ImportError:< module'main'from'/home/.../main。 PYC'>没有属性app
INFO 2016-10-17 11:29:37,496 module.py:788] default:GET / get HTTP / 1.1500 -
编辑1
尝试:
<$ p ()
导入webapp2
app = webapp2.RequestHandler()
$ b $ class GetHandler(webapp2.RequestHandler) :
def get(self):
self.response.headers ['Content-Type'] ='text / plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers ['Content-Type'] ='text / plain'
self.response.write('in SET')
def main():
全局应用程序
app = webapp2.WSGIApplication([
('/ get' ,GetHandler),
('/ set',SetHandler),
],debug = True)
返回应用程序
if __name__ =='__main__':
app = main()
结果: p>
INFO 2016-10-17 12:30:34,751 module.py:402] [default]检测到的文件更改:
/home/openstack/googleAppEngine/fastsimon/task2/task2_with_main/main.py
错误2016-10-17 12:30:42,328 wsgi.py:279]
追溯(最近一次调用最后一次):
文件/home/openstack/googleAppEngine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py,第267行,处理
result = handler(dict(self。 _environ),self._StartResponse)
TypeError:'RequestHandler'对象不可调用
INFO 2016-10-17 12:30:42,335 module.py:788] default:GET / get HTTP / 1.1 500 -
似乎解决方案非常简单它一直在躲避我,因为它隐藏在视线之内): __ name __
是 main ,而不是 __ main __ !
总之,以下代码按预期使用 main()
:
# with main()
import webapp2
$ b $ class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers ['Content-Type ')='text / plain'
self.response.write('in GET')
$ b $ class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers ['Content-Type'] ='text / plain'
self.response.write('in SET')
def main():
全局app
app = webapp2.WSGIApplication([
('/ get',GetHandler),
('/ set',SetHandler),
],debug = True)
#注意,如果__name__ =='main',它是'main'而不是'__main__'
:
main()
I'd like to use a main()
function in my GAE code
(note: the code below is just a minimal demonstration for a much larger program, hence the need for a main()
).
If I use the following code, it performs as expected:
import webapp2
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
where my app.yaml
is:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
However, I cannot figure out how to implement a main()
function, and still have app
act as it does in the code at the top. Namely, the following:
# with main()
import webapp2
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
def main():
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
if __name__ == '__main__':
main()
gives the following error for http://localhost:8080/get:
$ dev_appserver.py .
INFO 2016-10-17 11:29:30,962 devappserver2.py:769] Skipping SDK update check.
INFO 2016-10-17 11:29:31,059 api_server.py:205] Starting API server at: http://localhost:45865
INFO 2016-10-17 11:29:31,069 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2016-10-17 11:29:31,073 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2016-10-17 11:29:37,461 wsgi.py:263]
Traceback (most recent call last):
File "/home/.../sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/.../sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 302, in _LoadHandler
raise err
ImportError: <module 'main' from '/home/.../main.pyc'> has no attribute app
INFO 2016-10-17 11:29:37,496 module.py:788] default: "GET /get HTTP/1.1" 500 -
Edit 1
Trying:
# with main()
import webapp2
app = webapp2.RequestHandler()
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
def main():
global app
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
return app
if __name__ == '__main__':
app = main()
Results in:
INFO 2016-10-17 12:30:34,751 module.py:402] [default] Detected file changes:
/home/openstack/googleAppEngine/fastsimon/task2/task2_with_main/main.py
ERROR 2016-10-17 12:30:42,328 wsgi.py:279]
Traceback (most recent call last):
File "/home/openstack/googleAppEngine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
TypeError: 'RequestHandler' object is not callable
INFO 2016-10-17 12:30:42,335 module.py:788] default: "GET /get HTTP/1.1" 500 -
Seems that the solution was quite simple (it kept eluding me because it hid in plain sight): __name__
is main and not __main__!
In short, the following code utilises a main()
as expected:
# with main()
import webapp2
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
def main():
global app
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
# Note that it's 'main' and not '__main__'
if __name__ == 'main':
main()
这篇关于如何在GAE(Google App Engine)中使用Python main()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!