简体   繁体   English

使用cgi-python脚本设置co​​okie

[英]set cookie with cgi-python scripts

I'm trying to response from the server with a set-cookie header 我正在尝试使用set-cookie标头从服务器进行响应

my cgi script begins as follows 我的cgi脚本开始如下

#!/usr/bin/env python

import json
from collections import defaultdict
import cgi
import cgitb; cgitb.enable()  # for troubleshooting
from MySQL import sql

form = cgi.FieldStorage()
mode = form.getvalue('mode')

print """Content-type: text/html\r\n"""
print """Set-Cookie: name=12345\r\n\r\n"""

However, when checking the response from the server on my Chrome..the debugger doesn't show any set-cookie header in the response part, and the set-cookie header is printed in the response part as part of the text, unlike the content-type header... 但是,当在我的Chrome浏览器上检查服务器的响应时,调试器在响应部分中未显示任何set-cookie标头,并且set-cookie标头作为文本的一部分打印在响应部分中,与内容类型标题...

Any idea what I did wrong? 知道我做错了什么吗?

Thanks guys 多谢你们

The problem is that you are appending two newlines instead of one to the end of your "Content-Type" header, which is the standard separator between HTTP headers and body. 问题是,要附加两个新行,而不是一个你的“内容类型”头,这是HTTP标头和身体之间的标准分隔的结束。

You should instead be emitting \\r\\n at the end of each header value (as opposed to \\n\\n ) 而应该被发射\\r\\n在每个报头值的末尾(而不是\\n\\n

When you are done emitting headers, you should print one extra \\r\\n to begin your message body. 当您完成发射头,您应打印一个额外的\\r\\n开始你的邮件正文。

EDIT: Additionally, you should either be using sys.stdout.write(...) or appending a trailing comma to your print statement to avoid its inserting the implicit newline. 编辑:此外,您应该使用sys.stdout.write(...)或在打印语句后附加逗号,以避免其插入隐式换行符。 Eg, 例如,

print "Header: Value\\r\\n",

您应该在Content-type:text / html行之前打印Set-Cookie行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM