Here's the form my web server is serving upon a GET request (plus the contents of the memory list). I'm trying to make it so that when the message is submitted, it is appended to memory. Form is as follows:
form = ''' <!DOCTYPE html><html><head><title>Server</title></head><body>
<form action="/basic-server.py" method="post">
<label>Message <input name="message" type="text" /></label>
</form>
</body></html> '''
memory = []
Now here is the content of my POST method using BasicHTTPRequestHandler:
def do_POST(self):
length = int(self.headers.get('Content-Length'))
postvars = parse.parse_qs(self.rfile.read(length))
memory.append(postvars["message"])
self.send_response(303)
self.send_header('Location', '/')
self.end_headers()
It throws a KeyError for 'message'. The thing that confuses me about this is that if I remove that line and print out postvars, it looks like a dictionary with a "message" key containing the message I type in on the form.
If I use memory.append(postvars)
and input 'test' the application works and appends the following dictionary: {b'message': [b'test']}
. What's with the 'b' characters? If I use [b'message']
as a key, it works, but returns [b'test']
to memory instead of simply test.