在Python中以字节型数据发送cookie

发布于 2025-02-11 03:04:55 字数 2551 浏览 1 评论 0原文

我有一个具有以下格式的字典:

{
"cookie_name": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx",
"cookie_name2": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx",
...
}

我正在尝试使用请求将请求发送给网站上的cookie数据。

这是我尝试的:

import requests
cookies = {...}
r = requests.post('https://linktowebsite.com', cookies=cookies)

但是,我遇到了一个错误:

    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 471, in prepare_request
    cookies = cookiejar_from_dict(cookies)
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 537, in cookiejar_from_dict
    cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 352, in set_cookie
    and cookie.value.startswith('"')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

我尝试使用UTF-8编码/解码以及转换为字符串,但没有任何效果。

任何帮助都将不胜感激

:示例cookie值: {'asp.net_sessionId':b'v10k \ xfe:\ xe0 \ x18 \ xad \ xad \ x08 \ xd3 \ xe0 \ xe0 \ x12 \ x12 \ xe7` \ xe7` \ xe7 \ xe7 \ x15lib?\ x15lib? '}

\ xb6 \ xa0 \ x03g \

def get_cookies(ff_cookies, col_names):
    cj = http.cookiejar
    con = sqlite3.connect(ff_cookies)
    cur = con.cursor()
    cur.execute("SELECT "+", ".join(col_names)+" FROM cookies")  # gets data for SQLlite file
    
    # iterates through all columns and adds it to list
    cookieList = [item for item in cur.fetchall()]
    cookies_dict = {}
    for name in col_names:
        cookies_dict[name] = []

    # formats dictionary using above list
    cookies_dict_keys = list(cookies_dict)
    for cookie in cookieList:
        for i in range(len(cookie)):
            cookies_dict[cookies_dict_keys[i]].append(cookie[i])

    return cookies_dict

xe1w \ xb0- \ xf3 \ x0bh \ x0bh \ xd0 \ xccoi7 \ x10 \ x10 \ xe7。\ xaa \ xbf 是文件的路径,col_names是SQL文件中的列的名称,作为字符串列表,

此函数的返回值然后将其送入requests.post 函数

也必须注意,此SQL文件是在我的Edge浏览器文件中找到的:c:\ users \ username \ appData \ local \ local \ Microsoft \ Microsoft \ edge \ edge \ user deag \ user default \ default \ network \ cookies

I have a dictionary with the following format:

{
"cookie_name": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx",
"cookie_name2": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx",
...
}

I am attempting to use requests to send a request to the website with the cookie data.

Here's what I've tried:

import requests
cookies = {...}
r = requests.post('https://linktowebsite.com', cookies=cookies)

However, I'm met with an error instead:

    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 471, in prepare_request
    cookies = cookiejar_from_dict(cookies)
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 537, in cookiejar_from_dict
    cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
  File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 352, in set_cookie
    and cookie.value.startswith('"')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

I've tried encoding/decoding with UTF-8 as well as converting to string but nothing has worked.

Any help would be greatly appreciated

Edit: sample cookie value:
{'ASP.NET_SessionId': b'v10K\xfe:\xe0\x18\xad\x08\xd3\xe0\xe8\x12\xe7`\xe7\x15lib?\xd2\xe7oX\xb4jvT7\xd5\xad\xb6\xa0 \x03G\xe1W\xb0-\xf3\x0bh\xd0\xccOI7\x10\xe7.\xaa\xbf'}

Here's how to reproduce my dictionary:

def get_cookies(ff_cookies, col_names):
    cj = http.cookiejar
    con = sqlite3.connect(ff_cookies)
    cur = con.cursor()
    cur.execute("SELECT "+", ".join(col_names)+" FROM cookies")  # gets data for SQLlite file
    
    # iterates through all columns and adds it to list
    cookieList = [item for item in cur.fetchall()]
    cookies_dict = {}
    for name in col_names:
        cookies_dict[name] = []

    # formats dictionary using above list
    cookies_dict_keys = list(cookies_dict)
    for cookie in cookieList:
        for i in range(len(cookie)):
            cookies_dict[cookies_dict_keys[i]].append(cookie[i])

    return cookies_dict

Where ff_cookies is the path to the file and col_names are the names of the columns in the SQL file as a list of strings

The return value of this function is then fed into the requests.post function

It is also important to note that this SQL file was found in my edge browser files: C:\Users\username\AppData\Local\Microsoft\Edge\User Data\Default\Network\Cookies

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

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

发布评论

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

评论(1

少女情怀诗 2025-02-18 03:04:55

似乎无论您从哪里得到cookie do do not do not do not do not do not to not to not to。您可以使用 binascii.hexlify.hexlify 将这些binary Data转换为将这些Binary Data转换为十六进制,然后对其进行解码,以便可以将其作为cookie值传递:

尝试以下代码:

import requests
import binascii

val = b"v10K\xfe:\xe0\x18\xad\x08\xd3\xe0\xe8\x12\xe7`\xe7\x15lib?\xd2\xe7oX\xb4jvT7\xd5\xad\xb6\xa0 \x03G\xe1W\xb0-\xf3\x0bh\xd0\xccOI7\x10\xe7.\xaa\xbf"
val = binascii.hexlify(val)
val = val.decode()
print(val)
cookies = {"ASP.NET_SessionId": val}


r = requests.post('https://google.com', cookies=cookies)

输出

7631304bfe3ae018ad08d3e0e812e760e7156c69623fd2e76f58b46a765437d5adb6a0200347e157b02df30b68d0cc4f493710e72eaabf

但是,我鼓励您检查是否发现所有发现的cookie值是这种情况。使用此方法对它们进行解码,并查看结果值是否有意义(因为它们实际上不是十六进制的机会)。

Seems like wherever you got the cookies from do no not want you reading them. You can use binascii.hexlify to convert this binary data to hexadecimal, and then decode it so it can be passed as cookie value:

Try this code:

import requests
import binascii

val = b"v10K\xfe:\xe0\x18\xad\x08\xd3\xe0\xe8\x12\xe7`\xe7\x15lib?\xd2\xe7oX\xb4jvT7\xd5\xad\xb6\xa0 \x03G\xe1W\xb0-\xf3\x0bh\xd0\xccOI7\x10\xe7.\xaa\xbf"
val = binascii.hexlify(val)
val = val.decode()
print(val)
cookies = {"ASP.NET_SessionId": val}


r = requests.post('https://google.com', cookies=cookies)

Output

7631304bfe3ae018ad08d3e0e812e760e7156c69623fd2e76f58b46a765437d5adb6a0200347e157b02df30b68d0cc4f493710e72eaabf

However, I will encourage you to check whether this is the case for all cookie values you have found. Decode them using this method and see if the resulting values make sense (because of the off chance that they are not actually hexadecimals).

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