{"id":1134538,"date":"2025-01-08T21:16:33","date_gmt":"2025-01-08T13:16:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134538.html"},"modified":"2025-01-08T21:16:35","modified_gmt":"2025-01-08T13:16:35","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%81%9a%e4%b8%80%e4%b8%aa%e5%85%85%e5%80%bc%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1134538.html","title":{"rendered":"python\u5982\u4f55\u7528\u505a\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103432\/239d9589-40c1-47ba-a93a-a1f597ed879e.webp\" alt=\"python\u5982\u4f55\u7528\u505a\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf\" \/><\/p>\n<p><p> <strong>Python \u5982\u4f55\u7528\u505a\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf\u662f\u4e00\u4e2a\u5177\u6709\u6311\u6218\u6027\u7684\u4efb\u52a1\uff0c\u5b83\u6d89\u53ca\u5230\u591a\u65b9\u9762\u7684\u6280\u672f\u548c\u77e5\u8bc6\u3002<strong>Python\u3001\u6570\u636e\u5e93\u96c6\u6210\u3001\u5b89\u5168\u6027\u3001\u7528\u6237\u754c\u9762\u3001\u652f\u4ed8\u7f51\u5173\u96c6\u6210<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u7cfb\u7edf\u7684\u51e0\u4e2a\u5173\u952e\u8981\u7d20\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u8981\u7d20\uff0c\u5e76\u63d0\u4f9b\u4e00\u6b65\u6b65\u7684\u6307\u5bfc\uff0c\u5e2e\u52a9\u4f60\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684\u5145\u503c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Python \u7f16\u7a0b\u8bed\u8a00<\/h3>\n<\/p>\n<p><p>Python \u662f\u4e00\u79cd\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u56e0\u5176\u7b80\u6d01\u6613\u7528\u800c\u53d7\u5230\u5e7f\u6cdb\u6b22\u8fce\u3002<strong>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u4e30\u5bcc\u7684\u5e93\u652f\u6301\u3001\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301<\/strong>\u662f\u9009\u62e9 Python \u7684\u51e0\u5927\u539f\u56e0\u3002<\/p>\n<\/p>\n<p><h4>1. \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/h4>\n<\/p>\n<p><p>Python \u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u7c7b\u548c\u5bf9\u8c61\u6765\u8868\u793a\u7528\u6237\u3001\u4ea4\u6613\u548c\u5176\u4ed6\u7cfb\u7edf\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class User:<\/p>\n<p>    def __init__(self, user_id, balance=0):<\/p>\n<p>        self.user_id = user_id<\/p>\n<p>        self.balance = balance<\/p>\n<p>    def recharge(self, amount):<\/p>\n<p>        self.balance += amount<\/p>\n<p>        return self.balance<\/p>\n<p>user = User(user_id=1)<\/p>\n<p>user.recharge(100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4e30\u5bcc\u7684\u5e93\u652f\u6301<\/h4>\n<\/p>\n<p><p>Python \u62e5\u6709\u4e30\u5bcc\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u5feb\u5730\u5b9e\u73b0\u529f\u80fd\u3002\u6bd4\u5982\uff0cDjango \u548c Flask \u662f\u4e24\u4e2a\u6d41\u884c\u7684 web \u6846\u67b6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u642d\u5efa web \u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/recharge&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def recharge():<\/p>\n<p>    user_id = request.json.get(&#39;user_id&#39;)<\/p>\n<p>    amount = request.json.get(&#39;amount&#39;)<\/p>\n<p>    # \u5047\u8bbe\u5df2\u6709\u7528\u6237\u6570\u636e<\/p>\n<p>    user = User(user_id=user_id)<\/p>\n<p>    new_balance = user.recharge(amount)<\/p>\n<p>    return jsonify({&quot;balance&quot;: new_balance})<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5e93\u96c6\u6210<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u662f\u4efb\u4f55\u5145\u503c\u7cfb\u7edf\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u7528\u4e8e\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u3001\u4ea4\u6613\u8bb0\u5f55\u7b49\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u3001\u8bbe\u8ba1\u6570\u636e\u5e93\u7ed3\u6784\u3001\u5b9e\u73b0\u6570\u636e\u5e93\u64cd\u4f5c<\/strong>\u662f\u8fd9\u90e8\u5206\u7684\u91cd\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u7cfb\u7edf\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982 MySQL\u3001PostgreSQL\uff09\u6216 NoSQL \u6570\u636e\u5e93\uff08\u5982 MongoDB\uff09\u3002\u5173\u7cfb\u578b\u6570\u636e\u5e93\u9002\u5408\u9700\u8981\u590d\u6742\u67e5\u8be2\u548c\u4e8b\u52a1\u652f\u6301\u7684\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;recharge_system.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u7528\u6237\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>CREATE TABLE IF NOT EXISTS users (<\/p>\n<p>    user_id INTEGER PRIMARY KEY,<\/p>\n<p>    balance REAL DEFAULT 0<\/p>\n<p>)<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u7528\u6237<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>INSERT INTO users (user_id, balance) VALUES (?, ?)<\/p>\n<p>&#39;&#39;&#39;, (1, 100))<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbe\u8ba1\u6570\u636e\u5e93\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u7ed3\u6784\u8bbe\u8ba1\u5e94\u5f53\u8003\u8651\u5230\u7528\u6237\u4fe1\u606f\u3001\u4ea4\u6613\u8bb0\u5f55\u3001\u652f\u4ed8\u4fe1\u606f\u7b49\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE TABLE users (<\/p>\n<p>    user_id INTEGER PRIMARY KEY,<\/p>\n<p>    balance REAL<\/p>\n<p>);<\/p>\n<p>CREATE TABLE transactions (<\/p>\n<p>    transaction_id INTEGER PRIMARY KEY,<\/p>\n<p>    user_id INTEGER,<\/p>\n<p>    amount REAL,<\/p>\n<p>    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,<\/p>\n<p>    FOREIGN KEY (user_id) REFERENCES users (user_id)<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b89\u5168\u6027<\/h3>\n<\/p>\n<p><p>\u5b89\u5168\u6027\u5728\u4efb\u4f55\u6d89\u53ca\u91d1\u878d\u4ea4\u6613\u7684\u7cfb\u7edf\u4e2d\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<strong>\u6570\u636e\u52a0\u5bc6\u3001\u8eab\u4efd\u9a8c\u8bc1\u3001\u8bbf\u95ee\u63a7\u5236<\/strong>\u662f\u9700\u8981\u91cd\u70b9\u8003\u8651\u7684\u5b89\u5168\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u52a0\u5bc6<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u7684\u654f\u611f\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u5bc6\u6280\u672f\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u52a0\u5bc6\u5e93\uff0c\u5982 <code>cryptography<\/code> \u548c <code>pycryptodome<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cryptography.fernet import Fernet<\/p>\n<h2><strong>\u751f\u6210\u5bc6\u94a5<\/strong><\/h2>\n<p>key = Fernet.generate_key()<\/p>\n<p>cipher_suite = Fernet(key)<\/p>\n<h2><strong>\u52a0\u5bc6\u6570\u636e<\/strong><\/h2>\n<p>cipher_text = cipher_suite.encrypt(b&quot;Sensitive Data&quot;)<\/p>\n<h2><strong>\u89e3\u5bc6\u6570\u636e<\/strong><\/h2>\n<p>plain_text = cipher_suite.decrypt(cipher_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8eab\u4efd\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u8eab\u4efd\u9a8c\u8bc1\u53ef\u4ee5\u786e\u4fdd\u53ea\u6709\u6388\u6743\u7528\u6237\u624d\u80fd\u8bbf\u95ee\u7cfb\u7edf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 JWT\uff08JSON Web Token\uff09\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jwt<\/p>\n<p>import datetime<\/p>\n<p>secret_key = &quot;your_secret_key&quot;<\/p>\n<p>def generate_token(user_id):<\/p>\n<p>    payload = {<\/p>\n<p>        &quot;user_id&quot;: user_id,<\/p>\n<p>        &quot;exp&quot;: datetime.datetime.utcnow() + datetime.timedelta(hours=1)<\/p>\n<p>    }<\/p>\n<p>    token = jwt.encode(payload, secret_key, algorithm=&quot;HS256&quot;)<\/p>\n<p>    return token<\/p>\n<p>def verify_token(token):<\/p>\n<p>    try:<\/p>\n<p>        payload = jwt.decode(token, secret_key, algorithms=[&quot;HS256&quot;])<\/p>\n<p>        return payload[&quot;user_id&quot;]<\/p>\n<p>    except jwt.ExpiredSignatureError:<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7528\u6237\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u754c\u9762\u662f\u7528\u6237\u4e0e\u7cfb\u7edf\u4ea4\u4e92\u7684\u6865\u6881\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u524d\u7aef\u6846\u67b6\u3001\u8bbe\u8ba1\u53cb\u597d\u7684\u7528\u6237\u754c\u9762\u3001\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92<\/strong>\u662f\u8fd9\u4e00\u90e8\u5206\u7684\u91cd\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u9009\u62e9\u5408\u9002\u7684\u524d\u7aef\u6846\u67b6<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u9009\u62e9\u6d41\u884c\u7684\u524d\u7aef\u6846\u67b6\uff0c\u5982 React\u3001Vue \u6216 Angular \u6765\u6784\u5efa\u7528\u6237\u754c\u9762\u3002\u8fd9\u4e9b\u6846\u67b6\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u642d\u5efa\u590d\u6742\u7684\u754c\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">import React, { useState } from &#39;react&#39;;<\/p>\n<p>import axios from &#39;axios&#39;;<\/p>\n<p>function App() {<\/p>\n<p>    const [userId, setUserId] = useState(&#39;&#39;);<\/p>\n<p>    const [amount, setAmount] = useState(&#39;&#39;);<\/p>\n<p>    const [balance, setBalance] = useState(null);<\/p>\n<p>    const recharge = async () =&gt; {<\/p>\n<p>        const response = await axios.post(&#39;\/recharge&#39;, { user_id: userId, amount });<\/p>\n<p>        setBalance(response.data.balance);<\/p>\n<p>    };<\/p>\n<p>    return (<\/p>\n<p>        &lt;div&gt;<\/p>\n<p>            &lt;input type=&quot;text&quot; placeholder=&quot;User ID&quot; value={userId} onChange={(e) =&gt; setUserId(e.target.value)} \/&gt;<\/p>\n<p>            &lt;input type=&quot;text&quot; placeholder=&quot;Amount&quot; value={amount} onChange={(e) =&gt; setAmount(e.target.value)} \/&gt;<\/p>\n<p>            &lt;button onClick={recharge}&gt;Recharge&lt;\/button&gt;<\/p>\n<p>            {balance !== null &amp;&amp; &lt;p&gt;New Balance: {balance}&lt;\/p&gt;}<\/p>\n<p>        &lt;\/div&gt;<\/p>\n<p>    );<\/p>\n<p>}<\/p>\n<p>export default App;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbe\u8ba1\u53cb\u597d\u7684\u7528\u6237\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u7528\u6237\u754c\u9762\u7684\u8bbe\u8ba1\u5e94\u5f53\u7b80\u6d01\u660e\u4e86\uff0c\u6613\u4e8e\u64cd\u4f5c\u3002\u8003\u8651\u5230\u7528\u6237\u4f53\u9a8c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b UI \u5e93\uff0c\u5982 Material-UI \u6216 Ant Design\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">import React, { useState } from &#39;react&#39;;<\/p>\n<p>import { Button, Input, message } from &#39;antd&#39;;<\/p>\n<p>import axios from &#39;axios&#39;;<\/p>\n<p>function App() {<\/p>\n<p>    const [userId, setUserId] = useState(&#39;&#39;);<\/p>\n<p>    const [amount, setAmount] = useState(&#39;&#39;);<\/p>\n<p>    const [balance, setBalance] = useState(null);<\/p>\n<p>    const recharge = async () =&gt; {<\/p>\n<p>        try {<\/p>\n<p>            const response = await axios.post(&#39;\/recharge&#39;, { user_id: userId, amount });<\/p>\n<p>            setBalance(response.data.balance);<\/p>\n<p>            message.success(&#39;Recharge successful!&#39;);<\/p>\n<p>        } catch (error) {<\/p>\n<p>            message.error(&#39;Recharge failed!&#39;);<\/p>\n<p>        }<\/p>\n<p>    };<\/p>\n<p>    return (<\/p>\n<p>        &lt;div style={{ padding: 20 }}&gt;<\/p>\n<p>            &lt;Input placeholder=&quot;User ID&quot; value={userId} onChange={(e) =&gt; setUserId(e.target.value)} style={{ marginBottom: 10 }} \/&gt;<\/p>\n<p>            &lt;Input placeholder=&quot;Amount&quot; value={amount} onChange={(e) =&gt; setAmount(e.target.value)} style={{ marginBottom: 10 }} \/&gt;<\/p>\n<p>            &lt;Button type=&quot;primary&quot; onClick={recharge}&gt;Recharge&lt;\/Button&gt;<\/p>\n<p>            {balance !== null &amp;&amp; &lt;p&gt;New Balance: {balance}&lt;\/p&gt;}<\/p>\n<p>        &lt;\/div&gt;<\/p>\n<p>    );<\/p>\n<p>}<\/p>\n<p>export default App;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u652f\u4ed8\u7f51\u5173\u96c6\u6210<\/h3>\n<\/p>\n<p><p>\u652f\u4ed8\u7f51\u5173\u662f\u5b9e\u73b0\u5728\u7ebf\u652f\u4ed8\u7684\u5173\u952e\u7ec4\u4ef6\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u652f\u4ed8\u7f51\u5173\u3001\u96c6\u6210\u652f\u4ed8 API\u3001\u5904\u7406\u652f\u4ed8\u56de\u8c03<\/strong>\u662f\u8fd9\u90e8\u5206\u7684\u91cd\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u9009\u62e9\u5408\u9002\u7684\u652f\u4ed8\u7f51\u5173<\/h4>\n<\/p>\n<p><p>\u5e02\u9762\u4e0a\u6709\u5f88\u591a\u652f\u4ed8\u7f51\u5173\u53ef\u4f9b\u9009\u62e9\uff0c\u5982 PayPal\u3001Stripe\u3001Alipay\u3001WeChat Pay \u7b49\u3002\u4f60\u9700\u8981\u6839\u636e\u76ee\u6807\u5e02\u573a\u548c\u7528\u6237\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u652f\u4ed8\u7f51\u5173\u3002<\/p>\n<\/p>\n<p><h4>2. \u96c6\u6210\u652f\u4ed8 API<\/h4>\n<\/p>\n<p><p>\u4ee5 Stripe \u4e3a\u4f8b\uff0c\u9996\u5148\u4f60\u9700\u8981\u5b89\u88c5 Stripe \u7684 Python SDK\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install stripe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u540e\u53f0\u670d\u52a1\u5668\u4e2d\u96c6\u6210 Stripe \u7684\u652f\u4ed8\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import stripe<\/p>\n<p>stripe.api_key = &quot;your_stripe_secret_key&quot;<\/p>\n<p>def create_payment_intent(amount, currency=&quot;usd&quot;):<\/p>\n<p>    payment_intent = stripe.PaymentIntent.create(<\/p>\n<p>        amount=amount,<\/p>\n<p>        currency=currency,<\/p>\n<p>    )<\/p>\n<p>    return payment_intent.client_secret<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5904\u7406\u652f\u4ed8\u56de\u8c03<\/h4>\n<\/p>\n<p><p>\u652f\u4ed8\u6210\u529f\u540e\uff0c\u652f\u4ed8\u7f51\u5173\u4f1a\u53d1\u9001\u56de\u8c03\u8bf7\u6c42\u5230\u4f60\u7684\u670d\u52a1\u5668\u3002\u4f60\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u56de\u8c03\u8bf7\u6c42\uff0c\u66f4\u65b0\u7528\u6237\u7684\u8d26\u6237\u4f59\u989d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/webhook&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def webhook():<\/p>\n<p>    payload = request.get_data(as_text=True)<\/p>\n<p>    sig_header = request.headers.get(&#39;Stripe-Signature&#39;)<\/p>\n<p>    event = None<\/p>\n<p>    try:<\/p>\n<p>        event = stripe.Webhook.construct_event(<\/p>\n<p>            payload, sig_header, &quot;your_webhook_secret&quot;<\/p>\n<p>        )<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        # Invalid payload<\/p>\n<p>        abort(400)<\/p>\n<p>    except stripe.error.SignatureVerificationError as e:<\/p>\n<p>        # Invalid signature<\/p>\n<p>        abort(400)<\/p>\n<p>    if event[&#39;type&#39;] == &#39;payment_intent.succeeded&#39;:<\/p>\n<p>        payment_intent = event[&#39;data&#39;][&#39;object&#39;]<\/p>\n<p>        user_id = payment_intent[&#39;metadata&#39;][&#39;user_id&#39;]<\/p>\n<p>        amount = payment_intent[&#39;amount&#39;]<\/p>\n<p>        # \u66f4\u65b0\u7528\u6237\u4f59\u989d<\/p>\n<p>        # ...<\/p>\n<p>    return jsonify(success=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6d4b\u8bd5\u4e0e\u90e8\u7f72<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u5f00\u53d1\u540e\uff0c\u4f60\u9700\u8981\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\uff0c\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002<strong>\u529f\u80fd\u6d4b\u8bd5\u3001\u6027\u80fd\u6d4b\u8bd5\u3001\u5b89\u5168\u6d4b\u8bd5<\/strong>\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u529f\u80fd\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u529f\u80fd\u6d4b\u8bd5\u786e\u4fdd\u7cfb\u7edf\u7684\u5404\u9879\u529f\u80fd\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 Python \u7684 unittest \u6a21\u5757\u6216 pytest \u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>from your_module import User<\/p>\n<p>class TestRechargeSystem(unittest.TestCase):<\/p>\n<p>    def test_recharge(self):<\/p>\n<p>        user = User(user_id=1, balance=0)<\/p>\n<p>        self.assertEqual(user.recharge(100), 100)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6027\u80fd\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u6027\u80fd\u6d4b\u8bd5\u786e\u4fdd\u7cfb\u7edf\u5728\u9ad8\u8d1f\u8f7d\u4e0b\u80fd\u591f\u4fdd\u6301\u826f\u597d\u7684\u54cd\u5e94\u901f\u5ea6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 JMeter \u6216 Locust \u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b89\u5168\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5b89\u5168\u6d4b\u8bd5\u786e\u4fdd\u7cfb\u7edf\u80fd\u591f\u62b5\u5fa1\u5404\u79cd\u653b\u51fb\uff0c\u5982 SQL \u6ce8\u5165\u3001XSS \u653b\u51fb\u7b49\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 OWASP ZAP \u6216 Burp Suite \u8fdb\u884c\u5b89\u5168\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf\u662f\u4e00\u4e2a\u590d\u6742\u800c\u591a\u65b9\u9762\u7684\u4efb\u52a1\uff0c\u5b83\u4e0d\u4ec5\u9700\u8981\u624e\u5b9e\u7684\u7f16\u7a0b\u57fa\u7840\uff0c\u8fd8\u9700\u8981\u5bf9\u6570\u636e\u5e93\u3001\u5b89\u5168\u3001\u7528\u6237\u754c\u9762\u3001\u652f\u4ed8\u7f51\u5173\u7b49\u65b9\u9762\u7684\u6df1\u5165\u4e86\u89e3\u3002<strong>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u6808\u3001\u8bbe\u8ba1\u5408\u7406\u7684\u7cfb\u7edf\u67b6\u6784\u3001\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5<\/strong>\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u9ad8\u6548\u3001\u5b89\u5168\u3001\u7528\u6237\u53cb\u597d\u7684\u5145\u503c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u8bbe\u8ba1\u4e00\u4e2a\u57fa\u672c\u7684\u5145\u503c\u7cfb\u7edf\u67b6\u6784\uff1f<\/strong><br \/>\u5728\u8bbe\u8ba1\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf\u65f6\uff0c\u9700\u8981\u8003\u8651\u7cfb\u7edf\u7684\u6574\u4f53\u67b6\u6784\u3002\u901a\u5e38\uff0c\u8fd9\u5305\u62ec\u524d\u7aef\u7528\u6237\u754c\u9762\u548c\u540e\u7aef\u670d\u52a1\u5668\u5904\u7406\u3002\u524d\u7aef\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49Python\u6846\u67b6\uff0c\u540e\u7aef\u5219\u8d1f\u8d23\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u3001\u5145\u503c\u91d1\u989d\u5904\u7406\u548c\u652f\u4ed8\u63a5\u53e3\u7684\u96c6\u6210\u3002\u540c\u65f6\uff0c\u6570\u636e\u5e93\u7684\u9009\u62e9\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u63a8\u8350\u4f7f\u7528SQLite\u6216PostgreSQL\u6765\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u548c\u4ea4\u6613\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u5145\u503c\u7cfb\u7edf\u4e2d\u5982\u4f55\u786e\u4fdd\u7528\u6237\u6570\u636e\u7684\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u6570\u636e\u5b89\u5168\u5728\u4efb\u4f55\u5145\u503c\u7cfb\u7edf\u4e2d\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u52a0\u5bc6\u7528\u6237\u654f\u611f\u4fe1\u606f\uff08\u5982\u5bc6\u7801\u548c\u652f\u4ed8\u4fe1\u606f\uff09\u6765\u589e\u5f3a\u5b89\u5168\u6027\u3002\u4f7f\u7528HTTPS\u534f\u8bae\u6765\u4fdd\u62a4\u6570\u636e\u4f20\u8f93\uff0c\u4ee5\u53ca\u5b9e\u65bd\u6743\u9650\u7ba1\u7406\uff0c\u786e\u4fdd\u53ea\u6709\u7ecf\u8fc7\u6388\u6743\u7684\u7528\u6237\u624d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u529f\u80fd\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u8fdb\u884c\u5b89\u5168\u5ba1\u8ba1\u548c\u6f0f\u6d1e\u626b\u63cf\u4e5f\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u652f\u4ed8\u65b9\u5f0f\u53ef\u4ee5\u96c6\u6210\u5230\u5145\u503c\u7cfb\u7edf\u4e2d\uff1f<\/strong><br \/>\u5728\u5145\u503c\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u96c6\u6210\u591a\u79cd\u652f\u4ed8\u65b9\u5f0f\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7528\u6237\u7684\u9700\u6c42\u3002\u5e38\u89c1\u7684\u652f\u4ed8\u65b9\u5f0f\u5305\u62ec\u4fe1\u7528\u5361\u3001\u501f\u8bb0\u5361\u3001\u652f\u4ed8\u5b9d\u3001\u5fae\u4fe1\u652f\u4ed8\u548cPayPal\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u652f\u4ed8\u7f51\u5173\uff0c\u786e\u4fdd\u5176\u652f\u6301\u5feb\u901f\u4e14\u5b89\u5168\u7684\u4ea4\u6613\u5904\u7406\u3002\u540c\u65f6\uff0c\u8003\u8651\u7528\u6237\u4f53\u9a8c\uff0c\u63d0\u4f9b\u7b80\u6d01\u7684\u652f\u4ed8\u6d41\u7a0b\u548c\u591a\u79cd\u652f\u4ed8\u9009\u9879\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u7684\u5145\u503c\u610f\u613f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5982\u4f55\u7528\u505a\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf \u521b\u5efa\u4e00\u4e2a\u5145\u503c\u7cfb\u7edf\u662f\u4e00\u4e2a\u5177\u6709\u6311\u6218\u6027\u7684\u4efb\u52a1\uff0c\u5b83\u6d89\u53ca\u5230\u591a\u65b9\u9762\u7684\u6280\u672f\u548c\u77e5\u8bc6\u3002Pyt [&hellip;]","protected":false},"author":3,"featured_media":1134547,"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\/1134538"}],"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=1134538"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134538\/revisions"}],"predecessor-version":[{"id":1134550,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134538\/revisions\/1134550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}