【发布时间】:2020-05-19 05:49:17
【问题描述】:
这里是代码
switcher = {
"1": func1(),
"2": func2(),
"3": func3(),
"4": func4(),
"5": func5(),
"6": func6(),
"9": func7(),
"11": func8(),
"12":func9(),
"13": func10()
}
@app.route('/ussdcallback', methods=['GET', 'POST'])
def switch(text = request.values.get("text", None)):
session_id = request.values.get("sessionId", None)
service_code = request.values.get("serviceCode", None)
func = switcher.get(text, "none")
return func()
这是我得到的输出
Traceback (most recent call last):
2020-05-19T05:44:17.416279+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-05-19T05:44:17.416279+00:00 app[web.1]: worker.init_process()
2020-05-19T05:44:17.416280+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-05-19T05:44:17.416280+00:00 app[web.1]: self.load_wsgi()
2020-05-19T05:44:17.416280+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-05-19T05:44:17.416280+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-05-19T05:44:17.416281+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-05-19T05:44:17.416281+00:00 app[web.1]: self.callable = self.load()
2020-05-19T05:44:17.416281+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-05-19T05:44:17.416281+00:00 app[web.1]: return self.load_wsgiapp()
2020-05-19T05:44:17.416282+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-05-19T05:44:17.416282+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-05-19T05:44:17.416282+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-05-19T05:44:17.416282+00:00 app[web.1]: mod = importlib.import_module(module)
2020-05-19T05:44:17.416283+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-05-19T05:44:17.416284+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-05-19T05:44:17.416285+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-05-19T05:44:17.416285+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-05-19T05:44:17.416285+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2020-05-19T05:44:17.416285+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2020-05-19T05:44:17.416286+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2020-05-19T05:44:17.416286+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-05-19T05:44:17.416286+00:00 app[web.1]: File "/app/run.py", line 1, in <module>
2020-05-19T05:44:17.416286+00:00 app[web.1]: from app import app
2020-05-19T05:44:17.416286+00:00 app[web.1]: File "/app/app/__init__.py", line 359, in <module>
2020-05-19T05:44:17.416287+00:00 app[web.1]: from app import routes, usrmgt, safeDeposit
2020-05-19T05:44:17.416287+00:00 app[web.1]: File "/app/app/routes.py", line 22, in <module>
2020-05-19T05:44:17.416287+00:00 app[web.1]: "1": radar(),
2020-05-19T05:44:17.416287+00:00 app[web.1]: File "/app/app/radar.py", line 161, in radar
2020-05-19T05:44:17.416287+00:00 app[web.1]: resp = make_response(response, 200)
2020-05-19T05:44:17.416288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/helpers.py", line 223, in make_response
2020-05-19T05:44:17.416288+00:00 app[web.1]: return current_app.make_response(args)
2020-05-19T05:44:17.416288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
2020-05-19T05:44:17.416288+00:00 app[web.1]: return getattr(self._get_current_object(), name)
2020-05-19T05:44:17.416288+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
2020-05-19T05:44:17.416289+00:00 app[web.1]: return self.__local()
2020-05-19T05:44:17.416289+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/globals.py", line 52, in _find_app
2020-05-19T05:44:17.416311+00:00 app[web.1]: raise RuntimeError(_app_ctx_err_msg)
2020-05-19T05:44:17.416312+00:00 app[web.1]: RuntimeError: Working outside of application context.
2020-05-19T05:44:17.416312+00:00 app[web.1]:
2020-05-19T05:44:17.416313+00:00 app[web.1]: This typically means that you attempted to use functionality that needed
2020-05-19T05:44:17.416313+00:00 app[web.1]: to interface with the current application object in some way. To solve
2020-05-19T05:44:17.416313+00:00 app[web.1]: this, set up an application context with app.app_context(). See the
2020-05-19T05:44:17.416319+00:00 app[web.1]: documentation for more information.
2020-05-19T05:44:17.416716+00:00 app[web.1]: [2020-05-19 05:44:17 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-05-19T05:44:18.050667+00:00 app[web.1]: [2020-05-19 05:44:18 +0000] [4] [INFO] Shutting down: Master
2020-05-19T05:44:18.050834+00:00 app[web.1]: [2020-05-19 05:44:18 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-05-19T05:44:18.145123+00:00 heroku[web.1]: Process exited with status 3
2020-05-19T05:44:18.190532+00:00 heroku[web.1]: State changed from up to crashed
【问题讨论】:
-
请解释你要做什么!并向我们展示引发异常的代码。您的代码不适合。
-
我正在尝试在 python 中使用替代 switch 语句来使用烧瓶框架调用各种函数。而不是常规的 IF ... ELSE 语句。
-
总结一下如何使用flask在python中实现switch语句在web上运行
-
Python 没有 switch 语句。您正在尝试使用带有函数的 dict。当您将值放入时,您正在 调用 函数,您需要将实际的函数对象放入其中,所以不要调用它们。然后当你从字典中检索函数时,你调用它。
-
请问我可以举个例子吗?
标签: python flask python-3.7