1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3: クッキーの使い方

Last updated at Posted at 2020-08-14

こちらと同じことを、Python3 で行いました。
jquery-cookie の使い方
Perl: クッキーの使い方
PHP: クッキーの使い方

クッキーを送る

cookie_put.py
#! /usr/bin/python
#
#	cookie_put.py
#
#					Apr/27/2021
# ------------------------------------------------------------------
from http import cookies

C = cookies.SimpleCookie()
C["message"] = "This is a Test by Python."
C["aa"] = "富士山 by Python"
C["bb"] = "谷川岳 by Python"
C["cc"] = "阿蘇山 by Python"

print(C.output())


print("Content-Type: text/html")
print("")
print("<!DOCTYPE html>")
print("<html lang=\"ja\">")
print("<body>")
print("<p>*** cookie_put.py ***</p>")
print("<p>Apr/27/2021</p>")
print("</body>")
print("</html>")
# ------------------------------------------------------------------

実行結果
cookie_put_python.png

クッキーを確認

cookie_get.py
#! /usr/bin/python
#
#	cookie_get.py
#
#					Apr/27/2021
# ------------------------------------------------------------------
import os
from http import cookies

cookie = cookies.SimpleCookie()
cookie.load(os.environ["HTTP_COOKIE"])

print("Content-Type: text/html")
print("")

print("<!DOCTYPE html>")
print("<html lang=\"ja\">")
print("<head>")
print("<meta http-equiv=\"CONTENT-TYPE\" content=\"text/html; charset=utf-8\" />")
print("</head>")
print("<body>")
print("<p>*** cookie_get.py *** start ***</p>")
print("message: " + cookie["message"].value + "<br />")
print("aa: " + cookie["aa"].value + "<br />")
print("bb: " + cookie["bb"].value + "<br />")
print("cc: " + cookie["cc"].value + "<br />")
#
# print(session.cookies.get_dict())
print("<p>*** cookie_get.py *** end ***</p>")
print("<p>Apr/27/2021</p>")
print("</body>")
print("</html>")
# ------------------------------------------------------------------

実行結果
cookie_get_python.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?