标签归档:cookies

如何使用Python Requests库在发布请求中发送Cookie?

问题:如何使用Python Requests库在发布请求中发送Cookie?

我正在尝试使用Requests库发送带有后期请求的cookie,但是我不确定如何根据其文档实际设置cookie。该脚本可在Wikipedia上使用,并且需要发送的cookie具有以下形式:

enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly

但是,requests文档快速入门仅以此为例:

cookies = dict(cookies_are='working')

如何使用该库对上述Cookie进行编码?我是否需要使用python的标准cookie库进行制作,然后将其与POST请求一起发送?

I’m trying to use the Requests library to send cookies with a post request, but I’m not sure how to actually set up the cookies based on its documentation. The script is for use on Wikipedia, and the cookie(s) that need to be sent are of this form:

enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly

However, the requests documentation quickstart gives this as the only example:

cookies = dict(cookies_are='working')

How can I encode a cookie like the above using this library? Do I need to make it with python’s standard cookie library, then send it along with the POST request?


回答 0

最新版本的“请求”将通过简单的词典为您构建CookieJars。

import requests

cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookies)

请享用 :)

The latest release of Requests will build CookieJars for you from simple dictionaries.

import requests

cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookies)

Enjoy :)


回答 1

只是为了扩展上一个答案,如果将两个请求链接在一起,并且想要将第一个返回的cookie发送到第二个(例如,保持跨请求的会话有效),则可以执行以下操作:

import requests
r1 = requests.post('http://www.yourapp.com/login')
r2 = requests.post('http://www.yourapp.com/somepage',cookies=r1.cookies)

Just to extend on the previous answer, if you are linking two requests together and want to send the cookies returned from the first one to the second one (for example, maintaining a session alive across requests) you can do:

import requests
r1 = requests.post('http://www.yourapp.com/login')
r2 = requests.post('http://www.yourapp.com/somepage',cookies=r1.cookies)

回答 2

如果要将cookie传递给浏览器,则必须附加到标头以发送回。如果您使用的是wsgi:

import requests
...


def application(environ, start_response):
    cookie = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}
    response_headers = [('Content-type', 'text/plain')]
    response_headers.append(('Set-Cookie',cookie))
...

    return [bytes(post_env),response_headers]

通过将auth user / password传递给我的python脚本并将cookie传递给浏览器,我能够成功地通过托管在同一域中的Bugzilla和TWiki进行身份验证,而python wsgi脚本正在运行。这使我可以在同一浏览器中打开Bugzilla和TWiki页面并进行身份验证。我正在尝试对SuiteCRM执行相同的操作,但是即使SuiteCRM已成功通过身份验证,我也无法接受从python脚本获得的会话cookie。

If you want to pass the cookie to the browser, you have to append to the headers to be sent back. If you’re using wsgi:

import requests
...


def application(environ, start_response):
    cookie = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}
    response_headers = [('Content-type', 'text/plain')]
    response_headers.append(('Set-Cookie',cookie))
...

    return [bytes(post_env),response_headers]

I’m successfully able to authenticate with Bugzilla and TWiki hosted on the same domain my python wsgi script is running by passing auth user/password to my python script and pass the cookies to the browser. This allows me to open the Bugzilla and TWiki pages in the same browser and be authenticated. I’m trying to do the same with SuiteCRM but i’m having trouble with SuiteCRM accepting the session cookies obtained from the python script even though it has successfully authenticated.


如何使用Python登录网页并检索Cookie以供以后使用?

问题:如何使用Python登录网页并检索Cookie以供以后使用?

我想使用python下载和解析网页,但是要访问它,我需要设置一些cookie。因此,我需要先通过https登录到网页。登录时刻需要将两个POST参数(用户名,密码)发送到/login.php。在登录请求期间,我想从响应头中检索cookie并将其存储,以便可以在请求中使用它们来下载网页/data.php。

我将如何在python(最好是2.6)中做到这一点?如果可能,我只想使用内置模块。

I want to download and parse webpage using python, but to access it I need a couple of cookies set. Therefore I need to login over https to the webpage first. The login moment involves sending two POST params (username, password) to /login.php. During the login request I want to retrieve the cookies from the response header and store them so I can use them in the request to download the webpage /data.php.

How would I do this in python (preferably 2.6)? If possible I only want to use builtin modules.


回答 0

import urllib, urllib2, cookielib

username = 'myuser'
password = 'mypassword'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
opener.open('http://www.example.com/login.php', login_data)
resp = opener.open('http://www.example.com/hiddenpage.php')
print resp.read()

resp.read()是您要打开的页面的纯HTML,您可以使用opener会话cookie查看任何页面。

import urllib, urllib2, cookielib

username = 'myuser'
password = 'mypassword'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
opener.open('http://www.example.com/login.php', login_data)
resp = opener.open('http://www.example.com/hiddenpage.php')
print resp.read()

resp.read() is the straight html of the page you want to open, and you can use opener to view any page using your session cookie.


回答 1

这是使用优秀请求库的版本:

from requests import session

payload = {
    'action': 'login',
    'username': USERNAME,
    'password': PASSWORD
}

with session() as c:
    c.post('http://example.com/login.php', data=payload)
    response = c.get('http://example.com/protected_page.php')
    print(response.headers)
    print(response.text)

Here’s a version using the excellent requests library:

from requests import session

payload = {
    'action': 'login',
    'username': USERNAME,
    'password': PASSWORD
}

with session() as c:
    c.post('http://example.com/login.php', data=payload)
    response = c.get('http://example.com/protected_page.php')
    print(response.headers)
    print(response.text)

Requests-一个简单而优雅的HTTP库

Requests

Requests是一个简单而优雅的HTTP库

>>> import requests
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"type":"User"...'
>>> r.json()
{'disk_usage': 368627, 'private_gists': 484, ...}

请求允许您极其轻松地发送HTTP/1.1请求。不需要手动将查询字符串添加到URL,也不需要对PUT&POST数据-但现在,只需使用json方法!

Requests是目前下载量最大的Python包之一,14M downloads / week-根据GitHub的说法,请求目前正在depended upon通过500,000+存储库。您当然可以信任这段代码



安装请求和支持的版本

请访问PyPI上的Requests:

$ python -m pip install requests

Requests正式支持Python 2.7和3.6+

支持的功能和最佳做法

Requests已经为构建健壮可靠的HTTP语言应用程序的需求做好了准备,以满足当今的需求

  • 保活和连接池
  • 国际域名和URL
  • 具有Cookie持久性的会话
  • 浏览器样式的TLS/SSL验证
  • 基本和摘要身份验证
  • 熟悉dict-喜欢饼干
  • 自动内容解压缩和解码
  • 多部分文件上载
  • SOCKS代理支持
  • 连接超时
  • 流式下载
  • 自动兑现.netrc
  • 分块的HTTP请求

API参考和用户指南,请访问Read the Docs

克隆存储库

在克隆请求存储库时,您可能需要添加-c fetch.fsck.badTimezone=ignore用于避免有关错误提交的错误的标记(请参见this issue了解更多背景信息):

git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git

您还可以将此设置应用于全局Git配置:

git config --global fetch.fsck.badTimezone ignore