在两个模块中使用cherrypy,但只有一个cherrypy实例

2024-05-20 00:55:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个项目中使用cherrypy,并在python主脚本中使用它主.py 在主应用程序中的一个方法中,我导入了一个名为authentication的模块

from authentication import auth,然后将变量args传递给它。樱桃皮显然已经在这里用过了

@cherrypy.expose
def auth(self, *args):
    from authentication import auth
    auth = auth()

    page = common.header('Log in')

    authString = auth.login(*args)

    if authString:
        page += common.barMsg('Logged in succesfully', 1)
        page += authString
    else:
        page += common.barMsg('Authentication failed', 0)

    page += common.footer()

    return page

从内部认证.py我想设置会话变量以便再次包含cherrypy

^{pr2}$

问题是我使用这个时出现错误HTTPError: (400, 'Unexpected body parameters: username, password')。我想从主.py可接近认证.py在这里设置会话变量。我怎么能做到呢?在

我还尝试过传递cherrypy对象,比如so authString = auth.login(cherrypy, *args),并且省略了它在认证.py但是也有同样的错误


Tags: 项目infrompyimportauthauthentication错误
1条回答
网友
1楼 · 发布于 2024-05-20 00:55:49

很抱歉这么快就回答这个问题,但一项小调查发现,方法auth中省略的参数**kwargs导致cherrypy拒绝body_参数,因为它没有预料到这些参数。要解决此问题:

main.py

@cherrypy.expose
def auth(self, *args, **kwargs):
    from authentication import auth
    auth = auth()

    page = common.header('Log in')

    authString = auth.login(cherrypy, args)

    if authString:
        page += common.barMsg('Logged in succesfully', 1)
        page += authString
    else:
        page += common.barMsg('Authentication failed', 0)

    page += common.footer()

    return page

authentication.py

def login(self, cherrypy, args):
    output = "<b>&quot;args&quot; has %d variables</b><br/>\n" % len(args)

    if cherrypy.request.body_params is None:
        output += """<form action="/auth/login" method="post" name="login">
            <input type="text" maxlength="255" name="username"/>
            <input type="password" maxlength="255" name="password"/>
            <input type="submit" value="Log In"></input>
        </form>"""
        output += common.barMsg('Not a member yet? Join me <a         href="/auth/join">here</a>', 8)

    return output

相关问题 更多 >