I am implementing an http server with twisted.web. Here comes the problem: there is a login operation; after that, I want the http server to remember each client using acookie/session, until the user closes the browser.
我正在用twisted.web实现一个http服务器。问题来了:有一个登录操作;之后,我希望http服务器使用acookie/session记住每个客户端,直到用户关闭浏览器。
I've read the twisted.web document, but I can't figure out how to do this. I know the request object has a function named getSession(), then a session object will be returned. What next? How do I store the information during the several requests?
我读过扭曲的。web文档,但是我不知道怎么做。我知道请求对象有一个名为getSession()的函数,然后将返回一个会话对象。下一个什么?如何在多个请求期间存储信息?
I also searched the twisted mail list; there nothing very helpful, and I am still confused. If someone used this before, please explain this to me, or even put some code here, so I can understand it myself. Thank you very much!
我还搜索了twisted mail列表;没有什么很有帮助的,我还是很困惑。如果有人以前用过这个,请给我解释一下,或者在这里写一些代码,这样我就能自己理解了。非常感谢!
3 个解决方案
#1
4
Calling getSession() will generate a session and add the cookie to the request:
调用getSession()将生成一个会话并将cookie添加到请求中:
getSession()的源代码
If the clients already has a session cookie then calling getSession() will read it and return a Session with the original Session content. So it is transparent to your code whether or not it is actually creating the Session cookie or just reading it.
如果客户端已经有一个会话cookie,那么调用getSession()将读取它并返回带有原始会话内容的会话。所以不管它是在创建会话cookie还是只是读取它,它对代码都是透明的。
Session cookies have certain properties... if you want more control over the contents of the cookie then look at Request.addCookie() which getSession() calls behind the scenese.
会话cookie具有某些属性……如果您希望对cookie的内容有更多的控制,那么请查看Request.addCookie(), getSession()在scenese后面调用它。
#2
4
You can use "request.getSession()" to get a componentized object.
您可以使用“request.getSession()”来获取组件化对象。
You can read more about componentized in http://twistedmatrix.com/documents/current/api/twisted.python.components.Componentized.html -- the basic way of using it is via defining an interface and an implementation, and pushing your on objects into the session.
您可以在http://twistedmatrix.com/documents/current/api/twisted.python.components.components.componentized.html中阅读更多关于组件化的内容——使用它的基本方法是定义接口和实现,并将对象推入会话。
#3
2
See this related question Store an instance of a connection - twisted.web. The answer there links to this blog post http://jcalderone.livejournal.com/53680.html, which shows an example of storing a counter for the number of visits for the session (thanks to jcalderone for the example):
请参见此相关问题存储一个connection - twisted.web的实例。这里的答案链接到这篇博客文章http://jcalderone.livejournal.com/53680.html,其中显示了存储会话访问次数计数器的示例(感谢jcalderone的示例):
# in a .rpy file launched with `twistd -n web --path .`
cache()
from zope.interface import Interface, Attribute, implements
from twisted.python.components import registerAdapter
from twisted.web.server import Session
from twisted.web.resource import Resource
class ICounter(Interface):
value = Attribute("An int value which counts up once per page view.")
class Counter(object):
implements(ICounter)
def __init__(self, session):
self.value = 0
registerAdapter(Counter, Session, ICounter)
class CounterResource(Resource):
def render_GET(self, request):
session = request.getSession()
counter = ICounter(session)
counter.value += 1
return "Visit #%d for you!" % (counter.value,)
resource = CounterResource()
Don't worry if this seems confusing - there are two things that you need to understand before the behaviour here makes sense:
如果这看起来令人困惑,不要担心——在这里的行为变得有意义之前,有两点你需要理解:
- Twisted (Zope) Interfaces & Adapters
- Twisted (Zope)接口和适配器
- Componentized
- 组件化
The counter value is stored in an Adapter class, the Interface class documents what that class provides. The reason that you can store persistent data in the Adapter is because Session (returned by getSession()) is a subclass of Componentized.
计数器值存储在适配器类中,接口类记录该类提供的内容。可以在适配器中存储持久数据的原因是会话(由getSession()返回)是组件化的子类。
#1
4
Calling getSession() will generate a session and add the cookie to the request:
调用getSession()将生成一个会话并将cookie添加到请求中:
getSession()的源代码
If the clients already has a session cookie then calling getSession() will read it and return a Session with the original Session content. So it is transparent to your code whether or not it is actually creating the Session cookie or just reading it.
如果客户端已经有一个会话cookie,那么调用getSession()将读取它并返回带有原始会话内容的会话。所以不管它是在创建会话cookie还是只是读取它,它对代码都是透明的。
Session cookies have certain properties... if you want more control over the contents of the cookie then look at Request.addCookie() which getSession() calls behind the scenese.
会话cookie具有某些属性……如果您希望对cookie的内容有更多的控制,那么请查看Request.addCookie(), getSession()在scenese后面调用它。
#2
4
You can use "request.getSession()" to get a componentized object.
您可以使用“request.getSession()”来获取组件化对象。
You can read more about componentized in http://twistedmatrix.com/documents/current/api/twisted.python.components.Componentized.html -- the basic way of using it is via defining an interface and an implementation, and pushing your on objects into the session.
您可以在http://twistedmatrix.com/documents/current/api/twisted.python.components.components.componentized.html中阅读更多关于组件化的内容——使用它的基本方法是定义接口和实现,并将对象推入会话。
#3
2
See this related question Store an instance of a connection - twisted.web. The answer there links to this blog post http://jcalderone.livejournal.com/53680.html, which shows an example of storing a counter for the number of visits for the session (thanks to jcalderone for the example):
请参见此相关问题存储一个connection - twisted.web的实例。这里的答案链接到这篇博客文章http://jcalderone.livejournal.com/53680.html,其中显示了存储会话访问次数计数器的示例(感谢jcalderone的示例):
# in a .rpy file launched with `twistd -n web --path .`
cache()
from zope.interface import Interface, Attribute, implements
from twisted.python.components import registerAdapter
from twisted.web.server import Session
from twisted.web.resource import Resource
class ICounter(Interface):
value = Attribute("An int value which counts up once per page view.")
class Counter(object):
implements(ICounter)
def __init__(self, session):
self.value = 0
registerAdapter(Counter, Session, ICounter)
class CounterResource(Resource):
def render_GET(self, request):
session = request.getSession()
counter = ICounter(session)
counter.value += 1
return "Visit #%d for you!" % (counter.value,)
resource = CounterResource()
Don't worry if this seems confusing - there are two things that you need to understand before the behaviour here makes sense:
如果这看起来令人困惑,不要担心——在这里的行为变得有意义之前,有两点你需要理解:
- Twisted (Zope) Interfaces & Adapters
- Twisted (Zope)接口和适配器
- Componentized
- 组件化
The counter value is stored in an Adapter class, the Interface class documents what that class provides. The reason that you can store persistent data in the Adapter is because Session (returned by getSession()) is a subclass of Componentized.
计数器值存储在适配器类中,接口类记录该类提供的内容。可以在适配器中存储持久数据的原因是会话(由getSession()返回)是组件化的子类。