# Python主控端程式筆記 ###### tags: `Development`,`Study` ## 目錄 [ToC] ## Flask 1. <https://blog.techbridge.cc/2017/06/03/python-web-flask101-tutorial-introduction-and-environment-setup/> 2. <https://zwindr.blogspot.com/2018/03/python-flask.html> 3. https://hackmd.io/@shaoeChen/SyP4YEnef?type=view 4. https://medium.com/@twilightlau94/rest-apis-with-flask-%E7%B3%BB%E5%88%97%E6%95%99%E5%AD%B8%E6%96%87-1-5405216d3166 5. https://www.itread01.com/content/1546450059.html (upload file) 6. https://medium.com/datainpoint/flask-web-api-quickstart-3b13d96cccc2 * __Server__ * Library ```python from flask import Flask, request, ...(any lib needed) from config import DevConfig # 自訂的config.py, 內含已定義之config_object ``` * Construction ```python app = Flask(__name__) ``` * Configuration ```python app.config[attributes] = values ``` or ```python app.config.from_object(DevConfig) ``` to load attributes from self defined object (class). All configuration attributes: <https://flask.palletsprojects.com/en/0.12.x/config/> * Adding route and func ``` @app.route(url, methods) def func(): ~ ``` url: where to access func( ). methods: how clients request to server. ('GET', 'POST' or both, default is 'GET') * __Client__ <https://requests.kennethreitz.org/en/master/api/> ```python import requests # making HTTP requests r = requests.get(url) r2 = requests.post(url, data=your_data, files=your_file) # .get to ask for response # .post to send value (and get response of needed) # .~(url, data=, ) ``` Note that data and files are passed in dict form. * __Communication__ [Flask](http://flask.palletsprojects.com/en/1.1.x/#api-reference) is built up with [Jinja](https://jinja.palletsprojects.com/en/2.10.x/) template engine and the [Werkzeug](https://www.palletsprojects.com/p/werkzeug/) WSGI toolkit. Thus, the messages passing between server and client must obey the [WSGI](https://wsgi.readthedocs.io/en/latest/learn.html) specification. + Client Client would send request to server and get the response if needed. We used [requests](https://requests.kennethreitz.org/en/master/api/) tookit to send requests obeying the HTTP protocal. There are two methods of requests: GET and POST. ```python import requests url = 'http://127.0.0.1:5000' r1 = resquests.get(url) data = {'data': 'user_info', 'user': 'c95cc', 'password': '123456'} file = {'file': [1,2,3,4,5,6]} r2 = requests(url, data=data, files=file) print(r1.text) print(r2.status) print(r2.json) ... ``` + Server ```python app = Flask() # assigning your server configuration # ... # end of assignment @app.route(url='/', methods=METHODS) # url can be modified if needed def _func(): # The action when this url is activated msg = request.json if msg['user'] == 'c95cc'and msg[passward] == '123456': return 'Legal user' # in response.txt else: return {'error':'self defined message'} # in response.json ``` * METHODS can be 'GET', 'POST' or ['GET', 'POST'] ## Encoding and Decoding Image The request/response messages is transmitted as stream, thes the encoding/decoding procedure before request/after response is needed. Flask/requests api can transmit json format directly, whie the elements in the json should be encoded and decoded before the transition. + Encoding uint8 image -> byte image -> byte string -> b64 format -> utf-8 format ```python def b64_encode(img): byte_str = cv2.imencode('.png', img)[1].tostring() b64_str = base64.b64encode(byte_img).decode('utf-8') return b64_str ``` + Decoding utf-8 format -> byte string -> np array -> uint8 image ```python def b64_decode(b64_str): byte_str = base64.b64decode(b64_str) np_img = np.fromstring(byte_str, np.uint8) img = cv2.imdecode(np_img, cv2.IMREAD_COLOR) return img ```