Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to fix "KeyError: 'CONTENT-LENGTH'" when using Python3.7 #150

Open
Kamilet opened this issue Sep 28, 2019 · 1 comment
Open

Way to fix "KeyError: 'CONTENT-LENGTH'" when using Python3.7 #150

Kamilet opened this issue Sep 28, 2019 · 1 comment

Comments

@Kamilet
Copy link

Kamilet commented Sep 28, 2019

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.

  1. 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
  1. 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

@jitvimol
Copy link

@Kamilet Can you please advise what server.py file? I search in anaconda folder and there are multiple file with this name? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants