Package: weepy
weepyTiny python ASGI frameworkInstalling weepySimply use pip for install this package
Installation:$pipinstallweepy# starndard version# or version with developer requirements$pipinstallweepy[dev]Running development server$weepy[webserverparameters]UsingChange directory to project folder and run devserver.$weepy--host127.0.0.1--port8080--app`file`:`callable`Replacefilewith filename (without file extension) with your main function andcallablewith your function.App should run onlocalhost:8080.Command parameters--envshould contain python ini configuration file--hostshould contain IP address to bind server on--portshould contain port to be bind server on--appshould contain file and callable object separated by colon. (main:app)--pythonpathshould contain your code directory for imports in your project--daemonstarts server in deamon modeBash syntax:$weepy[--configCONFIG][--hostIP_ADDRESS][--portPORT][--appfile:callable][--pythonpathpath][--daemon]You can create server configuration in uasgi ini file.If you want to run python files without devserver add project directory toPYTHONPATH.Using weepyInitializingfromweepyimportASGIapplication=ASGI(content_type="application/json",allow="*",charset="UTF-8")ASGI parameters:content_type- specifies the default content type of generated responses (optional, default:application/json)charset- specifies the default charset generated responses (optional, default:UTF-8)allow- specifies global setting forAccess-Control-Allow-Originheader - can be overwrited by passing that header to response headers (optional, defaultNone- for public API should be set to*)RoutesRoute parametersendpoint- object should contain str or regex Pattern to match request path (mandatory)type- defines that routed is serving HTTP or WebSocket protocol (possible values:http,websocket; defalut:http)methods- list of HTTP methods to match request method - higher priority than method (optional, default: [GET], should be only be specified onhttptype routes)*kwargs - other arguments - can be accessed by midlleware in statesroutedandendcontent-typeUsingRoute is used as a decorator callable object you want to use as router method.
Router method takes two mandatory argumentsRequestandResponse(explained below in a separate section).
Router method can also take variables from router regex match (if used).HTTP ExamplesimportrefromweepyimportRoute@Route("/endpoint",methods=["GET"])# simple routeasyncdefrouted_method_1(request,response):# simple route methodreturn"Response from route 1"# response return without status code & mime type# re package import needed@Route(re.compile(r"/endpoint/(\d+)$"),methods=["GET","POST"])# regex route, with multiple methodsasyncdefrouted_method_2(request,response,number):return"Response from route - urlnumber is:%s"%number,"200 OK","text/html"# full response return@Route("/endpoint3",methods=["GET","POST"])#simple route, with multiple methodsasyncdefrouted_method_2(request,response):response.set("Response by set")# alternatively can be called reponse method set to set responseAbort HTTP responseForce http response with responseabortmethod parameters: status, message (body)fromweepyimportRoute@Route("/raise_endpoint",methods=["POST"])asyncdefrouted_method_2(request,response):ifnotrequest.data:awaitresponse.abort(400,"400 BAD REQUEST")return"data sent"Request & Response objectsInstances of these object are given to every function that is called with@RoutedecoratorfromweepyimportRoute@Route("/endpoint",methods=["GET"])asyncdefendpoint(Request,Response):# instances of Request and Response objectspassRequest items and methods (HTTP)method- request method (GET, POST etc.) : read onlypath- request path: read onlyquery_params-dictof query_string parametersdata- request parsed data (json, -www-form-urlencoded, multipart/form-data) : read onlyheaders- HTTP headers : read only (may containcontent_lengthandcontent_typein post requests)cookies- dict containsSimpleCookieobject of every cookie loadedscope- raw asgi scope obejectevent- raw asgi event objectResponse  items and methods (HTTP)content_type- response content-type : read only - can be specified in routecharset- response encoding; default:UTF-8headers-listof response headersredirect(location, status)- redirects response to (location,statusif not providet is set to302)set_cookie(name, value, expires=None, maxAge=None, **kwargs)- adds cookie to response with obvious parameres, you can alwo add additional arguments (kwargs) such asDomain,Path,Secure,HttpOnlyadd_header(name, value)- adds header to responseprocess(data, status=200)- allow to manual process response (body, status[optional, default=200] )abort(400, body="400 BAD REQUEST")- force HTTP response (status, body[optional, default=b""])ExampleSave this file as main.pyfromweepyimportASGI,Routeapplication=ASGI(content_type="text/html")@Route("/endpoint",methods=["GET","POST"])asyncdefendpoint(Request,Response):Response.add_header("X-header","blahblah")# set response headerResponse.set_cookie("cookie_from_server","384")# set reponse cookiereturn"Response BODY"Chdir into directory withmain.pyfile and start weepy dev server:$weepy--host127.0.0.1--port8080--appmain:applicationApp should run onlocalhost:8080andURLhttp://localhost:8080/endpointshould now response with "Response BODY"