【发布时间】:2012-02-07 09:42:30
【问题描述】:
上下文:
我正在处理对 OpenID 消费者(实际上是 StackExchange)的后端访问。如果我要向用户提供所有可能的 OpenID 提供程序作为选项,那么我必须模拟浏览器交互以对每个提供程序进行身份验证,然后才能提交 Open ID URL。但是,我想我可以通过访问用户网络浏览器的现有 cookie 并直接使用 URL 向消费者请求身份验证来缩短这一点。
问题:
如何访问用户的网络浏览器的 cookie?我很少看到有关如何使用 Python 进行操作的信息。这个previous question部分回答了关于Firefox的问题,特别指出下面的the code sample她。但是,我需要从 Linux 上最常用的网络浏览器访问 cookie,而不仅仅是 Firefox。
#! /usr/bin/env python
# Protocol implementation for handling gsocmentors.com transactions
# Author: Noah Fontes nfontes AT cynigram DOT com
# License: MIT
def sqlite2cookie(filename):
from cStringIO import StringIO
from pysqlite2 import dbapi2 as sqlite
con = sqlite.connect(filename)
cur = con.cursor()
cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")
ftstr = ["FALSE","TRUE"]
s = StringIO()
s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
""")
for item in cur.fetchall():
s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
item[0], ftstr[item[0].startswith('.')], item[1],
ftstr[item[2]], item[3], item[4], item[5]))
s.seek(0)
cookie_jar = cookielib.MozillaCookieJar()
cookie_jar._really_load(s, '', True, True)
return cookie_jar
问题:Python 是否提供了一个模块,可以方便地从网络浏览器中提取 cookie? 否则,我应该如何修改上述代码以从其他浏览器(如 Chromium 等)提取 cookie? p>
PS:还是我以错误的方式看待最初的问题(即向 OpenID 提供者进行身份验证)? (我觉得我只是在用另一个问题代替一个问题。)
【问题讨论】:
-
@monkut:实际上,这仅与在整个会话期间保留 cookie 有关,我已经知道该怎么做。我想做的是从浏览器本身获取cookie。
-
您可能想查看用于检索 cookie 的 Cookie 模块。 thiscookie = Cookie.SimpleCookie() if os.environ.keys() 中的 u'HTTP_COOKIE': thiscookie.load(os.environ[u'HTTP_COOKIE'])
-
@monkut:这里有趣的部分是
os.environ.keys()中的HTTP_COOKIE,你有更多关于它的信息吗?谢谢。
标签: python cookies openid browser