Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用aiohttp获取cookie_Python 3.x_Cookies_Aiohttp - Fatal编程技术网

Python 3.x 使用aiohttp获取cookie

Python 3.x 使用aiohttp获取cookie,python-3.x,cookies,aiohttp,Python 3.x,Cookies,Aiohttp,我正在尝试使用aiohttp从浏览器获取cookies。从文档和谷歌搜索中,我只找到了关于在aiohttp中设置cookies的文章 在烧瓶里,我会尽可能简单地得到饼干 cookie = request.cookies.get('name_of_cookie') # do something with cookie 是否有一种使用aiohttp从浏览器中获取cookie的简单方法 是否有一种使用aiohttp从浏览器获取cookie的简单方法 不确定这是否简单,但有一种方法: import a

我正在尝试使用aiohttp从浏览器获取cookies。从文档和谷歌搜索中,我只找到了关于在aiohttp中设置cookies的文章

在烧瓶里,我会尽可能简单地得到饼干

cookie = request.cookies.get('name_of_cookie')
# do something with cookie
是否有一种使用aiohttp从浏览器中获取cookie的简单方法

是否有一种使用aiohttp从浏览器获取cookie的简单方法

不确定这是否简单,但有一种方法:

import asyncio
import aiohttp


async def main():
    urls = [
        'http://httpbin.org/cookies/set?test=ok',
    ]
    for url in urls:
        async with aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar()) as s:
            async with s.get(url) as r:
                print('JSON', await r.json())
                cookies = s.cookie_jar.filter_cookies('http://httpbin.org')
                for key, cookie in cookies.items():
                    print('Key: "%s", Value: "%s"' % (cookie.key, cookie.value))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
程序生成以下输出:

JSON: {'cookies': {'test': 'ok'}}
Key: "test", Value: "ok"
示例改编自+

现在,如果要使用先前设置的cookie执行请求:

import asyncio
import aiohttp
url = 'http://example.com'

# Filtering for the cookie, saving it into a varibale
async with aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar()) as s:
cookies = s.cookie_jar.filter_cookies('http://example.com')
for key, cookie in cookies.items():
    if key == 'test':
        cookie_value = cookie.value

# Using the cookie value to do anything you want:
# e.g. sending a weird request containing the cookie in the header instead.
headers = {"Authorization": "Basic f'{cookie_value}'"}
async with s.get(url, headers=headers) as r:
    print(await r.json())


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

对于测试包含由IP地址组成的主机部分的URL,请使用
aiohttp.ClientSession(cookie\u jar=aiohttp.CookieJar(safe=True))
,根据是的,cookie作为dict存储在request.cookies中,就像在flask中一样,因此request.cookies.get('name\u of_\u cookie')的工作原理相同

在AIOHTTPRepository的部分中有一个文件,它显示了如何检索、设置和删除cookie。下面是脚本中读取cookies并将其作为预格式化字符串返回模板的部分:

from pprint import pformat
from aiohttp import web

tmpl = '''\
<html>
    <body>
        <a href="/login">Login</a><br/>
        <a href="/logout">Logout</a><br/>
        <pre>{}</pre>
    </body>
</html>'''

async def root(request):
    resp = web.Response(content_type='text/html')
    resp.text = tmpl.format(pformat(request.cookies))
    return resp
从pprint导入pformat
从aiohttp导入web
tmpl=''\


{} ''' 异步定义根目录(请求): resp=web.Response(content\u type='text/html') resp.text=tmpl.format(pformat(request.cookies)) 返回响应