Q:

Parsing multipart request on Python server

I am struggling with a problem with parsing multipart request on Python.  I've created a server using http.server and trying to handle such requests as GET, POST ans POST multipart.  GET and POST are Ok, but when I try to extract data from multipart requset I'm getting this:
line 220, in parse_multipart
    headers['Content-Length'] = pdict['CONTENT-LENGTH']
KeyError: 'CONTENT-LENGTH'

My purpose is to get an image through multipart request. 
Here's the piece of code I'm using:
  def do_POST(self):
        self.send_response(200)
        self.send_header('content-type', 'text/html')
        self.end_headers()
        ctype, pdict = cgi.parse_header(self.headers['content-type'])
        if ctype == 'multipart/form-data':
            print("multipart")
            ctype, pdict = cgi.parse_header(self.headers['content-type'])
            pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
            if ctype == 'multipart/form-data':
               fields = cgi.parse_multipart(self.rfile, pdict) # the problem is here
        else:
            print("no-multipart")
            messagecontent = "messagecontent"

A:

The HTTP 1.1 spec requires a message with a body - in this case the incoming POST request - to have a content-length or transfer-encoding header. 
The cgi module expects that the request will have a content-length header, and is crashing because it isn't present.  
The request is malformed, so if you're not generating the request yourself your server should catch the exception and return a 400 Bad Request response in this situation.   If you are generating the request yourself, you need to provide a content-length header. 