我有在Python它处理从卷曲KeyError异常在Python HTTP服务器处理卷曲文件上传时请
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()
文件上传请求,现在我必须使用卷曲发送请求的HTTP服务器,命令
curl -i -F name=photofile -F [email protected] http://server_ip:port/api/v1/addphoto
但是服务器报告错误
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'
这里有什么问题吗?
你None'的'比较re.search的'结果()'是愚蠢的。将其更改为'如果re.search(...)不是None:'或plain'if re.search(...):' – msvalkon 2014-09-02 06:42:03
确定,已编辑。我是Python新手,代码来自Web教程。有关错误的任何想法? – mrmoment 2014-09-02 06:53:51