Flaskのサンプル。
hello.py - server
from flask import * from markupsafe import escape import json app = Flask(__name__) @app.route("/") def index(): return "Index Page" @app.route("/hello_world") def hello_world(): return "<p>Hello, World!p>" @app.route("/user/<name>", methods=["GET"]) def hello(name): return jsonify({"name": f"{name}"}) @app.route("/regist_user", methods=["POST"]) def regist_user(): json = request.get_json() return jsonify({"name":json["name"]}) #=========================================================== # > flask --app hello run --port=50001 # > python hello.py # ← (\flaskproject\.venv\Scripts)で動かす #=========================================================== if __name__=='__main__': app.run(debug=True,host="0.0.0.0",port=50001)
mypost.py - client
import requests import json print("==================> script start") post_url = "http://localhost:50001/regist_user" json = { "name": "sato"} resp = requests.post( post_url, json = json ) print(resp.json()) print("==================> script end")
myget.py - client
import requests print("==================> script start") post_url = "http://localhost:50001/user/sato" print(requests.get(post_url).json()) print("==================> script end")