html - python dict object has not attribute read -
i want image file web,and send api_search_handler can not change code of. remove uploadimagehandler can that,but want use middle handler receive call api_handler image , show in web.can without uploadimagehandler? or how should fix error , send correct data type api_handler? limit of code of api_handler fixed..
index.html
<form action="/uploadimage" enctype="multipart/form-data" method="post"> <p><input type='file' multiple="true" name="file" id="file"/></p> <p><span>:</span><input type="int" size="3" value="120" name="dist"/>  <span>:</span><input type="int" size="3" value="50" name="max_num" id="max_num" /></p> <p><span>:</span></p> <p>all:<input type="checkbox" name="dir" value="all" checked></p> <p><input type="submit" value="" id="submit" /></p> </form><br/><br/> class uploadimagehandler(tornado.web.requesthandler): def post(self): dist=self.get_body_argument("dist") print self.request.body max_num=self.get_body_argument("max_num") imagedir=self.get_body_argument("dir") f in self.request.files['file']: print f['filename'] url = 'http://192.168.1.234:8968' files = {'file':{"filename":f["filename"],"body":f["body"]}} data={'dir':imagedir,'dist':dist,'max_num':max_num} requests.post( url + r'/api/search' ,data=data,files = files) self.write("save ok")
error:
traceback (most recent call last): file "/usr/lib64/python2.7/site-packages/tornado/web.py", line 1346, in _execute result = method(*self.path_args, **self.path_kwargs) file "/home/zhengminpeng/python-test/web/handlers/index.py", line 24, in post requests.post( url + r'/api/search' ,data=data,files = files) file "/usr/lib/python2.7/site-packages/requests/api.py", line 108, in post return request('post', url, data=data, json=json, **kwargs) file "/usr/lib/python2.7/site-packages/requests/api.py", line 50, in request response = session.request(method=method, url=url, **kwargs) file "/usr/lib/python2.7/site-packages/requests/sessions.py", line 450, in request prep = self.prepare_request(req) file "/usr/lib/python2.7/site-packages/requests/sessions.py", line 381, in prepare_request hooks=merge_hooks(request.hooks, self.hooks), file "/usr/lib/python2.7/site-packages/requests/models.py", line 307, in prepare self.prepare_body(data, files, json) file "/usr/lib/python2.7/site-packages/requests/models.py", line 452, in prepare_body (body, content_type) = self._encode_files(files, data) file "/usr/lib/python2.7/site-packages/requests/models.py", line 150, in _encode_files fdata = fp.read() attributeerror: 'dict' object has no attribute 'read'
api_search_handler
class api_search_handler(tornado.web.requesthandler): @tornado.web.asynchronous def post(self): print 'async search' dist = self.get_argument('dist') if dist none: dist = 81.0 num = self.get_argument('max_num') if num none: num = 6 subdir = self.get_argument('dir') if subdir == '': subdir = 'any' logger.info('dist=%s, num=%s' %(dist, num)) f in self.request.files['file']: print f['filename'] logfile = open(config.log_dir_path + 'log_' + str(time.clock())+'_'+f['filename'], 'w') logfile.write(f['body']) logfile.close() featureurl = 'http://' + config.feature_svr_addr+ ':' + str(config.feature_svr_port) featureurl = featureurl + featuresvr_urls.search_feature_handle_url featureurl = featureurl + ('?filename=%s&dist=%d&max_num=%d&dir=%s' % (f['filename'], int(dist), int(num), subdir)) def on_response(response): self.write(response.body) self.finish() req = tornado.httpclient.httprequest(featureurl,method='post', body=f['body']) http = tornado.httpclient.asynchttpclient() http.fetch(req, callback=on_response) break
the error occurring because requests doesn't expect dictionary items in files
dict.
according the documentation, using simple tuple should work:
files = {'file': (f["filename"], f["body"])}
Comments
Post a Comment