views:

257

answers:

1

How do I send the "Set-Cookie" header when working with a BaseHTTPServerRequestHandler, and Cookie? BaseCookie and children don't provide a method to output the value to be passed into send_header(), and *Cookie.output() does not provide a HTTP line delimiter.

Which Cookie class should I be using? Two have survived into Python3, what are the differences?

+1  A: 

Use C = http.cookie.SimpleCookie to hold the cookies and then C.output() to create the headers for it.

Example here

The request handler has a wfile attribute, which is the socket.

req_handler.send_response(200, 'OK')
req_handler.wfile.write(C.output()) # you may need to .encode() the C.output()
req_handler.end_headers()
#write body...
Tor Valamo
what about the sending?
Matt Joiner
@Tor this sending does not work, SimpleCookie does not output a complete header
Matt Joiner
Of course you need to add more headers than just the cookies... I thought that was obvious.
Tor Valamo
no, the cookie's output is not valid to write either to `self.wfile` in the handler **OR** to `self.send_header()`
Matt Joiner
Did you encode it like I said?
Tor Valamo
@Tor. I know the example you've given _looks_ right, but if you actually try it you will see what I'm talking about.
Matt Joiner
the example i've given is from the example link above it.. and the general example is taken from the source of BaseHTTPRequestHandler...
Tor Valamo