fromflaskimportFlask,Response,jsonify,requestdefdemo_post()->Response:request_dict=request.jsonor{}uid_str:str=request_dict.get("uid","")username:str=request_dict.get("username","")uid=int(uid_str)# Don't think about type conversion exceptions for nowifnot(10<uid<1000):raiseValueError("invalid uid")ifnot(2<=len(username)<=4):raiseValueError("invalid name")# get the corresponding value and return itreturnjsonify({"uid":uid,"username":username})app=Flask("demo")app.add_url_rule("/api","demo",demo_post,methods=["POST"])if__name__=="__main__":app.run(port=8000)
fromstarlette.applicationsimportStarlettefromstarlette.requestsimportRequestfromstarlette.responsesimportJSONResponsefromstarlette.routingimportRouteasyncdefdemo_post(request:Request)->JSONResponse:request_dict=awaitrequest.json()uid_str:str=request_dict.get("uid","")username:str=request_dict.get("username","")uid=int(uid_str)# Don't think about type conversion exceptions for nowifnot(10<uid<1000):raiseValueError("invalid uid")ifnot(2<=len(username)<=4):raiseValueError("invalid name")# get the corresponding value and return itreturnJSONResponse({"uid":uid,"username":username})app=Starlette(routes=[Route("/api",demo_post,methods=["POST"])])if__name__=="__main__":importuvicornuvicorn.run(app)
fromsanic.appimportSanicfromsanic.requestimportRequestfromsanic.responseimportHTTPResponse,jsonasyncdefdemo_post(request:Request)->HTTPResponse:request_dict=awaitrequest.json()uid_str:str=request_dict.get("uid","")username:str=request_dict.get("username","")uid=int(uid_str)# Don't think about type conversion exceptions for nowifnot(10<uid<1000):raiseValueError("invalid uid")ifnot(2<=len(username)<=4):raiseValueError("invalid name")# get the corresponding value and return itreturnjson({"uid":uid,"username":username})app=Sanic(name="demo")app.add_route(demo_post,"/api",methods=["POST"])if__name__=="__main__":importuvicornuvicorn.run(app)
importjsonfromtornado.ioloopimportIOLoopfromtornado.webimportApplication,RequestHandlerclassDemoHandler(RequestHandler):defpost(self)->None:request_dict=json.loads(self.request.body.decode())uid_str:str=request_dict.get("uid","")username:str=request_dict.get("username","")uid=int(uid_str)# Don't think about type conversion exceptions for nowifnot(10<uid<1000):raiseValueError("invalid uid")ifnot(2<=len(username)<=4):raiseValueError("invalid name")# get the corresponding value and return itself.write({"uid":uid,"username":username})app:Application=Application([(r"/api",DemoHandler)])if__name__=="__main__":app.listen(8000)IOLoop.instance().start()