{"id":959889,"date":"2024-12-27T03:44:48","date_gmt":"2024-12-26T19:44:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959889.html"},"modified":"2024-12-27T03:44:51","modified_gmt":"2024-12-26T19:44:51","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e7%99%bb%e5%bd%95%e5%af%86%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959889.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u767b\u5f55\u5bc6\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102558\/6767ef89-e4db-4f42-abd5-b9ee3728f439.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u767b\u5f55\u5bc6\u7801\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u663e\u793a\u767b\u5f55\u5bc6\u7801\u901a\u5e38\u4e0d\u662f\u4e00\u4e2a\u63a8\u8350\u7684\u505a\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u5b89\u5168\u9690\u60a3\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u5bc6\u7801\u5e76\u663e\u793a\u51fa\u6765\u4ee5\u786e\u4fdd\u6b63\u786e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u65b9\u6cd5\u548c\u5efa\u8bae\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528getpass\u6a21\u5757<\/strong>\uff1agetpass\u6a21\u5757\u901a\u5e38\u7528\u4e8e\u9690\u85cf\u5bc6\u7801\u8f93\u5165\uff0c\u5982\u679c\u60a8\u60f3\u5728\u8f93\u5165\u65f6\u663e\u793a\u5bc6\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6765\u5b9e\u73b0\u3002  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import getpass<\/p>\n<p>password = getpass.getpass(&quot;Enter your password: &quot;)<\/p>\n<p>print(&quot;You entered:&quot;, password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5bc6\u7801\u8f93\u5165\u540e\u4f1a\u663e\u793a\u51fa\u6765\uff0c\u4f46\u5728\u8f93\u5165\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u663e\u793a\u3002\u4e3a\u4e86\u5728\u8f93\u5165\u8fc7\u7a0b\u4e2d\u663e\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u5e95\u5c42\u7684\u8f93\u5165\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5229\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa<\/strong>\uff1a\u901a\u8fc7\u6355\u83b7\u952e\u76d8\u8f93\u5165\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u65f6\u5b9e\u65f6\u663e\u793a\u5bc6\u7801\u3002\u7136\u800c\uff0c\u8fd9\u901a\u5e38\u9700\u8981\u7ed3\u5408\u64cd\u4f5c\u7cfb\u7edf\u7684\u7279\u5b9a\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>msvcrt<\/code>\u5e93\u6765\u5b9e\u73b0\u5b57\u7b26\u9010\u4e2a\u8bfb\u53d6\u5e76\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import msvcrt<\/p>\n<p>def get_password(prompt=&#39;Enter your password: &#39;):<\/p>\n<p>    print(prompt, end=&#39;&#39;, flush=True)<\/p>\n<p>    password = &quot;&quot;<\/p>\n<p>    while True:<\/p>\n<p>        char = msvcrt.getch()<\/p>\n<p>        if char == b&#39;\\r&#39;:  # Enter key pressed<\/p>\n<p>            break<\/p>\n<p>        print(char.decode(&#39;utf-8&#39;), end=&#39;&#39;, flush=True)<\/p>\n<p>        password += char.decode(&#39;utf-8&#39;)<\/p>\n<p>    print()  # Move to the next line<\/p>\n<p>    return password<\/p>\n<p>password = get_password()<\/p>\n<p>print(&quot;You entered:&quot;, password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Unix\/Linux\u7cfb\u7edf<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u548c<code>termios<\/code>\u5e93\u6765\u6355\u83b7\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import tty<\/p>\n<p>import termios<\/p>\n<p>def get_password(prompt=&#39;Enter your password: &#39;):<\/p>\n<p>    print(prompt, end=&#39;&#39;, flush=True)<\/p>\n<p>    password = &quot;&quot;<\/p>\n<p>    fd = sys.stdin.fileno()<\/p>\n<p>    old_settings = termios.tcgetattr(fd)<\/p>\n<p>    try:<\/p>\n<p>        tty.setraw(sys.stdin.fileno())<\/p>\n<p>        while True:<\/p>\n<p>            char = sys.stdin.read(1)<\/p>\n<p>            if char == &#39;\\n&#39;:  # Enter key pressed<\/p>\n<p>                break<\/p>\n<p>            print(char, end=&#39;&#39;, flush=True)<\/p>\n<p>            password += char<\/p>\n<p>    finally:<\/p>\n<p>        termios.tcsetattr(fd, termios.TCSADR<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>N, old_settings)<\/p>\n<p>    print()  # Move to the next line<\/p>\n<p>    return password<\/p>\n<p>password = get_password()<\/p>\n<p>print(&quot;You entered:&quot;, password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u5168\u6027\u8003\u8651<\/strong>\uff1a<strong>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u663e\u793a\u7528\u6237\u5bc6\u7801\u662f\u4e0d\u5b89\u5168\u7684\u505a\u6cd5<\/strong>\u3002\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0c\u5bc6\u7801\u90fd\u4e0d\u5e94\u8be5\u4ee5\u660e\u6587\u5f62\u5f0f\u5b58\u50a8\u6216\u663e\u793a\u3002\u5efa\u8bae\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570\u52a0\u5bc6\u5bc6\u7801\uff0c\u4f8b\u5982bcrypt\u6216argon2\u3002\u5728\u8c03\u8bd5\u73af\u5883\u4e2d\u663e\u793a\u5bc6\u7801\u540e\uff0c\u786e\u4fdd\u5728\u53d1\u5e03\u4ee3\u7801\u65f6\u79fb\u9664\u8fd9\u4e9b\u90e8\u5206\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7528\u6237\u4f53\u9a8c<\/strong>\uff1a\u5728\u9700\u8981\u663e\u793a\u8f93\u5165\u7684\u60c5\u51b5\u4e0b\uff0c\u660e\u786e\u544a\u77e5\u7528\u6237\uff0c\u5e76\u786e\u4fdd\u5728\u6700\u7ec8\u7528\u6237\u754c\u9762\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u63d0\u793a\u4fe1\u606f\u6765\u5f15\u5bfc\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u663e\u793a\u767b\u5f55\u5bc6\u7801\u7684\u529f\u80fd\u3002\u7136\u800c\uff0c\u52a1\u5fc5\u7262\u8bb0\u5b89\u5168\u7684\u91cd\u8981\u6027\uff0c<strong>\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4e0d\u5e94\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5b9e\u73b0\u6b64\u529f\u80fd<\/strong>\u3002\u5bf9\u4e8e\u9700\u8981\u66f4\u9ad8\u5b89\u5168\u6027\u7684\u5e94\u7528\uff0c\u8003\u8651\u4f7f\u7528\u591a\u56e0\u7d20\u8ba4\u8bc1\u6216\u5176\u4ed6\u66f4\u5b89\u5168\u7684\u8eab\u4efd\u9a8c\u8bc1\u673a\u5236\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u5904\u7406\u548c\u663e\u793a\u767b\u5f55\u5bc6\u7801\uff1f<\/strong><br \/>\u5728\u5904\u7406\u767b\u5f55\u5bc6\u7801\u65f6\uff0c\u5b89\u5168\u6027\u662f\u6700\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757\u5982<code>getpass<\/code>\u6765\u5b89\u5168\u5730\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\uff0c\u800c\u4e0d\u5728\u7ec8\u7aef\u663e\u793a\u3002\u4f7f\u7528<code>getpass.getpass()<\/code>\u51fd\u6570\u65f6\uff0c\u8f93\u5165\u7684\u5bc6\u7801\u5c06\u4e0d\u4f1a\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\uff0c\u4ece\u800c\u4fdd\u62a4\u7528\u6237\u9690\u79c1\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u52a0\u5bc6\u548c\u89e3\u5bc6\u5bc6\u7801\uff1f<\/strong><br \/>\u5bf9\u4e8e\u9700\u8981\u5b58\u50a8\u7684\u5bc6\u7801\uff0c\u5efa\u8bae\u4f7f\u7528\u52a0\u5bc6\u6280\u672f\u6765\u63d0\u9ad8\u5b89\u5168\u6027\u3002\u53ef\u4ee5\u4f7f\u7528<code>hashlib<\/code>\u5e93\u6765\u751f\u6210\u5bc6\u7801\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u4f7f\u7528<code>bcrypt<\/code>\u7b49\u5e93\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002\u5b58\u50a8\u65f6\u53ea\u9700\u4fdd\u5b58\u54c8\u5e0c\u503c\uff0c\u767b\u5f55\u65f6\u518d\u6bd4\u8f83\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u54c8\u5e0c\u503c\u4e0e\u5b58\u50a8\u7684\u54c8\u5e0c\u503c\u662f\u5426\u5339\u914d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python GUI\u5e94\u7528\u4e2d\u663e\u793a\u548c\u9690\u85cf\u5bc6\u7801\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tkinter<\/code>\u5e93\u4e2d\u7684<code>Entry<\/code>\u5c0f\u90e8\u4ef6\u6765\u8f93\u5165\u5bc6\u7801\u3002\u901a\u8fc7\u8bbe\u7f6e<code>show<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u63a7\u5236\u5bc6\u7801\u7684\u663e\u793a\u548c\u9690\u85cf\u3002\u4f8b\u5982\uff0c<code>entry = Entry(root, show=&#39;*&#39;)<\/code>\u4f1a\u5c06\u8f93\u5165\u7684\u5bc6\u7801\u663e\u793a\u4e3a\u661f\u53f7\uff0c\u4ece\u800c\u907f\u514d\u6cc4\u9732\u5bc6\u7801\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u663e\u793a\u767b\u5f55\u5bc6\u7801\u901a\u5e38\u4e0d\u662f\u4e00\u4e2a\u63a8\u8350\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u5b89\u5168\u9690\u60a3\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\uff0c\u6216\u8005 [&hellip;]","protected":false},"author":3,"featured_media":959897,"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\/959889"}],"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=959889"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959889\/revisions"}],"predecessor-version":[{"id":959900,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959889\/revisions\/959900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959897"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}