Uploading an image from python client to a flask server -
i using requests
, requests_toolbelt
send image cloud , far have this
import requests import json requests_toolbelt import multipartencoder m = multipartencoder( fields={"user_name":"tom", "password":"tom", "method":"login", "location":"landing", "cam_id":"c00001", "datetime":"hammatime!" ,'image': ('filename', open('image.jpg', 'rb'))} ) r = requests.post(url, data=m) print r.text
after gets server, how dictionary of usable? toolbelt docs show how post, not how handle on other end. advice?
you can see working example of flask server accepts posts on you're trying make on httpbin. if like:
m = multipartencoder(fields=your_fields) r = requests.post('https://httpbin.org/post', data=m, headers={'content-type': m.content_type}) print(r.json()['form'])
you'll see in post should in dictionary.
using httpbin's source, can see form
section generated request.form
. can use retrieve rest of data. can use request.files
access image wish upload.
the example flask route handler like:
@app.route('/upload', methods=['post']) def upload_files(): resp = flask.make_response() if authenticate_user(request.form): request.files['image'].save('path/to/file.jpg') resp.status_code = 204 else: resp.status_code = 411 return resp
you should read uploading files documentation though. invaluable when using common patterns in flask.
Comments
Post a Comment