{"id":8932,"date":"2023-09-13T01:37:35","date_gmt":"2023-09-12T17:37:35","guid":{"rendered":"\/?p=8932"},"modified":"2023-09-13T01:37:35","modified_gmt":"2023-09-12T17:37:35","slug":"fastapi%e5%9f%ba%e7%a1%80%ef%bc%9a2-%e8%b7%af%e7%94%b1%e8%af%b7%e6%b1%82%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"\/?p=8932","title":{"rendered":"FastAPI\u57fa\u7840\uff1a2.\u8def\u7531&#038;\u8bf7\u6c42\u65b9\u6cd5"},"content":{"rendered":"<h1>\u5b89\u88c5\u9700\u8981\u7684\u4f9d\u8d56<\/h1>\n<pre><code class=\"language-shell\">pip install fastapi uvicorn<\/code><\/pre>\n<h1>FastAPI\u4e2d\u4e00\u4e2a\u63a5\u53e3\u670d\u52a1\u7684\u7ec4\u6210<\/h1>\n<p>\u8d44\u6e90\u5730\u5740 + \u89c6\u56fe<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/1670066396722-6ac20b68-1e7c-4b66-89c1-ec6c87dd72b4.png\" alt=\"img\" \/><\/p>\n<h1>\u6ce8\u518c\u8def\u7531\u7684\u51e0\u79cd\u65b9\u5f0f<\/h1>\n<pre><code class=\"language-python\">from fastapi import FastAPI, routing\nfrom uvicorn import run\n\ndef enp():\n    return &quot;123&quot;\n\n    # https:\/\/www.starlette.io\/ \u65b9\u5f0f\n    # \u8bf7\u6c42url\uff1ahttp:\/\/127.0.0.1:8000\/emp\nroutes = [routing.APIRoute(path=&quot;\/emp&quot;, endpoint=enp)]\n\n&quot;&quot;&quot;\nredoc_url = None\u7981\u7528 \/redoc \u6587\u6863\ndocs_url = None \u7981\u7528 \/docs \u6587\u6863\n&quot;&quot;&quot;\napp = FastAPI(title=&quot;\u8bf7\u6c42\u65b9\u6cd5&quot;, description=&quot;fastapi-\u8bf7\u6c42\u65b9\u6cd5&quot;,\n              # docs_url\n              routes=routes)\n\n# https:\/\/www.starlette.io\/ \u65b9\u5f0f\napp.add_api_route(&quot;\/enp&quot;, endpoint=enp, description=&quot;miaosu&quot;, tags=[&quot;\u5206\u7c7b&quot;], summary=&quot;\u540d\u79f0&quot;)\n\n# \u88c5\u9970\u5668\u7684\u65b9\u5f0f\n@app.api_route(&quot;\/index&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;, &quot;DELETE&quot;], name=&quot;\u5965\u5229\u7ed9&quot;)\ndef index():\n    return &quot;index&quot;\n\n@app.get(&quot;\/get&quot;)\nasync def get():\n    return &quot;get&quot;\n\n@app.post(&quot;\/post&quot;)\nasync def post():\n    return &quot;post&quot;\n\n@app.put(&quot;\/put&quot;)\nasync def put():\n    return &quot;put&quot;\n\n@app.delete(&quot;\/delete&quot;)\nasync def delete():\n    return &quot;delete&quot;\n\n    # \u5b50\u8def\u7531\nfrom fastapi import APIRouter\n\nasync def sub_close():\n    print(&quot;sub\u5b50\u8def\u6709\u5173\u95ed\u4e8b\u4ef6&quot;)\n\n# \u90e8\u5206\u53c2\u6570 \u548c FastAPI()\u4e00\u76f4 prefix \u8def\u7531\u524d\u7f00\u3001\u540e\u9762\u7684\u8def\u7531\u5730\u5740\u4e3a \u524d\u7f00 + path\nrouter = APIRouter(prefix=&quot;\/sub&quot;, on_startup=[lambda: print(&quot;sub\u5b50\u8def\u6709\u542f\u52a8\u4e8b\u4ef6&quot;)\n                                             ], on_shutdown=[sub_close])\n\n# url -&gt; host:port\/sub\/hello\n@router.get(path=&quot;\/hello&quot;, summary=&quot;\u5b50\u8def\u6709\u9996\u9875&quot;)\nasync def hell():\n    return &quot;sub\/hello&quot;\n\napp.include_router(router, tags=[&quot;\u5b50\u8def\u6709&quot;])\n\nif __name__ == &#039;__main__&#039;:\n    # \u542f\u7528web\u670d\u52a1\uff0c reload -&gt; \u5f53\u6539\u53d8\u4ee3\u7801\u65f6\u81ea\u52a8\u91cd\u542f # fastapi_02:app\n    run(&quot;__main__:app&quot;, reload=True)<\/code><\/pre>\n<h1>RESTFul\u89c4\u8303<\/h1>\n<p><a href=\"https:\/\/restfulapi.cn\/\">RESTful API \u4e00\u79cd\u6d41\u884c\u7684 API \u8bbe\u8ba1\u98ce\u683c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5\u9700\u8981\u7684\u4f9d\u8d56 pip install fastapi uvicorn FastAPI\u4e2d\u4e00\u4e2a\u63a5\u53e3\u670d\u52a1\u7684\u7ec4\u6210 \u8d44\u6e90\u5730\u5740 + \u89c6\u56fe \u6ce8\u518c\u8def\u7531   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[204],"tags":[116],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8932"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8932"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8932\/revisions"}],"predecessor-version":[{"id":8933,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8932\/revisions\/8933"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8932"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}