{"id":1124332,"date":"2025-01-08T19:40:26","date_gmt":"2025-01-08T11:40:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124332.html"},"modified":"2025-01-08T19:40:32","modified_gmt":"2025-01-08T11:40:32","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%99%bb%e9%99%86%e9%87%8d%e5%a4%8d3%e6%ac%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124332.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u767b\u9646\u91cd\u590d3\u6b21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085434\/f1cec7ad-7785-43a4-9ba8-195882ad91c1.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u767b\u9646\u91cd\u590d3\u6b21\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u767b\u5f55\u7cfb\u7edf\u5e76\u9650\u5236\u5c1d\u8bd5\u6b21\u6570\u4e3a3\u6b21\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5faa\u73af\u63a7\u5236\u767b\u5f55\u5c1d\u8bd5\u6b21\u6570\u3001\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u767b\u5f55\u903b\u8f91\u3001\u96c6\u6210\u9519\u8bef\u5904\u7406\u673a\u5236\u7b49\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u9009\u62e9\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u548c\u4e00\u4e2a\u8ba1\u6570\u5668\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7cfb\u7edf\u8bbe\u8ba1\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5316\u7684\u767b\u5f55\u7cfb\u7edf\u3002\u8fd9\u4e2a\u7cfb\u7edf\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u8981\u7d20\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u5b58\u50a8\uff08\u53ef\u4ee5\u662f\u786c\u7f16\u7801\u7684\uff0c\u4e5f\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\uff09\u3002<\/li>\n<li>\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u63a5\u53e3\u3002<\/li>\n<li>\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u903b\u8f91\u3002<\/li>\n<li>\u8ba1\u6570\u5668\u6765\u8ddf\u8e2a\u5c1d\u8bd5\u6b21\u6570\u3002<\/li>\n<li>\u5728\u7528\u6237\u5c1d\u8bd5\u8d85\u8fc73\u6b21\u540e\uff0c\u7cfb\u7edf\u5c06\u63d0\u793a\u5e76\u7ec8\u6b62\u767b\u5f55\u8fc7\u7a0b\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5b58\u50a8\u548c\u9a8c\u8bc1\u903b\u8f91<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7528\u6237\u540d\u548c\u5bc6\u7801\u786c\u7f16\u7801\u5728\u811a\u672c\u4e2d\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u6216\u8005\u5916\u90e8\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u6b63\u786e\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801<\/p>\n<p>correct_username = &quot;admin&quot;<\/p>\n<p>correct_password = &quot;password123&quot;<\/p>\n<p>def validate_credentials(username, password):<\/p>\n<p>    &quot;&quot;&quot;\u9a8c\u8bc1\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e&quot;&quot;&quot;<\/p>\n<p>    return username == correct_username and password == correct_password<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7528\u6237\u8f93\u5165\u548c\u5c1d\u8bd5\u6b21\u6570\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a<code>while<\/code>\u5faa\u73af\u6765\u63a7\u5236\u7528\u6237\u7684\u5c1d\u8bd5\u6b21\u6570\uff0c\u5e76\u5728\u6bcf\u6b21\u5931\u8d25\u540e\u589e\u52a0\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">attempts = 0  # \u521d\u59cb\u5316\u5c1d\u8bd5\u6b21\u6570<\/p>\n<p>while attempts &lt; 3:<\/p>\n<p>    username = input(&quot;\u8bf7\u8f93\u5165\u7528\u6237\u540d: &quot;)<\/p>\n<p>    password = input(&quot;\u8bf7\u8f93\u5165\u5bc6\u7801: &quot;)<\/p>\n<p>    if validate_credentials(username, password):<\/p>\n<p>        print(&quot;\u767b\u5f55\u6210\u529f\uff01&quot;)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        attempts += 1<\/p>\n<p>        print(&quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\u3002&quot;)<\/p>\n<p>if attempts == 3:<\/p>\n<p>    print(&quot;\u5c1d\u8bd5\u6b21\u6570\u8d85\u8fc7\u9650\u5236\uff0c\u767b\u5f55\u5931\u8d25\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u5584\u767b\u5f55\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u767b\u5f55\u7cfb\u7edf\u9700\u8981\u66f4\u52a0\u5065\u58ee\u548c\u5b89\u5168\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u4ee5\u8003\u8651\u7684\u6539\u8fdb\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bc6\u7801\u52a0\u5bc6<\/strong>\uff1a\u5b58\u50a8\u548c\u4f20\u8f93\u5bc6\u7801\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u8fdb\u884c\u52a0\u5bc6\u3002<\/li>\n<li><strong>\u9519\u8bef\u65e5\u5fd7<\/strong>\uff1a\u8bb0\u5f55\u5931\u8d25\u7684\u767b\u5f55\u5c1d\u8bd5\u548c\u5176\u4ed6\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7528\u6237\u9501\u5b9a\u673a\u5236<\/strong>\uff1a\u5728\u591a\u6b21\u5931\u8d25\u540e\u9501\u5b9a\u7528\u6237\u8d26\u53f7\uff0c\u5e76\u901a\u77e5\u7ba1\u7406\u5458\u3002<\/li>\n<li><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong>\uff1a\u5bf9\u8f93\u5165\u8fdb\u884c\u57fa\u672c\u7684\u683c\u5f0f\u9a8c\u8bc1\uff0c\u9632\u6b62SQL\u6ce8\u5165\u548c\u5176\u4ed6\u653b\u51fb\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<h2><strong>\u4f7f\u7528SHA-256\u7b97\u6cd5\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6<\/strong><\/h2>\n<p>def hash_password(password):<\/p>\n<p>    return hashlib.sha256(password.encode()).hexdigest()<\/p>\n<h2><strong>\u5047\u8bbe\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7684\u54c8\u5e0c\u5bc6\u7801<\/strong><\/h2>\n<p>stored_hashed_password = hash_password(&quot;password123&quot;)<\/p>\n<p>def validate_credentials(username, password):<\/p>\n<p>    &quot;&quot;&quot;\u9a8c\u8bc1\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e&quot;&quot;&quot;<\/p>\n<p>    hashed_password = hash_password(password)<\/p>\n<p>    return username == correct_username and hashed_password == stored_hashed_password<\/p>\n<p>attempts = 0  # \u521d\u59cb\u5316\u5c1d\u8bd5\u6b21\u6570<\/p>\n<p>while attempts &lt; 3:<\/p>\n<p>    username = input(&quot;\u8bf7\u8f93\u5165\u7528\u6237\u540d: &quot;)<\/p>\n<p>    password = input(&quot;\u8bf7\u8f93\u5165\u5bc6\u7801: &quot;)<\/p>\n<p>    if validate_credentials(username, password):<\/p>\n<p>        print(&quot;\u767b\u5f55\u6210\u529f\uff01&quot;)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        attempts += 1<\/p>\n<p>        print(&quot;\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\u3002&quot;)<\/p>\n<p>if attempts == 3:<\/p>\n<p>    print(&quot;\u5c1d\u8bd5\u6b21\u6570\u8d85\u8fc7\u9650\u5236\uff0c\u767b\u5f55\u5931\u8d25\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u767b\u5f55\u7cfb\u7edf\uff0c\u5e76\u9650\u5236\u7528\u6237\u7684\u767b\u5f55\u5c1d\u8bd5\u6b21\u6570\u4e3a3\u6b21\u3002\u6211\u4eec\u9996\u5148\u4ecb\u7ecd\u4e86\u57fa\u672c\u7684\u7cfb\u7edf\u8bbe\u8ba1\uff0c\u7136\u540e\u9010\u6b65\u5b8c\u5584\u4e86\u767b\u5f55\u903b\u8f91\uff0c\u6700\u540e\u63d0\u51fa\u4e86\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6539\u8fdb\u5efa\u8bae\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u7cfb\u7edf\uff0c\u8fd8\u80fd\u591f\u4e3a\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u7528\u6237\u8ba4\u8bc1\u63d0\u4f9b\u53c2\u8003\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u767b\u5f55\u7cfb\u7edf\u7684\u8bbe\u8ba1\u539f\u5219\u4e0d\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u811a\u672c\uff0c\u4e5f\u53ef\u4ee5\u6269\u5c55\u5230\u590d\u6742\u7684\u5e94\u7528\u4e2d\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u66f4\u591a\u95ee\u9898\u6216\u8005\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u8ba8\u8bba\uff0c\u6b22\u8fce\u968f\u65f6\u63d0\u95ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7528\u6237\u767b\u5f55\u5c1d\u8bd5\u6b21\u6570\u9650\u5236\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b9e\u73b0\u7528\u6237\u767b\u5f55\u7684\u5c1d\u8bd5\u6b21\u6570\u9650\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u68c0\u67e5\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u5982\u679c\u7528\u6237\u8fde\u7eed\u8f93\u5165\u9519\u8bef\u4fe1\u606f\u8d85\u8fc7\u4e09\u6b21\uff0c\u53ef\u4ee5\u7ed9\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u8ddf\u8e2a\u5c1d\u8bd5\u6b21\u6570\uff0c\u5e76\u5728\u6bcf\u6b21\u5931\u8d25\u65f6\u9012\u589e\u8ba1\u6570\u5668\u3002<\/p>\n<p><strong>\u767b\u5f55\u5931\u8d25\u540e\u5982\u4f55\u5411\u7528\u6237\u63d0\u4f9b\u53cd\u9988\uff1f<\/strong><br \/>\u5728\u6bcf\u6b21\u767b\u5f55\u5931\u8d25\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u6d88\u606f\u6765\u544a\u77e5\u7528\u6237\u8f93\u5165\u9519\u8bef\u3002\u53ef\u4ee5\u8ba9\u7528\u6237\u660e\u786e\u77e5\u9053\u5f53\u524d\u5df2\u5c1d\u8bd5\u7684\u6b21\u6570\uff0c\u5e76\u63d0\u9192\u4ed6\u4eec\u5269\u4f59\u7684\u5c1d\u8bd5\u673a\u4f1a\u3002\u8fd9\u6837\u7684\u53cd\u9988\u4e0d\u4ec5\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u8fd8\u80fd\u51cf\u5c11\u56e0\u8f93\u5165\u9519\u8bef\u800c\u5bfc\u81f4\u7684\u632b\u8d25\u611f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b9e\u73b0\u767b\u5f55\u529f\u80fd\u65f6\u4fdd\u62a4\u7528\u6237\u7684\u654f\u611f\u4fe1\u606f\uff1f<\/strong><br \/>\u5728\u5b9e\u73b0\u767b\u5f55\u529f\u80fd\u65f6\uff0c\u5e94\u786e\u4fdd\u4e0d\u5728\u63a7\u5236\u53f0\u6216\u65e5\u5fd7\u6587\u4ef6\u4e2d\u8bb0\u5f55\u7528\u6237\u7684\u5bc6\u7801\u3002\u53ef\u4ee5\u4f7f\u7528\u8f93\u5165\u906e\u7f69\u529f\u80fd\u6765\u9690\u85cf\u5bc6\u7801\uff0c\u5e76\u901a\u8fc7\u5b89\u5168\u7684\u65b9\u5f0f\u5b58\u50a8\u548c\u9a8c\u8bc1\u5bc6\u7801\uff0c\u5982\u91c7\u7528\u54c8\u5e0c\u7b97\u6cd5\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528HTTPS\u6765\u52a0\u5bc6\u7528\u6237\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u901a\u4fe1\uff0c\u589e\u52a0\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u4e00\u4e2a\u767b\u5f55\u7cfb\u7edf\u5e76\u9650\u5236\u5c1d\u8bd5\u6b21\u6570\u4e3a3\u6b21\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5faa\u73af\u63a7\u5236\u767b\u5f55\u5c1d [&hellip;]","protected":false},"author":3,"featured_media":1124349,"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\/1124332"}],"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=1124332"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124332\/revisions"}],"predecessor-version":[{"id":1124351,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124332\/revisions\/1124351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124349"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}