首页 > 解决方案 > 我的 cookie 阅读器不工作

问题描述

所以,我在网上找不到任何真正可以读取 cookie 的 Python 3 脚本。这是代码(解释后面的内容);

red_theme = "red_style.css"
blue_theme = "blue_style.css"
green_theme = "green_style.css"
dark_theme = "dark_style.css"
light_theme = "light_style.css"

### COOKIE WRITER + THEME SETTINGS
jar = Cookie.SimpleCookie()

if data.getvalue("t") == "red":
    theme = red_theme
    jar['s_theme']=red_theme
if data.getvalue("t") == "blue":
    theme = blue_theme
    jar['s_theme']=blue_theme
if data.getvalue("t") == "green":
    theme = green_theme
    jar['s_theme']=green_theme
if data.getvalue("t") == "dark":
    theme = dark_theme
    jar['s_theme']=dark_theme
if data.getvalue("t") == "light":
    theme = light_theme
    jar['s_theme']=light_theme
else:
    theme = red_theme
    jar['s_theme']=red_theme

### COOKIE READER
try:
    jar = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
    theme = jar["s_theme"].value
except (Cookie.CookieError, KeyError):
    theme = "error.css"

此脚本查找您是否将 ?t=***** 作为某个主题(红色、绿色、蓝色......等)。然后在该信息上,它将一个 cookie(jar)写入 [s_theme = colour] 但是...读取的脚本不起作用。

### COOKIE READER
try:
    jar = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
    theme = jar["s_theme"].value
except (Cookie.CookieError, KeyError):
    theme = "error.css"

在 HTML 代码之后;它这样做:

<link rel="stylesheet" href=\"""" + theme + """\">

总之; 尝试制作一个主题设置器,根据您选择的默认主题设置,因此如果您转到https://...?theme=dark,它会将您的默认主题设置为深色。但是...我无法使用阅读脚本。

真的很感谢你的帮助 - Reefive

标签: pythonpython-3.xcookies

解决方案


推荐阅读