2022年8月10日 星期三

Using httpx in FastAPI to call Flask API

calculate.py

from flask import Flask, redirect, url_for, request
import datetime

app = Flask(__name__)

@app.route('/calculate',methods = ['POST', 'GET'])
def calculate():
   now = datetime.datetime.now()
   if request.method == 'POST':
      res = f"POST {now}"
   else:
      res = f"GET {now}"
   print(res)
   return res

if __name__ == '__main__':
   app.run(debug = True, host='0.0.0.0', port=5000)


client.py

import httpx

with httpx.Client() as client:
    r = client.post("http://127.0.0.1:5000/calculate")
    print(r.status_code)
    print(r.text)


api.py

# uvicorn main:app --reload
# pip install fastapi[all]
# pip install uvicorn

from fastapi import FastAPI
from enum import Enum
from typing import Union
import httpx

import webbrowser
webbrowser.open("http://127.0.0.1:8000")
webbrowser.open("http://127.0.0.1:8000/redoc")
webbrowser.open("http://127.0.0.1:8000/openapi.json")
webbrowser.open("http://127.0.0.1:8000/docs")

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}
    
@app.get("/pretend")
async def pretend():
    async with httpx.AsyncClient() as client:
        r = await client.post("http://127.0.0.1:5000/calculate")
        print(r.status_code)
        print(r.text)
        return r.text
   

沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...