{"id":565,"date":"2024-05-07T14:55:15","date_gmt":"2024-05-07T06:55:15","guid":{"rendered":"http:\/\/www.madbull.site\/?p=565"},"modified":"2024-07-18T20:42:54","modified_gmt":"2024-07-18T12:42:54","slug":"fastapi%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=565","title":{"rendered":"Fastapi\u793a\u4f8b"},"content":{"rendered":"\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u5728\u5f53\u524d\u76ee\u5f55\u521b\u5efa\u6587\u4ef6\u5939 test\uff0c\u5e76\u521b\u5efa\u4e09\u4e2a\u6587\u4ef6 fapi.py\u3001test\/t1.py\u3001test\/__init__.py\u3002\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\n\u251c\u2500\u2500 fapi.py\n\u2514\u2500\u2500 test\n    \u251c\u2500\u2500 __init__.py\n    \u2514\u2500\u2500 t1.py<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u4e3b\u6587\u4ef6 fapi.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\nfrom fastapi import FastAPI \nfrom fastapi import APIRouter\nimport uvicorn\n\nfrom test import t1\n\napi_router = APIRouter()\napi_router.include_router(t1.test_router, prefix=\"\/test\", tags=&#91;\"CCCCCCCCCCCCCCCCC\"] )\n\ndef create_app():\n    tags_metadata = &#91;{ \"name\": \"\u6d4b\u8bd5APIII\", \"description\": \"fastapi \u7ec3\u4e60\" }]\n    # \u521b\u5efaapi\u5e94\u7528\n    app = FastAPI( title=\"\u6d4b\u8bd5API\", description=\"learnlearn\", version=\"1.0\", docs_url=\"\/docs\", openapi_url=\"\/api\/api.json\", openapi_tags=tags_metadata)\n    # \u589e\u52a0\u8def\u7531\n    app.include_router( api_router, prefix=\"\/tt\")\n\n    return app\n\n# \u521b\u5efa\u4e00\u4e2a\u5e94\u7528\ntestapp = create_app()\n\nif __name__ == \"__main__\":\n    uvicorn.run(app='fapi:testapp', host=\"0.0.0.0\", port=3456, reload=True)\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u5728test\u76ee\u5f55\u4e0b\u521b\u5efa t1.py \u6587\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n\nfrom fastapi import status\nfrom fastapi import APIRouter\nfrom fastapi.responses import JSONResponse, Response\nfrom pydantic import BaseModel\nfrom fastapi import Body\n\n# \u521b\u5efa\u8def\u7531\ntest_router = APIRouter()\n\n# get\u8bf7\u6c42\u5904\u7406\n@test_router.get(\"\/t1\", summary=u\"\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\")\ndef t1( a: str = \"&lt;not given&gt;\", b: str = \"&lt;not given&gt;\") -&gt; Response :\n    pstr = \"param a is &#91;\"+a+\"]; param b is &#91;\"+b+\"]\"\n    return JSONResponse(\n        status_code=status.HTTP_200_OK,\n        content = {\n            \"code\": \"1001\",\n            \"message\": \"hello , first fast api return\",\n            \"params\" : pstr\n        })\n\nclass Item(BaseModel):\n    aa: str = \"&lt;not given&gt;\"\n    bb: str = \"&lt;not given&gt;\"\n\n# post\u8bf7\u6c42\n@test_router.post(\"\/t1\", summary=u\"\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\")\ndef t1_post(item: Item = Body(...)) -&gt; Response :\n    datastr = \"param a is &#91;\"+item.aa+\"]; param b is &#91;\"+item.bb+\"]\"\n    return JSONResponse(\n        status_code=status.HTTP_200_OK,\n        content = {\n            \"code\": \"1002\",\n            \"message\": \"hello , first fast api return\",\n            \"datastr\": datastr\n        })\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u5728test\u76ee\u5f55\u4e0b\u521b\u5efa\u7a7a\u6587\u4ef6 __init__.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u542f\u52a8api\u63a5\u53e3<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"191\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi1.png\" alt=\"\" class=\"wp-image-567\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi1.png 676w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi1-300x85.png 300w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u63a5\u53e3 \u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee http:\/\/xx.xx.xx.xx:3456\/docs \u67e5\u770b<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"837\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/11.png\" alt=\"\" class=\"wp-image-807\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/11.png 1460w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/11-300x172.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/11-1024x587.png 1024w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/11-768x440.png 768w\" sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u8bf7\u6c42\u63a5\u53e3GET<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"188\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi2.png\" alt=\"\" class=\"wp-image-566\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi2.png 770w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi2-300x73.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi2-768x188.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u8bf7\u6c42\u63a5\u53e3POST<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"189\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi3.png\" alt=\"\" class=\"wp-image-573\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi3.png 1002w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi3-300x57.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/fapi3-768x145.png 768w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Fastapi\u793a\u4f8b\uff0cuvicorn\u542f\u52a8<\/p>\n","protected":false},"author":1,"featured_media":548,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[155,154],"tags":[181,179,182,142,183,180],"class_list":["post-565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-154","tag-apirouter","tag-fastapi","tag-jsonresonse","tag-python","tag-response","tag-uvicorn"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=565"}],"version-history":[{"count":7,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/565\/revisions"}],"predecessor-version":[{"id":820,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/565\/revisions\/820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/548"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}