{"id":1087452,"date":"2025-01-08T13:35:11","date_gmt":"2025-01-08T05:35:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087452.html"},"modified":"2025-01-08T13:35:13","modified_gmt":"2025-01-08T05:35:13","slug":"python%e5%a6%82%e4%bd%95%e6%b3%a8%e5%86%8c%e7%94%a8%e6%88%b7%e5%90%8d%e5%92%8c%e5%af%86%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087452.html","title":{"rendered":"Python\u5982\u4f55\u6ce8\u518c\u7528\u6237\u540d\u548c\u5bc6\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200401\/d40926f5-7f61-4025-b557-f6dc80efc4cd.webp\" alt=\"Python\u5982\u4f55\u6ce8\u518c\u7528\u6237\u540d\u548c\u5bc6\u7801\" \/><\/p>\n<p><p> <strong>Python\u6ce8\u518c\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u9a8c\u8bc1\u8f93\u5165\u3001\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u3001\u52a0\u5bc6\u5bc6\u7801\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u52a0\u5bc6\u5bc6\u7801\u662f\u786e\u4fdd\u7528\u6237\u6570\u636e\u5b89\u5168\u7684\u5173\u952e\u6b65\u9aa4\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/p>\n<p>\u5728\u6ce8\u518c\u7528\u6237\u548c\u5bc6\u7801\u65f6\uff0c\u9996\u5148\u8981\u8bbe\u8ba1\u4e00\u4e2a\u53cb\u597d\u7684\u7528\u6237\u754c\u9762\u3002\u7528\u6237\u754c\u9762\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u754c\u9762\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002\u5728\u547d\u4ee4\u884c\u754c\u9762\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u800c\u5728\u56fe\u5f62\u7528\u6237\u754c\u9762\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8868\u5355\u8f93\u5165\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">username = input(&quot;Enter your username: &quot;)<\/p>\n<p>password = input(&quot;Enter your password: &quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u9a8c\u8bc1\u8f93\u5165<\/p>\n<p>\u5728\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u540e\uff0c\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u8f93\u5165\u7684\u5408\u6cd5\u6027\u3002\u4f8b\u5982\uff0c\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u957f\u5ea6\u3001\u662f\u5426\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u4fe1\u606f\u7b26\u5408\u9884\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def validate_input(username, password):<\/p>\n<p>    if len(username) &lt; 5:<\/p>\n<p>        return False, &quot;Username must be at least 5 characters long&quot;<\/p>\n<p>    if len(password) &lt; 8:<\/p>\n<p>        return False, &quot;Password must be at least 8 characters long&quot;<\/p>\n<p>    return True, &quot;Valid input&quot;<\/p>\n<p>is_valid, message = validate_input(username, password)<\/p>\n<p>if not is_valid:<\/p>\n<p>    print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b58\u50a8\u7528\u6237\u4fe1\u606f<\/p>\n<p>\u9a8c\u8bc1\u8f93\u5165\u5408\u6cd5\u540e\uff0c\u9700\u8981\u5c06\u7528\u6237\u4fe1\u606f\u5b58\u50a8\u5728\u4e00\u4e2a\u6301\u4e45\u5316\u5b58\u50a8\u4e2d\uff0c\u5982\u6570\u636e\u5e93\u6216\u6587\u4ef6\u3002\u4e3a\u4e86\u6f14\u793a\uff0c\u6211\u4eec\u5c06\u7528\u6237\u4fe1\u606f\u5b58\u50a8\u5728\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def store_user_info(username, password):<\/p>\n<p>    with open(&quot;users.txt&quot;, &quot;a&quot;) as file:<\/p>\n<p>        file.write(f&quot;{username},{password}\\n&quot;)<\/p>\n<p>store_user_info(username, password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u52a0\u5bc6\u5bc6\u7801<\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\u6027\uff0c\u5fc5\u987b\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5b58\u50a8\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u52a0\u5bc6\u5e93\uff0c\u4f8b\u5982<code>hashlib<\/code>\u548c<code>bcrypt<\/code>\u3002\u6211\u4eec\u5c06\u4f7f\u7528<code>bcrypt<\/code>\u6765\u52a0\u5bc6\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bcrypt<\/p>\n<p>def encrypt_password(password):<\/p>\n<p>    salt = bcrypt.gensalt()<\/p>\n<p>    hashed_password = bcrypt.hashpw(password.encode(&#39;utf-8&#39;), salt)<\/p>\n<p>    return hashed_password<\/p>\n<p>encrypted_password = encrypt_password(password)<\/p>\n<p>store_user_info(username, encrypted_password.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b8c\u6574\u793a\u4f8b<\/p>\n<p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6ce8\u518c\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u793a\u4f8b\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bcrypt<\/p>\n<p>def validate_input(username, password):<\/p>\n<p>    if len(username) &lt; 5:<\/p>\n<p>        return False, &quot;Username must be at least 5 characters long&quot;<\/p>\n<p>    if len(password) &lt; 8:<\/p>\n<p>        return False, &quot;Password must be at least 8 characters long&quot;<\/p>\n<p>    return True, &quot;Valid input&quot;<\/p>\n<p>def encrypt_password(password):<\/p>\n<p>    salt = bcrypt.gensalt()<\/p>\n<p>    hashed_password = bcrypt.hashpw(password.encode(&#39;utf-8&#39;), salt)<\/p>\n<p>    return hashed_password<\/p>\n<p>def store_user_info(username, password):<\/p>\n<p>    with open(&quot;users.txt&quot;, &quot;a&quot;) as file:<\/p>\n<p>        file.write(f&quot;{username},{password}\\n&quot;)<\/p>\n<p>def register_user():<\/p>\n<p>    username = input(&quot;Enter your username: &quot;)<\/p>\n<p>    password = input(&quot;Enter your password: &quot;)<\/p>\n<p>    is_valid, message = validate_input(username, password)<\/p>\n<p>    if not is_valid:<\/p>\n<p>        print(message)<\/p>\n<p>        return<\/p>\n<p>    encrypted_password = encrypt_password(password)<\/p>\n<p>    store_user_info(username, encrypted_password.decode(&#39;utf-8&#39;))<\/p>\n<p>    print(&quot;User registered successfully!&quot;)<\/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>    register_user()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u91cd\u70b9\u5185\u5bb9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/strong>\uff1a\u63d0\u4f9b\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u754c\u9762\u3002<\/li>\n<li><strong>\u9a8c\u8bc1\u8f93\u5165<\/strong>\uff1a\u786e\u4fdd\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b26\u5408\u9884\u671f\u6807\u51c6\u3002<\/li>\n<li><strong>\u5b58\u50a8\u7528\u6237\u4fe1\u606f<\/strong>\uff1a\u5c06\u7528\u6237\u4fe1\u606f\u5b58\u50a8\u5728\u6301\u4e45\u5316\u5b58\u50a8\u4e2d\u3002<\/li>\n<li><strong>\u52a0\u5bc6\u5bc6\u7801<\/strong>\uff1a\u4f7f\u7528<code>bcrypt<\/code>\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5b58\u50a8\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u6ce8\u518c\u7cfb\u7edf\uff0c\u5e76\u786e\u4fdd\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\u6027\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\u6ce8\u518c\u529f\u80fd\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b9e\u73b0\u7528\u6237\u6ce8\u518c\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49web\u6846\u67b6\u3002\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u5904\u7406\u7528\u6237\u8f93\u5165\u548c\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u4fbf\u6377\u5de5\u5177\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u8868\u5355\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u4fe1\u606f\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u786e\u4fdd\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\u5904\u7406\uff0c\u4ee5\u4fdd\u62a4\u7528\u6237\u7684\u9690\u79c1\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u5728\u6ce8\u518c\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\u6027\u81f3\u5173\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528bcrypt\u6216argon2\u7b49\u52a0\u5bc6\u5e93\u5bf9\u5bc6\u7801\u8fdb\u884c\u54c8\u5e0c\u5904\u7406\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f7f\u7528SSL\/TLS\u52a0\u5bc6\u4f20\u8f93\u6570\u636e\uff0c\u4ee5\u9632\u6b62\u4e2d\u95f4\u4eba\u653b\u51fb\uff0c\u4fdd\u62a4\u7528\u6237\u7684\u654f\u611f\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u6ce8\u518c\u65f6\u7684\u9519\u8bef\u4fe1\u606f\uff1f<\/strong><br \/>\u5728\u7528\u6237\u6ce8\u518c\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u7528\u6237\u540d\u5df2\u88ab\u6ce8\u518c\u6216\u5bc6\u7801\u4e0d\u7b26\u5408\u8981\u6c42\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u5728\u524d\u7aef\u63d0\u4f9b\u5b9e\u65f6\u53cd\u9988\uff0c\u544a\u77e5\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u3002\u540c\u65f6\uff0c\u5728\u540e\u7aef\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u5728\u6570\u636e\u5e93\u4e2d\u63d2\u5165\u65b0\u7528\u6237\u4e4b\u524d\u8fdb\u884c\u5fc5\u8981\u7684\u68c0\u67e5\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6ce8\u518c\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u9a8c\u8bc1\u8f93\u5165\u3001\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u3001\u52a0\u5bc6\u5bc6\u7801\u7b49\u3002\u5176\u4e2d\uff0c\u52a0\u5bc6\u5bc6\u7801\u662f\u786e\u4fdd [&hellip;]","protected":false},"author":3,"featured_media":1087453,"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\/1087452"}],"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=1087452"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087452\/revisions"}],"predecessor-version":[{"id":1087456,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087452\/revisions\/1087456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087453"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}