你如何在Zope和Plone中获取和设置cookie?

时间:2022-04-23 00:22:29

Documentation, and more importantly, some code examples would be very useful. I would prefer this to not be in protected scripts, but in the code that goes into modern packages.

文档,更重要的是,一些代码示例非常有用。我希望这不是在受保护的脚本中,而是在进入现代包的代码中。

2 个解决方案

#1


Use the response.setCookie() method. You can reach the response object via the request object. The latter you can reach via acquisition (self.REQUEST), or in views by accessing the passed-in request object, usually via self.request:

使用response.setCookie()方法。您可以通过请求对象访问响应对象。后者可以通过获取(self.REQUEST)或通过访问传入的请求对象在视图中访问,通常通过self.request:

self.request.response.setCookie(name, value, **options)

where options end up as extra cookie parameters. Thus, turning a cookie into a non-session cookie requires a expires='date' keyword, limiting the cookie to a path is a path='/somepath' keyword to the setCookie() method. The usual browser cookie rules apply here.

选项最终作为额外的cookie参数。因此,将cookie转换为非会话cookie需要使用expires ='date'关键字,将cookie限制为路径是setCookie()方法的path ='/ somepath'关键字。通常的浏览器cookie规则适用于此处。

To expire a cookie already set in the browser, you could either use a expires='date in the past' keyword, or you could use the response.expireCookie() method, which does this for you:

要使已在浏览器中设置的cookie过期,您可以使用expires ='过去的日期'关键字,也可以使用response.expireCookie()方法,它为您执行此操作:

self.request.response.expireCookie(name, **options)

In this case you can still include options like the path or other cookie flags, but the method will override the max_age and expires options to ensure the cookie is deleted by the browser.

在这种情况下,您仍然可以包含路径或其他cookie标记等选项,但该方法将覆盖max_age和expires选项以确保浏览器删除cookie。

Although you could use Zope's SESSION support, you really need to think through the scalability issues. For example, you need to think through how session data will be shared across a cluster if you use ZEO or RelStorage. It is generally preferable to avoid using SESSION altogether if scalability is going to be an issue.

虽然您可以使用Zope的SESSION支持,但您确实需要考虑可伸缩性问题。例如,如果使用ZEO或RelStorage,则需要考虑如何在群集*享会话数据。如果可扩展性成为问题,通常最好完全避免使用SESSION。

#2


To set cookies you use RESPONSE.setCookie.

要设置cookie,请使用RESPONSE.setCookie。

>>> self.REQUEST.RESPONSE.setCookie('cookiename', 'cookievalue', expires='Wed, 22 June 2009 12:00:00 GMT')

The cookie will end up in the REQUEST in the next request.

cookie将在下一个请求中的REQUEST中结束。

>>> self.REQUEST['cookiename']
'cookievalue'

You "delete" the cookie by using None as a value.

您可以使用None作为值来“删除”cookie。

**Note, though, that most of the times when people use cookies it's to store variables that have to do with sessions, and you can use self.REQUEST.SESSION for that, it's easier.

**但是请注意,大多数人使用cookie时都会存储与会话有关的变量,你可以使用self.REQUEST.SESSION,这样更容易。

#1


Use the response.setCookie() method. You can reach the response object via the request object. The latter you can reach via acquisition (self.REQUEST), or in views by accessing the passed-in request object, usually via self.request:

使用response.setCookie()方法。您可以通过请求对象访问响应对象。后者可以通过获取(self.REQUEST)或通过访问传入的请求对象在视图中访问,通常通过self.request:

self.request.response.setCookie(name, value, **options)

where options end up as extra cookie parameters. Thus, turning a cookie into a non-session cookie requires a expires='date' keyword, limiting the cookie to a path is a path='/somepath' keyword to the setCookie() method. The usual browser cookie rules apply here.

选项最终作为额外的cookie参数。因此,将cookie转换为非会话cookie需要使用expires ='date'关键字,将cookie限制为路径是setCookie()方法的path ='/ somepath'关键字。通常的浏览器cookie规则适用于此处。

To expire a cookie already set in the browser, you could either use a expires='date in the past' keyword, or you could use the response.expireCookie() method, which does this for you:

要使已在浏览器中设置的cookie过期,您可以使用expires ='过去的日期'关键字,也可以使用response.expireCookie()方法,它为您执行此操作:

self.request.response.expireCookie(name, **options)

In this case you can still include options like the path or other cookie flags, but the method will override the max_age and expires options to ensure the cookie is deleted by the browser.

在这种情况下,您仍然可以包含路径或其他cookie标记等选项,但该方法将覆盖max_age和expires选项以确保浏览器删除cookie。

Although you could use Zope's SESSION support, you really need to think through the scalability issues. For example, you need to think through how session data will be shared across a cluster if you use ZEO or RelStorage. It is generally preferable to avoid using SESSION altogether if scalability is going to be an issue.

虽然您可以使用Zope的SESSION支持,但您确实需要考虑可伸缩性问题。例如,如果使用ZEO或RelStorage,则需要考虑如何在群集*享会话数据。如果可扩展性成为问题,通常最好完全避免使用SESSION。

#2


To set cookies you use RESPONSE.setCookie.

要设置cookie,请使用RESPONSE.setCookie。

>>> self.REQUEST.RESPONSE.setCookie('cookiename', 'cookievalue', expires='Wed, 22 June 2009 12:00:00 GMT')

The cookie will end up in the REQUEST in the next request.

cookie将在下一个请求中的REQUEST中结束。

>>> self.REQUEST['cookiename']
'cookievalue'

You "delete" the cookie by using None as a value.

您可以使用None作为值来“删除”cookie。

**Note, though, that most of the times when people use cookies it's to store variables that have to do with sessions, and you can use self.REQUEST.SESSION for that, it's easier.

**但是请注意,大多数人使用cookie时都会存储与会话有关的变量,你可以使用self.REQUEST.SESSION,这样更容易。