en:python:webapi
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:python:webapi [2021/04/14 23:29] – lonclegr | en:python:webapi [2021/04/25 22:58] (current) – [Architecture] lonclegr | ||
|---|---|---|---|
| Line 12: | Line 12: | ||
| Before going further: yes I know that a lot of solutions may solve this issue but the target is to make the POC in less than one hour. As I used to develop Web Restful API then it is my choice. | Before going further: yes I know that a lot of solutions may solve this issue but the target is to make the POC in less than one hour. As I used to develop Web Restful API then it is my choice. | ||
| + | ===== Architecture ===== | ||
| + | {{ : | ||
| + | |||
| + | {{ : | ||
| ===== Client side ===== | ===== Client side ===== | ||
| Line 18: | Line 22: | ||
| <file powershell client_side.ps1 > | <file powershell client_side.ps1 > | ||
| + | # init | ||
| $who=whoami | $who=whoami | ||
| $computer=hostname | $computer=hostname | ||
| $server=" | $server=" | ||
| - | Invoke-WebRequest (" | + | # before the main command, let's call the API |
| + | Invoke-WebRequest (" | ||
| + | |||
| + | # main command | ||
| + | my_action | ||
| + | |||
| + | # after the main command, let's call the API | ||
| + | Invoke-WebRequest (" | ||
| </ | </ | ||
| Line 30: | Line 43: | ||
| StatusCode | StatusCode | ||
| StatusDescription : Created | StatusDescription : Created | ||
| - | Content | + | Content |
| " | " | ||
| Line 40: | Line 53: | ||
| Server: gunicorn/ | Server: gunicorn/ | ||
| - | {" | + | {" |
| Forms : {} | Forms : {} | ||
| Headers | Headers | ||
| Line 50: | Line 63: | ||
| RawContentLength | RawContentLength | ||
| </ | </ | ||
| + | |||
| + | If you are running linux laptop you can adapt the script with curl. | ||
| + | |||
| + | <code bash> | ||
| + | curl -i -XPOST --silent http:// | ||
| + | HTTP/1.1 201 CREATED | ||
| + | Server: gunicorn | ||
| + | Date: Fri, 16 Apr 2021 03:07:27 GMT | ||
| + | Connection: close | ||
| + | Content-Type: | ||
| + | Content-Length: | ||
| + | |||
| + | {" | ||
| + | </ | ||
| + | ===== Server side ===== | ||
| + | |||
| + | ==== Run it ==== | ||
| + | |||
| + | |||
| + | <code bash> | ||
| + | python3 -m venv venv | ||
| + | source venv/ | ||
| + | pip install -r requirements.txt | ||
| + | gunicorn | ||
| + | </ | ||
| + | ==== Logger ==== | ||
| + | |||
| + | <file python logger.py> | ||
| + | import logging | ||
| + | |||
| + | logger = logging.getLogger(' | ||
| + | logger.setLevel(logging.DEBUG) | ||
| + | # create file handler which logs even debug messages | ||
| + | fh = logging.FileHandler(' | ||
| + | fh.setLevel(logging.INFO) | ||
| + | # create console handler with a higher log level | ||
| + | ch = logging.StreamHandler() | ||
| + | ch.setLevel(logging.DEBUG) | ||
| + | # create formatter and add it to the handlers | ||
| + | formatter = logging.Formatter( | ||
| + | ' | ||
| + | ) | ||
| + | ch.setFormatter(formatter) | ||
| + | fh.setFormatter(formatter) | ||
| + | # add the handlers to logger | ||
| + | logger.addHandler(ch) | ||
| + | logger.addHandler(fh) | ||
| + | </ | ||
| + | |||
| + | ==== WSGI ==== | ||
| + | |||
| + | <file python wsgi.py> | ||
| + | from api import app | ||
| + | |||
| + | if __name__ == " | ||
| + | app.run() | ||
| + | </ | ||
| + | ==== API ==== | ||
| + | |||
| + | <file python api.py> | ||
| + | from flask import Flask, request | ||
| + | from flask_restful import Resource, Api | ||
| + | from logger import logger | ||
| + | |||
| + | |||
| + | app = Flask(__name__) | ||
| + | api = Api(app) | ||
| + | |||
| + | |||
| + | class HelloWorld(Resource): | ||
| + | def post(self, user, action, computer): | ||
| + | logger.info(f" | ||
| + | return { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | |||
| + | api.add_resource( | ||
| + | HelloWorld, | ||
| + | '/ | ||
| + | ) | ||
| + | |||
| + | if __name__ == ' | ||
| + | app.run(debug=True, | ||
| + | </ | ||
| + | |||
| + | ==== Requirements ==== | ||
| + | |||
| + | <file text requirements.txt> | ||
| + | flake8 | ||
| + | flask_restful | ||
| + | gunicorn | ||
| + | </ | ||
en/python/webapi.1618457363.txt.gz · Last modified: by lonclegr
