python - PyCurl 7.19.0 : get cookies from response using getinfo(pycurl. Cookie 列表)

标签 python cookies libcurl pycurl

我想获取响应中携带的Cookie(Set-Cookie: name=value; ...)。

传递信息常量和空列表的引用时:

set_cookies = []
c.getinfo(c.COOKIELIST, set_cookies)

我收到以下错误:

TypeError: c.getinfo() takes exactly 1 argument (2 given)

这很简单。

我也尝试过这个签名:

set_cookies = c.getinfo(c.COOKIELIST)

我收到此错误:

ValueError: invalid argument to getinfo

但是,这有点模糊。

使用 getinfo() 获取 HTTP 状态代码效果很好。

当然,我得到了 200 OKSet-Cookie header (使用 c.setopt(pycurl.VERBOSE, 1) 进行调试 )。

我当然读过

With very few exceptions, PycURL constants are derived from libcurl constants by removing the CURLINFO_ prefix.

也许这是异常(exception)?

在我看来,该文档相当糟糕,并且不包含任何有关使用 Set-Cookie 发送 cookie 的信息。

引用文献:

最佳答案

调用perform()后没有方法获取响应cookie。在使用 setopt() 配置时,可以使用传递给 PyCurl 实例的回调函数来捕获服务器发送的 header :

CURLOPT_HEADERFUNCTION

   Callback for writing received headers. See CURLOPT_HEADERFUNCTION 

示例代码摘录:

    set_cookies = []

    # closure to capture Set-Cookie
    def _write_header(header):
        match = re.match("^Set-Cookie: (.*)$", header)

        if match:
            set_cookies.append(match.group(1))

    # use closure to collect cookies sent from the server
    c.setopt(pycurl.HEADERFUNCTION, _write_header)

引用文献:

关于python - PyCurl 7.19.0 : get cookies from response using getinfo(pycurl. Cookie 列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650902/

相关文章:

python mysql安装失败

python - 如何在 linux 终端控制台中运行 python 脚本并继续使用命令行?

python - 模板中的 Django "Display once"

wordpress - 在注销前和管理员登录后设置 cookie

linux - 是否有可能在 linux 中从系统库中抑制用户库

php - 使用 cURL 保存图像

python - Django 基于类的 View

javascript - 使用POST请求登录时读取服务器发送的cookie

未在 Safari 6 中设置 Cookie

c - 对符号 'CERT_GetDefaultCertDB@@NSS_3.2' 的 undefined reference