python 在pycurl中登录和使用cookies

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2221191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:06:37  来源:igfitidea点击:

Logging in and using cookies in pycurl

pythoncurlpycurl

提问by Vance

I need to download a file that is on a password protected page. To get to the page manually I first have to authenticate via an ordinary login page. I want to use curl to fetch this page in script.
My script first logins. It appears to succeed--it returns a 200 from a PUT to /login. However, the fetch of the desired page fails, with a 500.

我需要下载受密码保护的页面上的文件。要手动访问该页面,我首先必须通过普通登录页面进行身份验证。我想使用 curl 在脚本中获取此页面。
我的脚本首先登录。它似乎成功了——它从 PUT 返回一个 200 到 /login。但是,获取所需页面失败,返回 500。

I am using a "cookie jar":

我正在使用“饼干罐”:

C.setopt(pycurl.COOKIEJAR, 'cookie.txt')

In verbose mode, I can see cookies being exchanged when I fetch the file I need. Now my question: Is there more to using a COOKIEJAR?

在详细模式下,当我获取我需要的文件时,我可以看到正在交换 cookie。现在我的问题是:使用 COOKIEJAR 是否还有其他用途?

回答by wds

I believe Curl will store the cookies but you need to use them explicitly. I've only ever used the command line interface for this though. Scanning the documentation I think you might want to try:

我相信 Curl 会存储 cookie,但您需要明确使用它们。不过,我只为此使用过命令行界面。扫描文档我认为您可能想尝试:

C.setopt(pycurl.COOKIEFILE, 'cookie.txt')

(before the second request)

(在第二次请求之前)

回答by Vance

You should store cookie first and then read from it:

您应该先存储 cookie,然后从中读取:

C.setopt(pycurl.COOKIEJAR, 'cookie.txt')
C.setopt(pycurl.COOKIEFILE, 'cookie.txt')

Here what curl --help returned:

这是 curl --help 返回的内容:

-b, --cookie STRING/FILE  String or file to read cookies from (H)
-c, --cookie-jar FILE  Write cookies to this file after operation (H)

See this sample:

请参阅此示例:

def connect(self):
    '''
    Connect to NGNMS server
    '''
    host_url = self.ngnms_host + '/login'

    c = pycurl.Curl()
    c.setopt(c.URL, host_url)
    c.setopt(pycurl.TIMEOUT, 10)

    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(pycurl.POSTFIELDS, 'j_username={ngnms_user}&j_password={ngnms_password}'.format(**self.ngnms_login))
    c.setopt(pycurl.COOKIEJAR, 'data/ngnms.cookie')

    # c.setopt(c.VERBOSE, True)

    c.setopt(pycurl.SSL_VERIFYPEER, 0);
    session = c
    return session

回答by jsh

wds is right on.

wds 是正确的。

for your further edification, the available options are based on those at http://curl.haxx.se/libcurl/c/curl_easy_setopt.html(see the section on cookie shortcuts).

为了您的进一步启发,可用选项基于http://curl.haxx.se/libcurl/c/curl_easy_setopt.html 中的选项 (请参阅 cookie 快捷方式部分)。

a 500 is an internal server error...hard to be sure whether this can be blamed on yourscript without knowing more information about what's going on here. you could be failing to pass other data the page is expecting (unrelated to cookies) for all we know (and they have not implemented graceful error handling!)

500 是一个内部服务器错误...很难确定这是否可以归咎于您的脚本,而无需了解有关此处发生的情况的更多信息。就我们所知,您可能无法传递页面期望的其他数据(与 cookie 无关)(并且他们没有实现优雅的错误处理!)

jb

jb