Way to fix "KeyError: 'CONTENT-LENGTH'" when using Python3.7
Opened this issue · 1 comments
Kamilet commented
When using latest version of Python (3.7.4 for now), the following error will occor when uploading images.
File "...\lib\cgi.py", line 220, in parse_multipart
headers['Content-Length'] = pdict['CONTENT-LENGTH']
KeyError: 'CONTENT-LENGTH'
The same problem won't happen in Python 3.6, because 'CONTENT-LENGTH' isn't necessary. But in Python 3.7 it is.
I found a way to temporary fix it for Python 3.7.
- open 'server.py' and add a line under def parse_POST(self):
def parse_POST(self):
ctype, pdict = parse_header(self.headers['content-type'])
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
# Added 1 debug line here
pdict['CONTENT-LENGTH'] = int(self.headers['content-length'])
if ctype == 'multipart/form-data':
postvars = parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(
self.rfile.read(length),
keep_blank_values=1)
else:
postvars = {}
return postvars
- Change line
id_str = re.sub(r'\W+', '', id_str.decode())
in def do_POST(self):
def do_POST(self):
self.log_t()
form = self.parse_POST()
self.log_t()
if "id" in form:
id_str = form["id"][0]
# Changed 1 line here
# original line:
# id_str = re.sub(r'\W+', '', id_str.decode())
id_str = re.sub(r'\W+', '', id_str())
else:
self.ret_result(False)
return
Then it's good to go.
Successed with: Windows 10, Python 3.7.4 , CUDA 10.1