User Tools

Site Tools


en:python:webapi

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:python:webapi [2021/04/15 22:33] – [API] lonclegren: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 =====
  
 +{{ :en:python:web-api.png?600 |Architecture WebAPI}}
 +
 +{{ :en:python:web-api.dia |Source DIA}}
 ===== Client side ===== ===== Client side =====
  
Line 24: Line 28:
  
 # before the main command, let's call the API # before the main command, let's call the API
-Invoke-WebRequest ("http://{2}:5000/my_action/start/{0}/{1}" -f ($who -replace "\\", "@"), $computer, $server) -Method POST+Invoke-WebRequest ("http://{2}:5000/my_command/start/{0}/{1}" -f ($who -replace "\\", "@"), $computer, $server) -Method POST
  
 # main command # main command
Line 30: Line 34:
  
 # after the main command, let's call the API # after the main command, let's call the API
-Invoke-WebRequest ("http://{2}:5000/my_action/end/{0}/{1}" -f ($who -replace "\\", "@"), $computer, $server) -Method POST+Invoke-WebRequest ("http://{2}:5000/my_command/end/{0}/{1}" -f ($who -replace "\\", "@"), $computer, $server) -Method POST
  
 </file> </file>
Line 39: Line 43:
 StatusCode        : 201 StatusCode        : 201
 StatusDescription : Created StatusDescription : Created
-Content           : {"user": "my_user", "action": "my_action", "ip_remote": "0.1.2.3", "computer":+Content           : {"user": "my_user", "action": "start", "ip_remote": "0.1.2.3", "computer":
                     "my_computer"}                     "my_computer"}
  
Line 49: Line 53:
                     Server: gunicorn/20.0.4                     Server: gunicorn/20.0.4
  
-                    {"user": "my_user", "action": "my_acti...+                    {"user": "my_user", "action": "star...
 Forms             : {} Forms             : {}
 Headers           : {[Connection, close], [Content-Length, 115], [Content-Type, application/json], [Date, Tue, 26 May 2020 13:46:28 Headers           : {[Connection, close], [Content-Length, 115], [Content-Type, application/json], [Date, Tue, 26 May 2020 13:46:28
Line 60: Line 64:
 </code> </code>
  
 +If you are running linux laptop you can adapt the script with curl.
 +
 +<code bash>
 +curl -i -XPOST --silent http://localhost:5000/my_command/start/toto/computer
 +HTTP/1.1 201 CREATED
 +Server: gunicorn
 +Date: Fri, 16 Apr 2021 03:07:27 GMT
 +Connection: close
 +Content-Type: application/json
 +Content-Length: 86
 +
 +{"user": "toto", "action": "start", "ip_remote": "127.0.0.1", "computer": "computer"}
 +</code>
 ===== Server side ===== ===== Server side =====
  
 +==== Run it ====
 +
 +
 +<code bash>
 +python3 -m venv venv
 +source venv/bin/activate
 +pip install -r requirements.txt
 +gunicorn  --bind 0.0.0.0:5000 wsgi:app
 +</code>
 ==== Logger ==== ==== Logger ====
  
-<code python logger.py>+<file python logger.py>
 import logging import logging
  
Line 84: Line 110:
 logger.addHandler(ch) logger.addHandler(ch)
 logger.addHandler(fh) logger.addHandler(fh)
-</code>+</file>
  
 ==== WSGI ==== ==== WSGI ====
  
-<code python wsgi.py>+<file python wsgi.py>
 from api import app from api import app
  
 if __name__ == "__main__": if __name__ == "__main__":
     app.run()     app.run()
-</code>+</file>
 ==== API ==== ==== API ====
  
-<code python api.py >+<file python api.py>
 from flask import Flask, request from flask import Flask, request
 from flask_restful import Resource, Api from flask_restful import Resource, Api
Line 104: Line 130:
 app = Flask(__name__) app = Flask(__name__)
 api = Api(app) api = Api(app)
 +
  
 class HelloWorld(Resource): class HelloWorld(Resource):
-    def post(self, action, user, computer):+    def post(self, user, action, computer):
         logger.info(f"{computer},{action},{user},{request.remote_addr}")         logger.info(f"{computer},{action},{user},{request.remote_addr}")
         return {         return {
Line 113: Line 140:
             "ip_remote": request.remote_addr,             "ip_remote": request.remote_addr,
             "computer": computer             "computer": computer
-        }, 201+        }
  
-api.add_resource(HelloWorld, '/my_action/<string:action>/<string:user>/<string:computer>')+ 
 +api.add_resource( 
 +    HelloWorld, 
 +    '/my_command/<string:action>/<string:user>/<string:computer>' 
 +)
  
 if __name__ == '__main__': if __name__ == '__main__':
     app.run(debug=True, host="0.0.0.0")     app.run(debug=True, host="0.0.0.0")
-</code>+</file>
  
 +==== Requirements ====
 +
 +<file text requirements.txt>
 +flake8
 +flask_restful
 +gunicorn
 +</file>
en/python/webapi.1618540386.txt.gz · Last modified: 2021/04/15 22:33 by lonclegr