我想从web获取图像文件,然后发送到API_Search_Handler,我无法更改代码。删除UploadImageHandler我可以这样做,但我想使用中间处理程序从API_handler接收回调,这是图像,然后在web中显示。我可以不使用UploadImageHandler吗? 或者我该如何修复错误并将正确的数据类型发送到API_handler? API_handler代码的限制是固定的..
的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")
for 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")
错误:
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 is None:
dist = 81.0
num = self.get_argument('max_num')
if num is None:
num = 6
subDir = self.get_argument('dir')
if subDir == '':
subDir = 'any'
logger.info('dist=%s, num=%s' %(dist, num))
for 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
答案 0 :(得分:0)
发生错误是因为请求不期望files = {'file': (f["filename"], f["body"])}
dict中的项目的字典。
根据http://mikerobbins.co.uk/2015/07/29/sitecore-razor-glass-attribute-helper-methods-fluent-api/,使用一个简单的元组应该可以工作:
@echo off
for /f "tokens=*" %%a in ('findstr "W3SVCWINSRVRHOSTS" "C:\Data\SiebelAdmin\Commands\file.txt"') do (
for /f "tokens=2 delims==" %%b in ("%%a") do (
for %%c in (%%b) do (
echo in loop
set str=%%c
echo %%c
echo.%str%
set str=%str:~-6%
echo.%str%
)))