Create Cookies with Python
(Assume this file invoked from a HTML form with fields name, and
color)
#!/usr/bin/env python3
from http import cookies
import urllib.parse, cgi, cgitb
#get parameters
params = cgi.FieldStorage()
name = params.getvalue("name")
height = params.getvalue("height")
color = params.getvalue("color")
#set cookies
#set expiration time in 1 hour
expires = 60*60;
mycookie = cookies.SimpleCookie()
mycookie["Name"] = name
mycookie["Color"] = color
mycookie["Color"]['expires']= expires
print (mycookie) #BEFORE content-type line
print( "Content-type:text/html\n");
set10_createCookies.py – part1
print (""“
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8“>
<title>Storing cookies</title>
</head>
<body>
<h1>2 cookies were stored!</h1>
""“)
print ("<h2>Name: "+ name +
"<br /> Color: "+color + "</h2>");
print("</body></html>");
set10_createCookies.py – part2