{"id":4515,"date":"2014-11-05T13:42:08","date_gmt":"2014-11-05T18:42:08","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=4515"},"modified":"2014-11-05T13:43:53","modified_gmt":"2014-11-05T18:43:53","slug":"python-cookies-and-session-management","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2014\/11\/05\/python-cookies-and-session-management\/","title":{"rendered":"Python, cookies, and session management"},"content":{"rendered":"<p>While working with an external vendor, we had a need to understand how to manage HTTP session cookies with python.<\/p>\n<p>This is simply how we did it&#8230;<\/p>\n<pre lang=\"python\">\r\nimport urllib, urllib2, cookielib, re\r\n\r\ncj = cookielib.CookieJar()\r\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\r\n\r\nhome = opener.open('https:\/\/foobar.com\/account\/signin.jsp')\r\nfor c in cj:\r\n  if c.name == \"JSESSIONID\":\r\n    jsessionid = c.value\r\n\r\nprint jsessionid\r\n\r\n\r\nurl = 'https:\/\/foobar.com\/account\/signin.jsp'\r\npage = opener.open(url).read()\r\ndynSessConf = re.findall(r'input name=\"_dynSessConf\" value=\"(.*)\" type=\"hidden\"', page)\r\n\r\ndata = urllib.urlencode({\"email\":\"a@a.com\", \r\n                         \"password\":\"barfoo\", \r\n                         \"_D:email\":\"+\",\r\n                         \"_D:password\":\"+\",\r\n                         \"_D:\/atg\/userprofiling\/ProfileFormHandler.login\":\"+\",\r\n                         \"_DARGS\":\"\/account\/signin.jsp.loginForm\",\r\n                         \"\/atg\/userprofiling\/ProfileFormHandler.login\":\"Sign+in\"})\r\n\r\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\r\nopener.addheaders.append(('User-agent', 'Mozilla\/5.0'))\r\nopener.addheaders.append(('Referer', 'https:\/\/foobar.com\/account\/signin.jsp'))\r\nopener.addheaders.append(('Cookie', 'JSESSIONID=' + jsessionid))\r\nrequest = urllib2.Request(\"https:\/\/foobar.com\/account\/actions\/login-submit.jsp?_DARGS=\/account\/signin.jsp.loginForm\", data)\r\nresponse = opener.open(request)\r\n\r\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\r\nopener.addheaders.append(('User-agent', 'Mozilla\/5.0'))\r\nopener.addheaders.append(('Referer', 'https:\/\/foobar.com\/account\/actions\/login-submit.jsp?_DARGS=\/account\/signin.jsp.loginForm'))\r\nopener.addheaders.append(('Cookie', 'JSESSIONID=' + jsessionid))\r\nrequest = urllib2.Request(\"https:\/\/foobar.com\/account\/foobaz-next.jsp\")\r\nresponse = opener.open(request)\r\n#page = opener.open(url).read()\r\n\r\nj = None\r\nfor h in response.info().headers:\r\n  if h.find(\"JSESSIONID\") > -1:\r\n    j=h\r\n\r\nif j != None:\r\n  print \"JSESSIONID changed from \",jsessionid,\" to \",j\r\nelse:\r\n  print \"JSESSIONID persisted as \",jsessionid,\" throughout the session\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While working with an external vendor, we had a need to understand how to manage HTTP session cookies with python. This is simply how we did it&#8230; import urllib, urllib2, cookielib, re cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) home =&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2014\/11\/05\/python-cookies-and-session-management\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/4515"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=4515"}],"version-history":[{"count":7,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/4515\/revisions"}],"predecessor-version":[{"id":4523,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/4515\/revisions\/4523"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=4515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=4515"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=4515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}