title: Cookies and HTTP with Python
body: <p>I wish to "retrieve" the cookies sent by the client in my subclass of <code>BaseHTTPRequestHandler</code>.</p>
  
  <p>Firstly I'm unsure of the exact sequence of sending of headers, in a typical HTTP request and response this is my understanding of the sequence of events:</p>
  
  <ol>
  <li>Client sends request (method, path, HTTP version, host, and ALL headers).</li>
  <li>The server responds with a response code, followed by a bunch of headers of its own.</li>
  <li>The server then sends the body of the response.</li>
  </ol>
  
  <p>When exactly is the client's POST data sent? Does any overlapping occur in this sequence as described above?</p>
  
  <p>Second, when is it safe to assume that the "Cookie" header has been received by the server. Should all of the client headers have been received by the time <code>self.send_response</code> is called by the server? When in the HTTP communication is the appropriate time to peek at cookie headers in <code>self.headers</code>?</p>
  
  <p>Thirdly, what <strong>is</strong> the canonical way to parse cookies in Python. I currently believe a <code>Cookie.SimpleCookie</code> should be instantiated, and then data from the cookie headers some how fed into it. Further clouding this problem, is the Cookie classes clunkiness when dealing with the HTTPRequestHandler interfaces. Why does the output from <code>Cookie.output()</code> not end with a line terminator to fit into <code>self.wfile.write(cookie.output())</code>, or instead drop the implicitly provided header name to fit nicely into <code>self.send_header("Set-Cookie", cookie.output())</code></p>
  
  <p>Finally, the cookie classes in the <code>Cookie</code> module, give the illusion that they're dictionaries of dictionaries. Assigning to different keys in the cookie, does not pack more data into the cookie, but rather generates more cookies... all apparently in the one class, and each generating its own Set-Cookie header. What is the best practise for packing containers of values into cookie(s)?</p>