Bottleで画像を受け取る方法です
形式としては、formで添付ファイルとして来ているようなケースを想定します。
このスクリプトでは組込み機器との連携を前提にしているので投稿フォームは無し。
実際のところ
Jpeg画像を受け取るケースを考えます。
@post('/img') def receiveImg(): timestump = strftime('%m%d%H%M%S') upload = request.files.get('upload') name, ext = os.path.splitext(upload.filename) if ext not in ('.jpg','.jpeg'): return "File extension not allowed." save_path = "/tmp" upload.filename = timestump + ext file_path = "{path}/{file}".format(path=save_path, file=upload.filename) upload.save("/tmp",overwrite=True) return "" + timestump + ext + "\r\n" @get(/get) def send_image(filename): return static_file(filename, root='/tmp', mimetype='image/jpeg')
動作確認はこんな塩梅です
$ curl -F "upload=@/PATH/TO/FILE.EXT" http://localhost:8080/img