【发布时间】:2014-07-29 06:16:51
【问题描述】:
我通常用 PHP 编写 Web 应用程序。现在我正在学习使用 Python 的 CherryPy 框架来做 web 程序。我要做的是接受来自用户的 http Get 请求,然后使用查询字符串中的变量来执行某些功能,最后我会返回结果。但现在我想我被 CherryPY 的编程惯例困住了。
我尝试使用索引函数通过GET方法接受变量post,然后执行如下功能:
import cherrypy
from sqlalchemy import *
from function import function
class WelcomePage:
def index(self,number):
if not number:
return "failure";
else:
number1=number;
return ;
now = datetime.datetime.utcnow();
code = generateCode();
finalResult();
def finalResult():
return code;
上面的代码不起作用,它只会在 index(self,number) 的 return 语句处结束,我无法继续其他函数。我想我正在打破 Python 风格的面向对象结构(不同于 PHP)。 那么以下只是处理GET输入并在计算后返回的正确方法
@cherrypy.exposed
def index(self,number):
if not number:
return "failure";
else:
cal = Calculation();
other =OtherFunction();
code=cal.doSomething(number1);
final =other.doAnotherThing(code);
return final;
也就是说,我需要在索引函数中调用所有其他函数,然后在索引函数中返回结果。这是对 http GET 变量(或帖子)进行处理的唯一方法。我可以有替代方法来编写不需要调用索引函数内的所有函数的代码(这似乎会使索引函数如此冗长)。这是一种更简洁明了的方式来完成具有不同功能的 GET 变量的处理,然后最终返回计算结果吗? google了很多天,还是没有找到好办法。
【问题讨论】:
-
如果你不想回来,就不要
return...那里到底在做什么? -
如果不返回,代码会出错