2012-04-02 72 views
1

我使用CherryPy的一個項目,並在主python腳本使用它使用的CherryPy在main.py 在方法中的主要應用程序,我導入了一個模塊稱爲認證中在兩個模塊,但只有一個CherryPy的實例

from authentication import auth然後將它傳遞給變量args。 CherryPy的已經在這裏使用已經明顯

@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 

從authentication.py內我想設置會話變量,所以我包括CherryPy的再次

def login(self, *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 

的問題是錯誤HTTPError: (400, 'Unexpected body parameters: username, password')當我用這個。我想在authentication.py中使用main.py中的cherrypy實例來設置會話變量。我怎樣才能做到這一點?

我也試圖傳遞CherryPy的對象,像這樣authString = auth.login(cherrypy, *args)和省略在authentication.py列入然而得到同樣的錯誤

回答

1

對不起這麼快就回答這一點,但一個小小的研究證明,論證** kwargs從auth方法中省略auth導致body_parameters被cherrypy拒絕,因爲它沒有期待它們。爲了解決這個問題:

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 
相關問題