2013-06-05 100 views
3

我是Python的初学者。我想知道它为什么会抛出一个错误。 我得到一个错误,指出TypeError:client_session()只需要2个参数(给出1) client_session方法返回SecureCookie对象。Python2.7中的错误只需要2个参数(1给出)

我这里有

from werkzeug.utils import cached_property 
from werkzeug.contrib.securecookie import SecureCookie 
from werkzeug.wrappers import BaseRequest, AcceptMixin, ETagRequestMixin, 

class Request(BaseRequest): 

def client_session(self,SECRET_KEY1): 
    data = self.cookies.get('session_data') 
    print " SECRET_KEY " , SECRET_KEY1 
    if not data: 
    print "inside if data" 
    cookie = SecureCookie({"SECRET_KEY": SECRET_KEY1},secret_key=SECRET_KEY1) 
    cookie.serialize() 
    return cookie 
    print 'self.form[login.name] ', self.form['login.name'] 
    print 'data new' , data 
    return SecureCookie.unserialize(data, SECRET_KEY1) 


#and another 
class Application(object): 
def __init__(self): 
    self.SECRET_KEY = os.urandom(20) 

def dispatch_request(self, request): 
    return self.application(request) 

def application(self,request): 
    return request.client_session(self.SECRET_KEY).serialize() 


# This is our externally-callable WSGI entry point 
def __call__(self, environ, start_response): 
    """Invoke our WSGI application callable object""" 
    return self.wsgi_app(environ, start_response) 
+0

我认为'application()'方法中的'request'应该大写?此外,这看起来不像python n00b代码(做得好)。无论我看到这个错误,我看到它只有1关闭(需要2,1给出),我通常会发现它与'自我' – TehTris

+0

我曾尝试使用请求更改r大写,但它仍然给出相同错误 – user2456373

回答

1

这个代码通常情况下,这意味着你调用client_session为不受约束的方法,给它只有一个参数。你应该反思一下,看看你在application()方法中使用的是什么,也许这不是你期望的。

要知道它是什么,你可以随时添加调试打印点:

print "type: ", type(request) 
print "methods: ", dir(request) 

,我希望你会看到该请求是原始Request类WERKZEUG给你...

在这里,你扩展了werkzeug的BaseRequest,并且在application()中,你期望werkzeug神奇地知道你自己实现的BaseRequest类。但是如果你阅读了python的禅意,你就会知道“显式比隐式更好”,所以Python永远不会神奇地做出任何事情,你必须告诉你的库你以某种方式做出了改变。

所以阅读WERKZEUG的文档后,你可以发现,这其实是这样:

The request object is created with the WSGI environment as first argument and will add itself to the WSGI environment as 'werkzeug.request' unless it’s created with populate_request set to False.

这可能不是人们完全清楚谁也不知道WERKZEUG是什么,什么是设计背后的逻辑。

但一个简单的谷歌查询,显示BaseRequest的用法示例:

我只能从werkzeug.wrappers一派进口BaseRequest`

S o现在,您应该能够猜出您的应用程序中要更改什么。由于您只给出了应用程序的几个部分,因此我无法告知您具体在哪里/要更改哪些内容。

+0

我曾尝试使用Request将r更改为大写,但它仍然给出了与我的意思相同的错误 – user2456373

+0

。更改变量的名称不会改变行为。尝试添加'print type(request)'和'print dir(request)',这样你就可以获得'request'参数的类型名称和所有方法列表。 – zmo

+0

阅读[documentation](http://werkzeug.pocoo.org/docs/wrappers/#werkzeug.wrappers.BaseRequest)它看起来应该给Request对象以wsgi应用程序调用。我从来没有使用过werkzeug,所以我不知道如何/在哪里...... – zmo

相关问题