python:与cgi脚本中的会话交互
python cgi 脚本可以向会话写入和读取数据吗?如果是这样怎么办?是否有高级 API 或者我必须推出自己的类?
Can python cgi scripts write and read data to the session? If so how? Is there a high-level API or must I roll my own classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cgi
上没有“会话”。如果您使用原始cgi
,则必须滚动自己的会话处理代码。基本上,会话的工作原理是创建一个唯一的 cookie 编号并将其通过响应标头发送给客户端,然后在每个连接上检查此 cookie。将会话数据存储在服务器上的某个位置(内存、数据库、磁盘),并使用 cookie 编号作为密钥在客户端发出的每个请求时检索它。
然而,
cgi
并不是使用 Python 开发 Web 应用程序的方式。使用wsgi
。使用网络框架。下面是一个使用 cherrypy 的简单示例。
cherrypy.tools.sessions
是一个cherrypy工具,它自动处理cookie设置/检索以及与数据的关联:请注意,此代码是一个
wsgi
应用程序,从某种意义上说,您可以将其发布到任何支持wsgi
的网络服务器(apache 有mod_wsgi< /代码>
)。另外,cherrypy 有自己的
wsgi
服务器,因此您只需使用 python 运行代码,它将开始在http://localhost:8080/
There's no "session" on
cgi
. You must roll your own session handling code if you're using rawcgi
.Basically, sessions work by creating a unique cookie number and sending it on a response header to the client, and then checking for this cookie on every connection. Store the session data somewhere on the server (memory, database, disk) and use the cookie number as a key to retrieve it on every request made by the client.
However
cgi
is not how you develop applications for the web in python. Usewsgi
. Use a web framework.Here's a quick example using cherrypy.
cherrypy.tools.sessions
is a cherrypy tool that handles cookie setting/retrieving and association with data automatically:Note that this code is a
wsgi
application, in the sense that you can publish it to anywsgi
-enabled web server (apache hasmod_wsgi
). Also, cherrypy has its ownwsgi
server, so you can just run the code with python and it will start serving onhttp://localhost:8080/
我的“低成本”网络托管计划不允许使用 wsgi。无法使用“mod_wsgi”apache 模块,因为它是共享 apache 服务器。我正在开发自己的课程。
为了不从零开始,我正在尝试实现此站点中提供的会话类: http://cgi.tutorial.codepoint.net/a-session-class
My 'low-cost' web hosting plan don't permit use wsgi. The 'mod_wsgi' apache module can't be used because is a shared apache server. I am developing my own class.
To not start from zero, I am experimenting the implementation of a session class available in this site: http://cgi.tutorial.codepoint.net/a-session-class