{"id":239,"date":"2017-08-11T22:02:01","date_gmt":"2017-08-12T03:02:01","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=239"},"modified":"2018-07-28T16:31:49","modified_gmt":"2018-07-28T21:31:49","slug":"python-on-the-web-how-to-get-cookies-in-a-cgi-script","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/11\/python-on-the-web-how-to-get-cookies-in-a-cgi-script\/","title":{"rendered":"Python on the web: How to get cookies in a CGI script"},"content":{"rendered":"<p>In this previous article I detailed how to <em>set <\/em>cookies in a CGI script:<\/p>\n<blockquote data-secret=\"7XJlEOvNfI\" class=\"wp-embedded-content\"><p><a href=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/08\/09\/python-on-the-web-how-to-set-cookies-in-a-cgi-script\/\">Python on the web: How to set cookies in a CGI script<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/08\/09\/python-on-the-web-how-to-set-cookies-in-a-cgi-script\/embed\/#?secret=7XJlEOvNfI\" data-secret=\"7XJlEOvNfI\" width=\"600\" height=\"338\" title=\"&#8220;Python on the web: How to set cookies in a CGI script&#8221; &#8212; Chris Nielsen Code Walk\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>In this article I am going to describe how to <em>get<\/em> those cookies in a CGI script.<\/p>\n<ol>\n<li>Get &#8216;HTTP_COOKIE&#8217; from the Environment variables. Account for the fact that this can be either all upper case, or lower case. <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">cookie_string=os.environ.get('HTTP_COOKIE')<\/code><\/li>\n<li>Browsers generally separate cookies based on domain such that cookies for intranet location 1uslchriston.ad.here.com and cookies for the parent domain here.com are kept separately. However, the &#8216;HTTP_COOKIE&#8217; container that comes from the server groups all cookies of the same parent domain together. For example, it sees &#8216;here.com&#8217; in the intranet domain 1uslchriston.ad.here.com. Due to this fact, we can&#8217;t assume that the one cookie placed from our intranet site is going to be the only cookie in the &#8216;HTTP_COOKIE&#8217; container. Because of this, we need to first attempt to split our cookie_string by &#8216;; &#8216; like so:<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">cookies = cookie_string.split('; ')<\/code><\/li>\n<li>Continue to parse the cookies for the pattern set in the previous example. i.e. split on pipe symbol, then &#8216;=&#8217; sign.<\/li>\n<li>Since there may be more key-value pairs than we want (in this example case there were), create a list of only the keys we are interested in: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">xml_tool_cookie_keys = ['xml_edit_tool', 'xml_file', 'place']<\/code><\/li>\n<li>Then use <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0274\/\" target=\"_blank\" rel=\"noopener noreferrer\">dictionary comprehension<\/a> to create a new smaller dictionary that contains only the target keys:<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">xmlt_dict = { key: cookie_dict[key] for key in cookie_dict.keys() if key in xml_tool_cookie_keys }<\/code><\/li>\n<\/ol>\n<p>Here is the complete code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def get_cookies():\r\n    \"\"\"Return cookie values as a dictionary.\"\"\"\r\n    cookie_dict = {}\r\n    if 'HTTP_COOKIE' in os.environ or 'http_cookie' in os.environ:\r\n        try:\r\n            cookie_string=os.environ.get('HTTP_COOKIE')\r\n        except:\r\n            cookie_string=os.environ.get('http_cookie')\r\n\r\n        # there could be more than one cookie, or cookies from parent domain\r\n        # for example, cookies from 1uslchriston.ad.here.com and here.com\r\n        cookies = cookie_string.split('; ')\r\n        for c in cookies:\r\n            cookie_kv = c.split('|')\r\n            for e in cookie_kv:\r\n                e = e.replace('\"', '')\r\n                f = e.split('=')\r\n                k = f[0]\r\n                v = f[1]\r\n                cookie_dict[k] = v\r\n\r\n    xml_tool_cookie_keys = ['xml_edit_tool', 'xml_file', 'place']\r\n    xmlt_dict = { key: cookie_dict[key] for key in cookie_dict.keys() if key in xml_tool_cookie_keys }\r\n    return xmlt_dict<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this previous article I detailed how to set cookies in a CGI script: Python on the web: How to set cookies in a CGI script In this article I am going to describe how to get those cookies in a CGI script. Get &#8216;HTTP_COOKIE&#8217; from the Environment variables. Account for the fact that this &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/11\/python-on-the-web-how-to-get-cookies-in-a-cgi-script\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python on the web: How to get 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-239","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\/239","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=239"}],"version-history":[{"count":17,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/239\/revisions\/261"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}