上下文:
我正在对OpenID使用者(实际上是StackExchange)进行后端访问。如果我要为用户提供所有可能的OpenID提供程序,那么在提交Open ID URL之前,我必须模拟浏览器交互以对每个提供程序进行身份验证。不过,我认为我可以通过访问用户网络浏览器的现有cookie来缩短这一短缺时间,并直接通过URL向消费者请求认证。如何使用Python从网络浏览器获取cookie?
问题:
如何访问用户的网络浏览器的cookie?我很少看到有关如何使用Python进行操作的信息。这previous question部分回答了关于Firefox的问题,特别指出她下面的the code sample。但是,我需要从Linux上最常用的Web浏览器访问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?
附注:或者我正在查看最初的问题(即向OpenID提供程序进行身份验证)是否有错误的方法? (我觉得我只是另一种替代的问题。)
你可能想看看http://stackoverflow.com/questions/4634408/can-non-web-applications-use-openid – Amber 2012-01-11 00:25:20
或http:// stackoverflow。 com/questions/921532/retrieveing-cookies-python – monkut 2012-01-11 01:07:55
@monkut:实际上,只有在整个会话中保持cookie的状态,我已经知道该怎么做。我想要做的是从浏览器本身获取cookie。 – Benjamin 2012-01-11 01:24:29