{"id":927361,"date":"2024-12-26T16:07:32","date_gmt":"2024-12-26T08:07:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/927361.html"},"modified":"2024-12-26T16:07:35","modified_gmt":"2024-12-26T08:07:35","slug":"python%e5%a6%82%e4%bd%95api","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/927361.html","title":{"rendered":"python\u5982\u4f55api"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063448\/d97189c5-164f-4405-9783-03b84c34005a.webp\" alt=\"python\u5982\u4f55api\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e93\u5982Flask\u3001Django\u3001FastAPI\u7b49\u6765\u521b\u5efaAPI\u3001\u7f16\u5199API\u7684\u8fc7\u7a0b\u901a\u5e38\u6d89\u53ca\u5b9a\u4e49\u8def\u7531\u3001\u5904\u7406\u8bf7\u6c42\u548c\u8fd4\u56de\u54cd\u5e94\u3002Flask\u662f\u4e00\u4e2a\u5fae\u6846\u67b6\uff0c\u9002\u5408\u5c0f\u578b\u5e94\u7528\uff1bDjango\u66f4\u9002\u5408\u5927\u578b\u5e94\u7528\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff1bFastAPI\u4ee5\u5176\u9ad8\u6027\u80fd\u548c\u81ea\u52a8\u751f\u6210\u6587\u6863\u800c\u53d7\u5230\u6b22\u8fce\u3002<\/strong> \u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Flask\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684API\u3002<\/p>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python\u5fae\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u6784\u5efa\u5c0f\u578b\u5e94\u7528\u548cAPI\u3002\u5b83\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684API\uff0c\u4f60\u9700\u8981\u5b89\u88c5Flask\u5e93\uff0c\u5b9a\u4e49\u4f60\u7684API\u7aef\u70b9\uff0c\u5e76\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002Flask\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u4f7f\u5176\u6210\u4e3a\u521b\u5efaAPI\u7684\u4e00\u4e2a\u6781\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u8bbe\u7f6eFlask<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199API\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Flask\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u4f8b\u5982<code>app.py<\/code>\uff0c\u7528\u4e8e\u7f16\u5199\u4f60\u7684Flask\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask API<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684API\u6765\u6f14\u793aFlask\u7684\u57fa\u672c\u7528\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Hello World\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/hello&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def hello_world():<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;Hello, World!&#39;})<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86Flask\u7c7b\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2aFlask\u5e94\u7528\u5b9e\u4f8b\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531<code>\/hello<\/code>\uff0c\u5f53\u5ba2\u6237\u7aef\u53d1\u9001GET\u8bf7\u6c42\u5230\u8fd9\u4e2a\u7aef\u70b9\u65f6\uff0c\u670d\u52a1\u5668\u5c06\u8fd4\u56de\u4e00\u4e2aJSON\u54cd\u5e94<code>{&#39;message&#39;: &#39;Hello, World!&#39;}<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8fd0\u884c\u548c\u6d4b\u8bd5\u4f60\u7684API<\/p>\n<\/p>\n<p><p>\u8981\u8fd0\u884c\u4f60\u7684API\uff0c\u6253\u5f00\u7ec8\u7aef\u5e76\u5bfc\u822a\u5230\u5305\u542b<code>app.py<\/code>\u7684\u76ee\u5f55\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Flask\u5e94\u7528\u5c06\u5728\u672c\u5730\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\uff0c\u9ed8\u8ba4\u7aef\u53e3\u662f5000\u3002\u6253\u5f00\u6d4f\u89c8\u5668\u6216\u4f7f\u7528\u5de5\u5177\u5982Postman\uff0c\u8bbf\u95ee<a href=\"http:\/\/127.0.0.1:5000\/hello%EF%BC%8C%E4%BD%A0%E5%B0%86%E7%9C%8B%E5%88%B0%E8%BF%94%E5%9B%9E%E7%9A%84JSON%E5%93%8D%E5%BA%94%E3%80%82\">http:\/\/127.0.0.1:5000\/hello\uff0c\u4f60\u5c06\u770b\u5230\u8fd4\u56de\u7684JSON\u54cd\u5e94\u3002<\/a><\/p>\n<\/p>\n<p><p>\u56db\u3001\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\u5230\u4f60\u7684API<\/p>\n<\/p>\n<p><p>Flask\u4e0d\u4ec5\u4ec5\u53ef\u4ee5\u5904\u7406\u7b80\u5355\u7684GET\u8bf7\u6c42\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406POST\u3001PUT\u3001DELETE\u7b49HTTP\u8bf7\u6c42\u3002\u4e0b\u9762\u6211\u4eec\u6765\u6269\u5c55\u6211\u4eec\u7684API\uff0c\u589e\u52a0\u66f4\u591a\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406POST\u8bf7\u6c42<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7aef\u70b9\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u63d0\u4ea4\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import request<\/p>\n<p>@app.route(&#39;\/data&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def receive_data():<\/p>\n<p>    data = request.get_json()<\/p>\n<p>    return jsonify({&#39;received_data&#39;: data})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>request<\/code>\u5bf9\u8c61\uff0c\u7528\u4e8e\u8bbf\u95ee\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u3002\u5ba2\u6237\u7aef\u53ef\u4ee5\u53d1\u9001JSON\u6570\u636e\u5230<code>\/data<\/code>\u7aef\u70b9\uff0c\u6211\u4eec\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5df2\u63a5\u6536\u6570\u636e\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5904\u7406PUT\u8bf7\u6c42<\/strong><\/li>\n<\/ol>\n<p><p>PUT\u8bf7\u6c42\u901a\u5e38\u7528\u4e8e\u66f4\u65b0\u8d44\u6e90\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7aef\u70b9\u6765\u66f4\u65b0\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/update&#39;, methods=[&#39;PUT&#39;])<\/p>\n<p>def update_data():<\/p>\n<p>    new_data = request.get_json()<\/p>\n<p>    return jsonify({&#39;updated_data&#39;: new_data})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5ba2\u6237\u7aef\u53d1\u9001PUT\u8bf7\u6c42\u5230<code>\/update<\/code>\u7aef\u70b9\uff0c\u5e76\u9644\u5e26\u6570\u636e\uff0c\u6211\u4eec\u5c06\u8fd4\u56de\u66f4\u65b0\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5904\u7406DELETE\u8bf7\u6c42<\/strong><\/li>\n<\/ol>\n<p><p>DELETE\u8bf7\u6c42\u7528\u4e8e\u5220\u9664\u8d44\u6e90\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7aef\u70b9\u6765\u5904\u7406\u5220\u9664\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/delete\/&lt;int:id&gt;&#39;, methods=[&#39;DELETE&#39;])<\/p>\n<p>def delete_data(id):<\/p>\n<p>    return jsonify({&#39;message&#39;: f&#39;Data with id {id} has been deleted&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5ba2\u6237\u7aef\u53d1\u9001DELETE\u8bf7\u6c42\u5230<code>\/delete\/&lt;id&gt;<\/code>\u7aef\u70b9\uff0c\u5176\u4e2d<code>&lt;id&gt;<\/code>\u662f\u8981\u5220\u9664\u7684\u6570\u636e\u7684\u6807\u8bc6\u7b26\u3002\u6211\u4eec\u5c06\u8fd4\u56de\u4e00\u4e2a\u786e\u8ba4\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Flask\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u548c\u9519\u8bef\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u6784\u5efaAPI\u65f6\uff0c\u9a8c\u8bc1\u5ba2\u6237\u7aef\u8f93\u5165\u7684\u6570\u636e\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u975e\u5e38\u91cd\u8981\u3002Flask\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u9a8c\u8bc1<\/strong><\/li>\n<\/ol>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Flask\u7684\u6269\u5c55\u5e93\uff0c\u5982<code>Flask-WTF<\/code>\u6216<code>Marshmallow<\/code>\uff0c\u6765\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u4f7f\u7528Flask\u5185\u7f6e\u7684\u9a8c\u8bc1\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/validate&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def validate_data():<\/p>\n<p>    data = request.get_json()<\/p>\n<p>    if &#39;name&#39; not in data:<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;Name is required&#39;}), 400<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;Data is valid&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u8bf7\u6c42\u6570\u636e\u4e2d\u662f\u5426\u5305\u542b<code>name<\/code>\u5b57\u6bb5\u3002\u5982\u679c\u6ca1\u6709\uff0c\u6211\u4eec\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>Flask\u5141\u8bb8\u4f60\u5b9a\u4e49\u5168\u5c40\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u6355\u83b7\u5e76\u5904\u7406\u5e94\u7528\u4e2d\u7684\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.errorhandler(404)<\/p>\n<p>def page_not_found(e):<\/p>\n<p>    return jsonify({&#39;error&#39;: &#39;Page not found&#39;}), 404<\/p>\n<p>@app.errorhandler(500)<\/p>\n<p>def internal_server_error(e):<\/p>\n<p>    return jsonify({&#39;error&#39;: &#39;Internal server error&#39;}), 500<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\uff0c\u5206\u522b\u5904\u7406404\u548c500\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528Flask\u7684\u9ad8\u7ea7\u529f\u80fd<\/p>\n<\/p>\n<p><p>Flask\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u521b\u5efa\u66f4\u590d\u6742\u548c\u5f3a\u5927\u7684API\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Flask Blueprints<\/strong><\/li>\n<\/ol>\n<p><p>Blueprints\u5141\u8bb8\u4f60\u5c06\u5e94\u7528\u7684\u4e0d\u540c\u90e8\u5206\u7ec4\u7ec7\u5230\u4e0d\u540c\u7684\u6a21\u5757\u4e2d\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Blueprint<\/p>\n<p>mod = Blueprint(&#39;mod&#39;, __name__)<\/p>\n<p>@mod.route(&#39;\/mod&#39;)<\/p>\n<p>def mod_index():<\/p>\n<p>    return &quot;This is a module&quot;<\/p>\n<p>app.register_blueprint(mod)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528Flask-SQLAlchemy\u8fdb\u884c\u6570\u636e\u5e93\u96c6\u6210<\/strong><\/li>\n<\/ol>\n<p><p>Flask-SQLAlchemy\u662fFlask\u7684\u4e00\u4e2a\u6269\u5c55\u5e93\uff0c\u4f7f\u5f97\u4e0e\u6570\u636e\u5e93\u7684\u96c6\u6210\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask-SQLAlchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Flask-SQLAlchemy\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask_sqlalchemy import SQLAlchemy<\/p>\n<p>app.config[&#39;SQLALCHEMY_DATABASE_URI&#39;] = &#39;sqlite:\/\/\/mydatabase.db&#39;<\/p>\n<p>db = SQLAlchemy(app)<\/p>\n<p>class User(db.Model):<\/p>\n<p>    id = db.Column(db.Integer, primary_key=True)<\/p>\n<p>    username = db.Column(db.String(80), unique=True, nullable=False)<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&#39;&lt;User {self.username}&gt;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528Flask-Migrate\u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb<\/strong><\/li>\n<\/ol>\n<p><p>Flask-Migrate\u662fFlask\u7684\u53e6\u4e00\u4e2a\u6269\u5c55\u5e93\uff0c\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u5e93\u8fc1\u79fb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask-Migrate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Flask-Migrate\u7ba1\u7406\u6570\u636e\u5e93\u8fc1\u79fb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask_migrate import Migrate<\/p>\n<p>migrate = Migrate(app, db)<\/p>\n<h2><strong>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u8fc1\u79fb\u547d\u4ee4<\/strong><\/h2>\n<h2><strong>flask db init<\/strong><\/h2>\n<h2><strong>flask db migrate -m &quot;Initial migration.&quot;<\/strong><\/h2>\n<h2><strong>flask db upgrade<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528FastAPI\u63d0\u9ad8\u6027\u80fd<\/p>\n<\/p>\n<p><p>FastAPI\u662f\u4e00\u4e2a\u73b0\u4ee3\u7684\u3001\u5feb\u901f\u7684\uff08\u9ad8\u6027\u80fd\uff09Web\u6846\u67b6\uff0c\u7528\u4e8e\u6784\u5efaAPI\u3002\u5b83\u57fa\u4e8ePython 3.6+\uff0c\u4f7f\u7528Pydantic\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\uff0c\u4f7f\u7528Starlette\u8fdb\u884c\u5f02\u6b65\u8bf7\u6c42\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5FastAPI\u548cUvicorn<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install fastapi uvicorn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684FastAPI\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from fastapi import FastAPI<\/p>\n<p>app = FastAPI()<\/p>\n<p>@app.get(&quot;\/hello&quot;)<\/p>\n<p>async def read_root():<\/p>\n<p>    return {&quot;message&quot;: &quot;Hello, World!&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8fd0\u884cFastAPI\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">uvicorn main:app --reload<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528Django REST Framework\u6784\u5efa\u5927\u578bAPI<\/p>\n<\/p>\n<p><p>Django REST Framework\uff08DRF\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u96c6\uff0c\u7528\u4e8e\u6784\u5efaWeb API\u3002\u5b83\u4e0eDjango\u96c6\u6210\u826f\u597d\uff0c\u9002\u5408\u6784\u5efa\u5927\u578b\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Django\u548cDjango REST Framework<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install django djangorestframework<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efaDjango\u9879\u76ee\u548c\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p>django-admin startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8bbe\u7f6eDjango REST Framework<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u9879\u76ee\u7684<code>settings.py<\/code>\u4e2d\u6dfb\u52a0DRF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">INSTALLED_APPS = [<\/p>\n<p>    ...<\/p>\n<p>    &#39;rest_framework&#39;,<\/p>\n<p>    &#39;myapp&#39;,<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u521b\u5efaAPI\u89c6\u56fe<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5e94\u7528\u7684<code>views.py<\/code>\u4e2d\u521b\u5efa\u4e00\u4e2aAPI\u89c6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from rest_framework.views import APIView<\/p>\n<p>from rest_framework.response import Response<\/p>\n<p>from rest_framework import status<\/p>\n<p>class HelloWorld(APIView):<\/p>\n<p>    def get(self, request):<\/p>\n<p>        return Response({&quot;message&quot;: &quot;Hello, World!&quot;}, status=status.HTTP_200_OK)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u8bbe\u7f6eURL\u8def\u7531<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5e94\u7528\u7684<code>urls.py<\/code>\u4e2d\u8bbe\u7f6e\u8def\u7531\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.urls import path<\/p>\n<p>from .views import HelloWorld<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;hello\/&#39;, HelloWorld.as_view(), name=&#39;hello_world&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"6\">\n<li><strong>\u8fd0\u884cDjango\u9879\u76ee<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001API\u7684\u5b89\u5168\u6027\u8003\u8651<\/p>\n<\/p>\n<p><p>\u5728\u6784\u5efaAPI\u65f6\uff0c\u5b89\u5168\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5b89\u5168\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528JWT\uff08JSON Web Tokens\uff09\u6216OAuth\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u52a0\u5bc6<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528HTTPS\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong><\/li>\n<\/ol>\n<p><p>\u9a8c\u8bc1\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u9632\u6b62SQL\u6ce8\u5165\u548c\u5176\u4ed6\u653b\u51fb\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u901f\u7387\u9650\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u9650\u5236\u5355\u4e2a\u7528\u6237\u5728\u7ed9\u5b9a\u65f6\u95f4\u5185\u53ef\u4ee5\u53d1\u51fa\u7684\u8bf7\u6c42\u6570\u91cf\uff0c\u9632\u6b62DDoS\u653b\u51fb\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u6784\u5efaAPI\u662f\u4e00\u9879\u5f3a\u5927\u7684\u6280\u80fd\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u89c4\u6a21\u7684\u9879\u76ee\u3002\u65e0\u8bba\u662f\u4f7f\u7528Flask\u3001Django\u8fd8\u662fFastAPI\uff0c\u6bcf\u4e2a\u6846\u67b6\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u9009\u62e9\u6846\u67b6\u65f6\uff0c\u5e94\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u548c\u89c4\u6a21\u505a\u51fa\u51b3\u7b56\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u6846\u67b6\u7684\u57fa\u672c\u7528\u6cd5\u548c\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u9ad8\u6548\u3001\u5b89\u5168\u548c\u53ef\u6269\u5c55\u7684API\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2aAPI\uff1f<\/strong><br \/>\u4f7f\u7528Python\u521b\u5efaAPI\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u5229\u7528Flask\u6216Django\u7b49\u6846\u67b6\u3002Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u5feb\u901f\u6784\u5efa\u5c0f\u578bAPI\u3002\u60a8\u9700\u8981\u5b89\u88c5Flask\u5e93\uff0c\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\uff0c\u7136\u540e\u8fd0\u884c\u5e94\u7528\u3002Django\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684REST\u6846\u67b6\uff08Django REST Framework\uff09\uff0c\u9002\u5408\u6784\u5efa\u66f4\u590d\u6742\u7684API\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u662f\u5173\u952e\u3002<\/p>\n<p><strong>Python API\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u5f00\u53d1Python API\u65f6\uff0c\u9075\u5faaRESTful\u539f\u5219\u3001\u4f7f\u7528\u5408\u9002\u7684HTTP\u72b6\u6001\u7801\u3001\u63d0\u4f9b\u8be6\u7ec6\u7684\u6587\u6863\u548c\u9519\u8bef\u5904\u7406\u673a\u5236\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6b64\u5916\uff0c\u786e\u4fddAPI\u7684\u7248\u672c\u63a7\u5236\u548c\u5b89\u5168\u6027\uff08\u5982\u4f7f\u7528OAuth\u6216API\u5bc6\u94a5\uff09\u4e5f\u80fd\u63d0\u9ad8API\u7684\u53ef\u7528\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5982\u4f55\u6d4b\u8bd5Python API\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Postman\u7b49\u5de5\u5177\u6765\u6d4b\u8bd5Python API\u7684\u529f\u80fd\uff0c\u5e2e\u52a9\u60a8\u53d1\u9001\u4e0d\u540c\u7684\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u3002\u6b64\u5916\uff0cPython\u7684unittest\u6216pytest\u7b49\u6d4b\u8bd5\u6846\u67b6\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7f16\u5199\u81ea\u52a8\u5316\u6d4b\u8bd5\uff0c\u786e\u4fddAPI\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u59cb\u7ec8\u4fdd\u6301\u7a33\u5b9a\u548c\u9ad8\u6548\u3002\u8fd9\u4e9b\u6d4b\u8bd5\u53ef\u4ee5\u6db5\u76d6\u6b63\u5e38\u60c5\u51b5\u3001\u8fb9\u754c\u60c5\u51b5\u548c\u9519\u8bef\u5904\u7406\u7b49\u591a\u79cd\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e93\u5982Flask\u3001Django\u3001FastAPI\u7b49\u6765\u521b\u5efaAPI\u3001\u7f16\u5199API\u7684\u8fc7\u7a0b\u901a\u5e38\u6d89\u53ca\u5b9a [&hellip;]","protected":false},"author":3,"featured_media":927364,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927361"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=927361"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927361\/revisions"}],"predecessor-version":[{"id":927366,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927361\/revisions\/927366"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/927364"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=927361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=927361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=927361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}