如何使用Python从网络浏览器获取cookie?

发布于 2024-12-25 22:48:39 字数 1778 浏览 0 评论 0原文

上下文:
我正在研究对 OpenID 消费者(实际上是 StackExchange)的后端访问。如果我要向用户提供所有可能的 OpenID 提供商作为选项,那么我必须模拟浏览器交互以对每个提供商进行身份验证,然后才能提交 Open ID URL。不过,我认为我可以通过访问用户网络浏览器的现有 cookie 并直接使用 URL 向消费者请求身份验证来缩短此时间。

问题:
如何访问用户网络浏览器的cookie?我看到关于如何使用 Python 实现这一点的信息非常少。这个上一个问题部分回答了有关 Firefox 的问题,特别指出下面的代码示例。但是,我需要从 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?

PS:或者我是否以错误的方式看待最初的问题(即向 OpenID 提供商进行身份验证)? (我觉得我只是用一个问题替换另一个问题。)

Context:
I am working on a backend access to an OpenID consumer (StackExchange in fact). If I am to provide all possible OpenID providers as an option to the user, then I'd have to simulate browser interaction to authenticate to each of these providers before I could submit the Open ID URL. However, I think I could cut this short by accessing the existing cookies of the user's web-browser, and requesting authentication to the consumer directly with the URL.

Problem:
How to access the user's web-browser's cookies? I've seen very little information on how to do it with Python. This previous question partly answers the problem regarding Firefox, pointing especially to the code sample her below. However, I would need to access cookies from the most common web browsers used on Linux, not just 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

Question: Does Python provide a module that can facilitate cookie extraction from web-browsers? Otherwise, how should I adapt the above code to draw cookies from other browsers, like Chromium etc.?

PS: Or am I looking at the initial problem (i.e. authenticate to the OpenID provider) the wrong way? (I feel I am just replacing a problem by another.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叶落知秋 2025-01-01 22:48:39

我创建了一个模块来做到这一点,可以在这里找到: https://bitbucket.org/richardpenman/browsercookie/

用法示例:

import requests
import browsercookie
cj = browsercookie.chrome()
r = requests.get('http://stackoverflow.com', cookies=cj)

python3 分支: https://github.com/borisbabic/browser_cookie3

I created a module to do exactly that, available here: https://bitbucket.org/richardpenman/browsercookie/

Example usage:

import requests
import browsercookie
cj = browsercookie.chrome()
r = requests.get('http://stackoverflow.com', cookies=cj)

python3 fork: https://github.com/borisbabic/browser_cookie3

缘字诀 2025-01-01 22:48:39

除了 browser-cookie3 之外,您还可以尝试 https://github.com/n8henrie/pycookiecheat -- 它截至 2021 年 4 月,我在 Ubuntu 20.04 上使用 Chrome 工作。

Besides browser-cookie3, you can try https://github.com/n8henrie/pycookiecheat -- it worked for me with Chrome on Ubuntu 20.04 as of April 2021.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文