{"id":408675,"date":"2024-06-29T20:18:46","date_gmt":"2024-06-29T20:18:46","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=408675"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=408675","title":{"rendered":"<span>\u0412\u0441\u0451 \u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 cookie \u0432 Python \u2014 \u043a\u043b\u0430\u0441\u0441 http.cookies<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/bx\/ns\/8n\/bxns8nw2tvxowzgacx7cevpmgsm.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/bx\/ns\/8n\/bxns8nw2tvxowzgacx7cevpmgsm.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0434\u0443\u043b\u044c <code>http.cookies<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0430\u0440\u0441\u0435\u0440 \u0434\u043b\u044f cookie, \u043f\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 <a href=\"https:\/\/tools.ietf.org\/html\/rfc2109.html\">RFC 2109<\/a> \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 cookie \u0438 \u0441\u043c\u0435\u0436\u043d\u044b\u043c\u0438 \u0432\u0435\u0449\u0430\u043c\u0438. <\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0443\u0442\u044c \u043c\u0435\u043d\u0435\u0435 \u0441\u0442\u0440\u043e\u0433\u0430\u044f, \u0447\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442, \u0442\u0430\u043a \u043a\u0430\u043a Microsoft Internet Explorer 3.0x, \u0430 \u043f\u043e\u0437\u0436\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u044b \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 cookie.<\/p>\n<p>  <\/p>\n<h1 id=\"sozdanie-i-ustanovka-znacheniya-cookie\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f Cookie<\/h1>\n<p>  <\/p>\n<p>Cookie \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u043d\u0438 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f, \u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c, \u0430 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#  http_cookies_setheaders.py from http import cookies  c = cookies.SimpleCookie() c['mycookie'] = 'cookie_value' print(c)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0432\u043e\u0434\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0439 <code>Set-Cookie<\/code> \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a, \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c HTTP \u043e\u0442\u0432\u0435\u0442\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ python3 http_cookies_setheaders.py  Set-Cookie: mycookie=cookie_value<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"morsel\">Morsel<\/h1>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 cookie, \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u043f\u0443\u0442\u044c \u0438 \u0434\u043e\u043c\u0435\u043d. \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435\u043c\u0438 RFC \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Morsel<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#  http_cookies_Morsel.py from http import cookies import datetime  def show_cookie(c):     print(c)     for key, morsel in c.items():         print()         print('key =', morsel.key)         print('  value =', morsel.value)         print('  coded_value =', morsel.coded_value)         for name in morsel.keys():             if morsel[name]:                 print('  {} = {}'.format(name, morsel[name]))  c = cookies.SimpleCookie()  # Cookie \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043e, # \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c\u0441\u044f \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a c['encoded_value_cookie'] = '\"cookie,value;\"' c['encoded_value_cookie']['comment'] = 'Has escaped punctuation'  # \u0410 \u044d\u0442\u0430 cookie \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043a \u0447\u0430\u0441\u0442\u0438 \u0441\u0430\u0439\u0442\u0430 c['restricted_cookie'] = 'cookie_value' c['restricted_cookie']['path'] = '\/sub\/path' c['restricted_cookie']['domain'] = 'PyMOTW' c['restricted_cookie']['secure'] = True  # \u042d\u0442\u0430 cookie \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 5 \u043c\u0438\u043d\u0443\u0442 c['with_max_age'] = 'expires in 5 minutes' c['with_max_age']['max-age'] = 300  # seconds  # Cookie \u0438\u0441\u0442\u0435\u043a\u0430\u0435\u0442 \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f c['expires_at_time'] = 'cookie_value' time_to_live = datetime.timedelta(hours=1) expires = (datetime.datetime(2009, 2, 14, 18, 30, 14) +            time_to_live)  # \u0424\u043e\u0440\u043c\u0430\u0442: Wdy, DD-Mon-YY HH:MM:SS GMT expires_at_time = expires.strftime('%a, %d %b %Y %H:%M:%S') c['expires_at_time']['expires'] = expires_at_time  show_cookie(c)<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0434\u0432\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f cookie \u0441 \u0434\u0430\u0442\u043e\u0439 \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u043e\u043a\u0430 \u0436\u0438\u0437\u043d\u0438. \u041e\u0434\u0438\u043d \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 <code>max-age<\/code> \u043a\u0430\u043a \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0435\u043a\u0443\u043d\u0434 \u0436\u0438\u0437\u043d\u0438 cookie \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0434\u0440\u0443\u0433\u043e\u0439 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>expires<\/code> \u043a\u0430\u043a \u0434\u0430\u0442\u0443 \u0438 \u0432\u0440\u0435\u043c\u044f, \u043a\u043e\u0433\u0434\u0430 cookie \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ python3 http_cookies_Morsel.py  Set-Cookie: encoded_value_cookie=\"\\\"cookie\\054value\\073\\\"\"; Comment=\"Has escaped punctuation\" Set-Cookie: expires_at_time=cookie_value; expires=Sat, 14 Feb 2009 19:30:14 Set-Cookie: restricted_cookie=cookie_value; Domain=PyMOTW; Path=\/sub\/path; Secure Set-Cookie: with_max_age=\"expires in 5 minutes\"; Max-Age=300  key = encoded_value_cookie   value = \"cookie,value;\"   coded_value = \"\\\"cookie\\054value\\073\\\"\"   comment = Has escaped punctuation  key = restricted_cookie   value = cookie_value   coded_value = cookie_value   path = \/sub\/path   domain = PyMOTW   secure = True  key = with_max_age   value = expires in 5 minutes   coded_value = \"expires in 5 minutes\"   max-age = 300  key = expires_at_time   value = cookie_value   coded_value = cookie_value   expires = Sat, 14 Feb 2009 19:30:14<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Cookie<\/code> \u0438 <code>Morsel<\/code> \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u0441\u043b\u043e\u0432\u0430\u0440\u0438. <code>Morsel<\/code> \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439: <code>expires<\/code>, <code>path<\/code>, <code>comment<\/code>, <code>domain<\/code>, <code>max-age<\/code>, <code>secure<\/code>, <code>version<\/code>. <\/p>\n<p>  <\/p>\n<h1 id=\"zakodirovannye-znacheniya\">\u0417\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f<\/h1>\n<p>  <\/p>\n<p>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>cookie<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u043e\u043d \u0431\u044b\u043b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0435\u043d.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#  http_cookies_coded_value.py from http import cookies  c = cookies.SimpleCookie() c['integer'] = 5 c['with_quotes'] = 'He said, \"Hello, World!\"'  for name in ['integer', 'with_quotes']:     print(c[name].key)     print('  {}'.format(c[name]))     print('  value={!r}'.format(c[name].value))     print('  coded_value={!r}'.format(c[name].coded_value))     print()<\/code><\/pre>\n<p>  <\/p>\n<p><code>Morsel.value<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c cookie, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a <code>Morsel.coded_value<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443. \u041e\u0431\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438, \u0431\u0443\u0434\u0443\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u044b \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u0442\u0438\u043f.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ python3 http_cookies_coded_value.py  integer   Set-Cookie: integer=5   value='5'   coded_value='5'  with_quotes   Set-Cookie: with_quotes=\"He said\\054 \\\"Hello\\054 World!\\\"\"   value='He said, \"Hello, World!\"'   coded_value='\"He said\\\\054 \\\\\"Hello\\\\054 World!\\\\\"\"'<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"poluchenie-i-parsing-zagolovkov-cookie\">\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432 Cookie<\/h1>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a <code>Set-cookie<\/code> \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b \u043a\u043b\u0438\u0435\u043d\u0442\u0443, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0438\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Cookie<\/code>. \u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e cookie, \u0440\u0430\u0437\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c\u0438 <code>;<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">Cookie: integer=5; with_quotes=\"He said, \\\"Hello, World!\\\"\"<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430, cookie \u0434\u043e\u0441\u0442\u0443\u043f\u044b \u043b\u0438\u0431\u043e \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430, \u043b\u0438\u0431\u043e \u0438\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>HTTP_COOKIE<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#  http_cookies_parse.py from http import cookies  HTTP_COOKIE = '; '.join([     r'integer=5',     r'with_quotes=\"He said, \\\"Hello, World!\\\"\"', ])  print('From constructor:') c = cookies.SimpleCookie(HTTP_COOKIE) print(c)  print() print('From load():') c = cookies.SimpleCookie() c.load(HTTP_COOKIE) print(c)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0445 \u0440\u0430\u0441\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u043f\u0435\u0440\u0435\u0434\u0430\u0439\u0442\u0435 \u043d\u0443\u0436\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c c\u0442\u0440\u043e\u043a\u0438 \u0432 <code>SimpleCookie<\/code>, \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 <code>load()<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ python3 http_cookies_parse.py  From constructor: Set-Cookie: integer=5 Set-Cookie: with_quotes=\"He said, \\\"Hello, World!\\\"\"  From load(): Set-Cookie: integer=5 Set-Cookie: with_quotes=\"He said, \\\"Hello, World!\\\"\"<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"alternativnye-formaty-vyvoda\">\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u044b \u0432\u044b\u0432\u043e\u0434\u0430<\/h1>\n<p>  <\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 <code>Set-Cookie<\/code>, \u0441\u0435\u0440\u0432\u0435\u0440\u044b \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 JavaScript, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 cookie \u043a\u043b\u0438\u0435\u043d\u0442\u0443. <code>SimpleCookie<\/code> \u0438 <code>Morsel<\/code> \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442 JavaScript \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 <code>js_output()<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#  http_cookies_js_output.py from http import cookies import textwrap  c = cookies.SimpleCookie() c['mycookie'] = 'cookie_value' c['another_cookie'] = 'second value' js_text = c.js_output() print(textwrap.dedent(js_text).lstrip())<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0442\u0435\u0433 <code>script<\/code> \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 cookie.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">$ python3 http_cookies_js_output.py  &lt;script type=\"text\/javascript\"> &lt;!-- begin hiding document.cookie = \"another_cookie=\\\"second value\\\"\"; \/\/ end hiding --> &lt;\/script>  &lt;script type=\"text\/javascript\"> &lt;!-- begin hiding document.cookie = \"mycookie=cookie_value\"; \/\/ end hiding --> &lt;\/script><\/code><\/pre>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/db\/nm\/bq\/dbnmbq2bop7_uh7jof1hukxf_0w.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/db\/nm\/bq\/dbnmbq2bop7_uh7jof1hukxf_0w.jpeg\" data-blurred=\"true\"\/><\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/timeweb.com\/ru\/?utm_source=habr&amp;utm_medium=banner&amp;utm_campaign=10S&amp;utm_content=low\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/wo\/qf\/6c\/woqf6cweprtwtuq2m7jch92h5wk.png\" data-src=\"https:\/\/habrastorage.org\/webt\/wo\/qf\/6c\/woqf6cweprtwtuq2m7jch92h5wk.png\"\/><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/564620\/\"> https:\/\/habr.com\/ru\/articles\/564620\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/bx\/ns\/8n\/bxns8nw2tvxowzgacx7cevpmgsm.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/bx\/ns\/8n\/bxns8nw2tvxowzgacx7cevpmgsm.jpeg\" data-blurred=\"true\"\/> <\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0434\u0443\u043b\u044c <code>http.cookies<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0430\u0440\u0441\u0435\u0440 \u0434\u043b\u044f cookie, \u043f\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 <a href=\"https:\/\/tools.ietf.org\/html\/rfc2109.html\">RFC 2109<\/a> \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 cookie \u0438 \u0441\u043c\u0435\u0436\u043d\u044b\u043c\u0438 \u0432\u0435\u0449\u0430\u043c\u0438. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-408675","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/408675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=408675"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/408675\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}