如何在python中读取cookie

 拍友2502920603 发布于 2023-02-06 19:33

我是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

但是,我只看到the cookies are:味精。

难道我做错了什么?

1 个回答
  • 尝试此操作以读取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>
    

    2023-02-06 19:35 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有