python - Return binary and parametric data from bottle web service -


i trying write web service performs data processing. requests contain data vector binary file, , meta-data , processing parameters form data. example using python requests:

import numpy np import requests  url = 'http://localhost:8080/pp' payload = {'param_a': 4, 'param_b': -2.1} file = {'binary_data': ('file_name.bin', bytes(np.random.randn(1000))}  r = requests.post(url, data=payload, files=file) 

now on service side, have:

import bottle import numpy np  @bottle.post('/pp') def pp():      file_path = '/home/generic_user/howhaveyou.bin'     return_file_path = '/home/generic_user/finethanks.bin'      bin_file = bottle.request.files.get('binary_data')     bin_file.save(file_path, overwrite=true)     param_a = float(bottle.request.forms.get('param_a')     param_b = float(bottle.request.forms.get('param_b')      data_vector = np.fromfile(file_path)     processed_data_vector = (data_vector-param_a)*param_b     processed_data_mean = np.mean(processed_data_vector)     processed_data_samples = len(processed_data_vector)      return_metrics = {'mean': processed_data_mean,                        'n_of_samples': processed_data_samples}     open(return_file_path, 'wb') return_file:         return_file.write(bytes(processed_data_vector))      return return_metrics, bottle.static_file(return_file_path, '/') 

which doesn't quite work. either of returns work on own, following response:

<!doctype html public "-//ietf//dtd html 2.0//en"> <html>     <head>         <title>error: 500 internal server error</title>         <style type="text/css">           html {background-color: #eee; font-family: sans;}           body {background-color: #fff; border: 1px solid #ddd;                 padding: 15px; margin: 15px;}           pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}         </style>     </head>     <body>         <h1>error: 500 internal server error</h1>         <p>sorry, requested url <tt>&#039;http://localhost:8080/pp&#039;</tt>            caused error:</p>         <pre>unsupported response type: &lt;class &#039;dict&#039;&gt;</pre>     </body> </html> 

i have complete lack of experience web services, don't know if i'm on right track @ all. point wish return binary data, along few (preferably named) metrics of said data. possible using bottle only? there best practice (with respect web services, python or both) should follow when comes kind of thing?

note client not written in python, test case.

thanks guidance!

the problem server-side code cannot return dictionary (return_metrics) , contents of file (return_file_path) same response. solution encode file contents in string , include in returned dict (return_metrics). then, client need decode content string access data.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -