OpenSSH %i Server Key Generation這是 sshd-keygen@.service,其中有
ConditionFileNotEmpty=|!/data/ssh/ssh_host_%i_key所以已經產生的話,不會再產生一次。
另外,產生 sshd-keygen 是 /usr/libexec/openssh/sshd-keygen
這個是 script,所以修改產生 sshd_host key 的位置。
OpenSSH %i Server Key Generation這是 sshd-keygen@.service,其中有
ConditionFileNotEmpty=|!/data/ssh/ssh_host_%i_key所以已經產生的話,不會再產生一次。
swagger-py-codegen --swagger-doc openapi.yaml example --ui --spec產生 python code
diff --git a/example/example/__init__.py b/example/example/__init__.py index fe02a18..ccf02a9 100644 --- a/example/example/__init__.py +++ b/example/example/__init__.py @@ -2,16 +2,21 @@ from __future__ import absolute_import from flask import Flask +from flask_jwt_extended import JWTManager import v1 def create_app(): app = Flask(__name__, static_folder='static') app.register_blueprint( v1.bp, url_prefix='/v1') + app.config["JWT_SECRET_KEY"] = "mysecret" + JWTManager(app) return app
diff --git a/example/example/v1/schemas.py b/example/example/v1/schemas.py index 4b20894..7e54c7c 100644 --- a/example/example/v1/schemas.py +++ b/example/example/v1/schemas.py @@ -61,7 +61,6 @@ filters = { } scopes = { - ('protect', 'GET'): ['secret'], } resolver = RefResolver.from_schema(definitions) @@ -228,4 +227,4 @@ def normalize(schema, data, required_defaults=None, resolver=None): return funcs[type_](schema, data)
diff --git a/example/example/__init__.py b/example/example/__init__.py index fe02a18..ccf02a9 100644 --- a/example/example/__init__.py +++ b/example/example/__init__.py @@ -2,16 +2,21 @@ from __future__ import absolute_import from flask import Flask +from flask_cors import CORS import v1 def create_app(): app = Flask(__name__, static_folder='static') + CORS(app)
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. swagger-py-codegen 0.4.0 requires click<7, but you have click 8.0.3 which is incompatible. connexion 2.10.0 requires PyYAML<6,>=5.1, but you have pyyaml 4.2b1 which is incompatible.但是 flask 又要 >7,所以 swagger-py-codegen 跟 flask 沒有板法一起。
POST: /login body { "username":"test", "password":"test" }send 後,得到 response:
{ "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTY0Mjc1MjcyNiwianRpIjoiODk4MzUwNDQtNjAxMi00OTlmLWIxZTItZmMyMzIwMzg3NTY0IiwidHlwZSI6ImFjY2VzcyIsInN1YiI6InRlc3QiLCJuYmYiOjE2NDI3NTI3MjYsImV4cCI6MTY0Mjc1MzYyNn0.B3F-9LJ20JvcUHNxTkrsQONVPH9DGGBaD2ggUGuPd9Q" }把 : 後面的字串 copy 下來,等下要對 /protected GET 時,要用...
GET: /protected Authorization: TYPE: Bearer Token 右邊的 Token: 把剛剛 copy 的字串 貼上send 後,response 就是
{ "logged_in_as": "test" }
from flask_jwt_extended import JWTManager from flask_jwt_extended import create_access_token from flask import Flask, jsonify, request jwt = JWTManager() app = Flask(__name__) app.config['JWT_SECRET_KEY'] = 'this-should-be-change' jwt.init_app(app) @app.route('/login',methods=['POST']) def login(): username = request.json.get('username',None) password = request.json.get('password',None) print( username, password) if username != 'test' or password != 'test': return jsonify({"msg":"Bad username or password"}), 401 access_token = create_access_token(identity=username) print(access_token) return jsonify(access_token) if __name__ == '__main__': app.run()這個就是各教學文中,最簡單的範例。
POST: http://localhost:5000/login Body: raw { "username":"test", "password":"test" }send. 就可以看到 response Body.. 是一堆 base64 文。
swagger_py_codegen --swagger-doc api.json example --ui --spec這樣就會多產生一個 static folder,裡面就是 swagger-ui 的 server side code。
google-chrome --disable-web-security --user-data-dir=./aaa用這幾個 option 啟動,就會忽略這格檢查。
for file in *.heic; do heif-convert -q 100 $file ${file/%.heic/.jpg}; done2024 update:
conda create -n flaskenv python=3.6conda repo 有 flask,沒有 swagger-py-codegen。
pip install swagger-py-codegen然後抄上面 ref的 api.json:
{ "swagger": "2.0", "info": { "version": "1.0.0", "title": "Simple API", "description": "A simple API to learn how to write OpenAPI Specification" }, "schemes": [ "https" ], "host": "simple.api", "basePath": "/openapi101", "paths": { "/persons": { "get": { "summary": "Gets some persons", "description": "Returns a list containing all persons.", "responses": { "200": { "description": "A list of Person", "schema": { "type": "array", "items": { "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "username": { "type": "string" } } } } } } } } } }就可以generate flask code:
swagger-py-codegen --swagger-doc api.json example-app接著安裝 requirement.txt 需要的module
http://localhost:5000/openapi101/persons因為 code gen 的 code 都是空的,所以內容只有 '[]',可以修改 person.py 的 get(self) 的 return 內容,
diff --git a/example-app/example_app/openapi101/schemas.py b/example-app/example_app/openapi101/schemas.py index 86571b5..7ca6cbd 100644 --- a/example-app/example_app/openapi101/schemas.py +++ b/example-app/example_app/openapi101/schemas.py @@ -226,4 +226,4 @@ def normalize(schema, data, required_defaults=None, resolver=None): return funcs[type_](schema, data) - return _normalize(schema, data), errors + return data, errors
File "/usr/lib/python3.6/json/encoder.py", line 180, in default o.__class__.__name__) TypeError: Object of type 'date' is not JSON serializable這個要修改swagger_py_codegen 的 code 才行,參考這一篇修改code, build, install。
Installing collected packages: zipp, typing-extensions, importlib-metadata, Werkzeug, itsdangerous, click, flask Attempting uninstall: click Found existing installation: click 6.7 Uninstalling click-6.7: Successfully uninstalled click-6.7 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. swagger-py-codegen 0.4.0 requires click<7, but you have click 8.1.3 which is incompatible. Successfully installed Werkzeug-2.1.2 click-8.1.3 flask-2.1.2 importlib-metadata-4.11.4 itsdangerous-2.1.2 typing-extensions-4.2.0 zipp-3.8.0這樣install flask 完,run swagger_py_codegen 會出現 error 在 click
File "/home/charles-chang/miniconda3/envs/swagger37/lib/python3.7/site-packages/click/core.py", line 760, in invoke return __callback(*args, **kwargs) TypeError: generate() got an unexpected keyword argument 'spec'所以...
File "/home/charles-chang/.local/lib/python2.7/site-packages/dpath/util.py", line 1, in <module> from collections.abc import MutableMapping ImportError: No module named abc
Traceback (most recent call last): File "/home/charles-chang/miniconda3/envs/swagger3.6/bin/swagger_py_codegen", line 5, in用 python 3.7 就沒問題。from swagger_py_codegen import generate File "/home/charles-chang/miniconda3/envs/swagger3.6/lib/python3.6/site-packages/swagger_py_codegen/__init__.py", line 4, in from .command import generate # noqa File "/home/charles-chang/miniconda3/envs/swagger3.6/lib/python3.6/site-packages/swagger_py_codegen/command.py", line 18, in from swagger_spec_validator import SwaggerValidationError File "/home/charles-chang/miniconda3/envs/swagger3.6/lib/python3.6/site-packages/swagger_spec_validator/__init__.py", line 1, in from swagger_spec_validator.common import SwaggerValidationError File "/home/charles-chang/miniconda3/envs/swagger3.6/lib/python3.6/site-packages/swagger_spec_validator/common.py", line 1 from __future__ import annotations ^ SyntaxError: future feature annotations is not defined
# packages in environment at /home/charles-chang/miniconda3/envs/swagger3.7: # # Name Version Build Channel _libgcc_mutex 0.1 main _openmp_mutex 5.1 1_gnu ca-certificates 2022.10.11 h06a4308_0 certifi 2022.9.24 py37h06a4308_0 click 6.7 pypi_0 pypi dpath 2.1.3 pypi_0 pypi jinja2 3.1.2 pypi_0 pypi json-spec 0.10.1 pypi_0 pypi jsonschema 2.6.0 pypi_0 pypi ld_impl_linux-64 2.38 h1181459_1 libffi 3.4.2 h6a678d5_6 libgcc-ng 11.2.0 h1234567_1 libgomp 11.2.0 h1234567_1 libstdcxx-ng 11.2.0 h1234567_1 markupsafe 2.1.1 pypi_0 pypi ncurses 6.3 h5eee18b_3 openssl 1.1.1s h7f8727e_0 pip 22.3.1 py37h06a4308_0 python 3.7.15 h7a1cb2a_1 pyyaml 4.2b1 pypi_0 pypi readline 8.2 h5eee18b_0 setuptools 65.5.0 py37h06a4308_0 six 1.16.0 pypi_0 pypi sqlite 3.40.0 h5082296_0 swagger-py-codegen 0.4.0 pypi_0 pypi swagger-spec-validator 3.0.3 pypi_0 pypi tk 8.6.12 h1ccaba5_0 typing-extensions 4.4.0 pypi_0 pypi wheel 0.37.1 pyhd3eb1b0_0 xz 5.2.8 h5eee18b_0 zlib 1.2.13 h5eee18b_0
aplat -D plug:dmix sample.wav這樣就可以同時播放,不會有 snd device lock 的問題。