gpt4 book ai didi

python - 如何在python中读取cookie

转载 作者:太空狗 更新时间:2023-10-30 02:46:23 24 4
gpt4 key购买 nike

我是 python cgi 脚本的新手。我想在 python 中读取 cookie。我尝试了以下代码:

from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib

#Create a CookieJar object to hold the cookies
cj = cookielib.CookieJar()

#Create an opener to open pages using the http protocol and to process cookies.
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())

#Check out the cookies
print "the cookies are: "
for cookie in cj:
print cookie

但是,我只看到 cookies 是: msg。

我做错了什么吗?

最佳答案

尝试在 python 中读取 cookie:

#!/usr/bin/python

import os

# Hello world python program
print "Content-Type: text/html;charset=utf-8";
print

handler = {}
if 'HTTP_COOKIE' in os.environ:
cookies = os.environ['HTTP_COOKIE']
cookies = cookies.split('; ')

for cookie in cookies:
cookie = cookie.split('=')
handler[cookie[0]] = cookie[1]

for k in handler:
print k + " = " + handler[k] + "<br>

关于python - 如何在python中读取cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20898394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com