{"id":191,"date":"2017-08-09T21:36:58","date_gmt":"2017-08-10T02:36:58","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=191"},"modified":"2019-06-15T12:22:42","modified_gmt":"2019-06-15T17:22:42","slug":"python-on-the-web-how-to-set-cookies-in-a-cgi-script","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/09\/python-on-the-web-how-to-set-cookies-in-a-cgi-script\/","title":{"rendered":"Python on the web: How to set cookies in a CGI script"},"content":{"rendered":"<p>I am working on a Python CGI web tool that allows a user to traverse through a long list of xml elements, where each element appears on the screen one at a time. The tool allows the user to make changes to editable fields and creates a report of the deltas, as well as a copy of the new edited xml.<\/p>\n<p>The tool has a log in screen\u00a0 where the user selects themself from a menu of predefined editors, and the user supplies the project password to gain access to the data.<\/p>\n<p>When the user log in is validated, the script sets a cookie in the user&#8217;s browser that consists of the user&#8217;s name, xml file, and last page number. In this post I am going to describe how to set that cookie.<\/p>\n<p>1. Setting the cookie needs to be the FIRST thing that happens in the HTTP response, every time the server responds to a GET or POST event from the browser. For example, either by:<\/p>\n<ul>\n<li>loading the URL to the python cgi script &#8211; which is a &#8216;GET&#8217;,<\/li>\n<li>or by posting something from a web form &#8211; which is a &#8216;POST&#8217;,<\/li>\n<\/ul>\n<p>2. The logic in the cgi script must be ordered to make sure this is always the case. With every HTTP response, nothing should be printed to the browser via\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Content-type: text\/html<\/code> until AFTER the cookie is taken care of.<\/p>\n<p>Here is how to set the cookie:<\/p>\n<ol>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import Cookie<\/code><\/li>\n<li>Instantiate a new <code>SimpleCookie()<\/code> object:\u00a0 <code>c = Cookie.SimpleCookie()<\/code><\/li>\n<li>Treat the SimpleCookie object as if it is a dictionary. The key will be the name of the cookie, and the value can be a string of key-value pairs, separated by pipe symbols. For example: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">c['xml_edit_tool'] = 'Chris Nielsen|xml_file=important_part6.txt|place=2'<\/code><\/li>\n<li>Finally, print the cookie object &#8216;c&#8217; to the browser. Do this <em>before <\/em>printing anything else to the browser with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">\"Content-type: text\/html\\r\\n\"<\/code><\/li>\n<\/ol>\n<p>Here is the complete code for setting the cookie:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!C:\\Python27\\python.exe -u\n\nimport Cookie\n\nc = Cookie.SimpleCookie()\nc['xml_edit_tool'] = 'Chris Nielsen|xml_file=important_part6.txt|place=2'\n\n# Output the HTTP message containing the cookie BEFORE Content-type text\/html!\nprint c\nprint \"Content-type: text\/html\\r\\n\"<\/pre>\n<p>The complete header that is sent from the server via <code>print c<\/code> looks like this:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Set-Cookie: xml_edit_tool=\"Chris Nielsen|xml_file=important_part6.txt|place=2\" <\/code><\/p>\n<p>In Firefox, you can check to make sure that your cookie is there:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-211\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies-1-300x205.png\" alt=\"\" width=\"780\" height=\"533\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies-1-300x205.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies-1-768x525.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies-1-676x462.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies-1.png 1010w\" sizes=\"auto, (max-width: 780px) 100vw, 780px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-258\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies2.1-300x44.png\" alt=\"\" width=\"784\" height=\"115\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies2.1-300x44.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies2.1-768x112.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies2.1-676x99.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies2.1.png 788w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-259\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies4.1-300x164.png\" alt=\"\" width=\"415\" height=\"227\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies4.1-300x164.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/08\/firefox_show_cookies4.1.png 414w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working on a Python CGI web tool that allows a user to traverse through a long list of xml elements, where each element appears on the screen one at a time. The tool allows the user to make changes to editable fields and creates a report of the deltas, as well as a &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/09\/python-on-the-web-how-to-set-cookies-in-a-cgi-script\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python on the web: How to set cookies in a CGI script<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,22],"tags":[16,17,4],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-python-cgi","category-python-language","tag-cgi","tag-cookies","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":33,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":2780,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/191\/revisions\/2780"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}