简体   繁体   中英

KeyError in Python HTTP server when handling curl file upload requests

I have a HTTP server in Python which handles file upload requests from curl

class HTTPRequestHandler(BaseHTTPRequestHandler):
    def do_POST(self):
            if re.search('/api/v1/addphoto', self.path):
                    form_data=cgi.FieldStorage()
                    file_data=form_data['photofile'].value 
                    # Next I will save this file 
                    # fp =open('some/file','wb')
                    # fp.write(file_data)
                    # fp.close()

Now I have to use curl to send the request, and the command is

curl -i -F name=photofile -F filedata=@01.jpeg http://server_ip:port/api/v1/addphoto

But the server report error

File "./apiserver.py", line 21, in do_POST
    file_data=form_data['photofile'].value
File "/usr/lib/python2.7/cgi.py", line 541, in __getitem__
    raise KeyError, key
KeyError: 'photofile'

What is the problem here?

KeyError is raised when no key found in the dictionary.

I'm not familiar with cgi.FieldStorage but I guess you're expected to get a dictionary and the key 'photofile' is missing. Just print the entire form_data and see it's content.

You can use form_data.get('photofile', '') to get default value and avoid the exception.

BTW, for writing to a file you can use with statement as a context manager.

with open('some/file','wb') as fp:
    fp.write(file_data)

OK, I find the reason and how to do it from here .

The reason is "You can't embed the CGI code in the server code like that. The whole point of CGIHTTPServer (and CGI in general) is that it executes separate scripts in designated directories and returns the output. Because you had overridden the do_POST method, the code to parse the form data was never called ." (Actually I print the form_data and it is empty - " None, None, [] ").

And to fix it, use passed in parameters ( details )

 form = cgi.FieldStorage(
        fp=self.rfile, 
        headers=self.headers,
        environ={'REQUEST_METHOD':'POST',
                 'CONTENT_TYPE':self.headers['Content-Type'],
                 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM