{"id":1012844,"date":"2024-12-27T11:42:44","date_gmt":"2024-12-27T03:42:44","guid":{"rendered":""},"modified":"2024-12-27T11:42:46","modified_gmt":"2024-12-27T03:42:46","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%89%8b%e6%9c%ba%e4%bb%a4%e7%89%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1012844.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u624b\u673a\u4ee4\u724c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090451\/bcd4e990-2f33-41e4-9c6d-268a82e69e5c.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u624b\u673a\u4ee4\u724c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u624b\u673a\u4ee4\u724c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528TOTP\uff08\u57fa\u4e8e\u65f6\u95f4\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u7b97\u6cd5\uff09\u3001HOTP\uff08\u57fa\u4e8eHMAC\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u7b97\u6cd5\uff09\u3001QR\u7801\u751f\u6210\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b89\u5168\u7684\u53cc\u56e0\u7d20\u8ba4\u8bc1\u7cfb\u7edf\u3002<\/strong>\u5176\u4e2d\uff0cTOTP\u662f\u4e00\u79cd\u975e\u5e38\u6d41\u884c\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7ed3\u5408\u4e86\u65f6\u95f4\u56e0\u7d20\uff0c\u4f7f\u5f97\u5bc6\u7801\u5728\u77ed\u65f6\u95f4\u5185\u6709\u6548\uff0c\u589e\u5f3a\u4e86\u5b89\u5168\u6027\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0TOTP\u624b\u673a\u4ee4\u724c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001TOTP\u5b9e\u73b0\u539f\u7406\u53ca\u57fa\u7840<\/h3>\n<\/p>\n<p><p>TOTP\u662f\u57fa\u4e8e\u65f6\u95f4\u7684\u52a8\u6001\u5bc6\u7801\u751f\u6210\u7b97\u6cd5\u3002\u5b83\u57fa\u4e8e\u4e00\u4e2a\u5171\u4eab\u7684\u5bc6\u94a5\u548c\u5f53\u524d\u65f6\u95f4\u6233\u6765\u751f\u6210\u4e00\u6b21\u6027\u5bc6\u7801\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>pyotp<\/code>\u5e93\u6765\u5b9e\u73b0TOTP\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u4e0e\u4f7f\u7528pyotp\u5e93<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528TOTP\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pyotp<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyotp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aTOTP\u5bf9\u8c61\uff0c\u5e76\u751f\u6210\u4e00\u6b21\u6027\u5bc6\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyotp<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u94a5<\/strong><\/h2>\n<p>secret = pyotp.random_base32()<\/p>\n<p>print(&quot;\u5bc6\u94a5:&quot;, secret)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTOTP\u5bf9\u8c61<\/strong><\/h2>\n<p>totp = pyotp.TOTP(secret)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u4e00\u6b21\u6027\u5bc6\u7801<\/strong><\/h2>\n<p>otp = totp.now()<\/p>\n<p>print(&quot;\u5f53\u524dTOTP:&quot;, otp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u94a5\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aTOTP\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7<code>now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001TOTP\u5bc6\u7801\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u4e00\u6b21\u6027\u5bc6\u7801\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5b9e\u73b0\u5bc6\u7801\u7684\u9a8c\u8bc1\u529f\u80fd\u3002<code>pyotp<\/code>\u5e93\u63d0\u4f9b\u4e86<code>verify()<\/code>\u65b9\u6cd5\u6765\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u662f\u5426\u6b63\u786e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684TOTP<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165TOTP: &quot;)<\/p>\n<p>if totp.verify(user_input):<\/p>\n<p>    print(&quot;\u9a8c\u8bc1\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u9a8c\u8bc1\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001HOTP\u5b9e\u73b0\u539f\u7406\u53ca\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>HOTP\u662f\u4e00\u79cd\u57fa\u4e8e\u4e8b\u4ef6\u8ba1\u6570\u7684\u52a8\u6001\u5bc6\u7801\u751f\u6210\u7b97\u6cd5\u3002\u5b83\u4e0eTOTP\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u4e0d\u4f9d\u8d56\u4e8e\u65f6\u95f4\u6233\uff0c\u800c\u662f\u57fa\u4e8e\u4e00\u4e2a\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><h4>1\u3001HOTP\u7684\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>pyotp<\/code>\u5e93\u6765\u5b9e\u73b0HOTP\u3002\u9996\u5148\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u7136\u540e\u751f\u6210\u4e00\u6b21\u6027\u5bc6\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyotp<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u94a5<\/strong><\/h2>\n<p>secret = pyotp.random_base32()<\/p>\n<p>print(&quot;\u5bc6\u94a5:&quot;, secret)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aHOTP\u5bf9\u8c61<\/strong><\/h2>\n<p>hotp = pyotp.HOTP(secret)<\/p>\n<h2><strong>\u83b7\u53d6\u6307\u5b9a\u8ba1\u6570\u5668\u503c\u7684\u4e00\u6b21\u6027\u5bc6\u7801<\/strong><\/h2>\n<p>counter = 0<\/p>\n<p>otp = hotp.at(counter)<\/p>\n<p>print(&quot;\u5f53\u524dHOTP:&quot;, otp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001HOTP\u5bc6\u7801\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u4e0eTOTP\u7c7b\u4f3c\uff0cHOTP\u4e5f\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684HOTP<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165HOTP: &quot;)<\/p>\n<p>if hotp.verify(user_input, counter):<\/p>\n<p>    print(&quot;\u9a8c\u8bc1\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u9a8c\u8bc1\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6bcf\u6b21\u9a8c\u8bc1\u6210\u529f\u540e\u9700\u8981\u589e\u52a0\u8ba1\u6570\u5668\u503c\uff0c\u4ee5\u786e\u4fdd\u4e0b\u6b21\u751f\u6210\u4e0d\u540c\u7684\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001QR\u7801\u7684\u751f\u6210\u4e0e\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u65b9\u4fbf\u7528\u6237\u4f7f\u7528\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u5bc6\u94a5\u751f\u6210\u4e00\u4e2aQR\u7801\uff0c\u7136\u540e\u901a\u8fc7\u624b\u673a\u5e94\u7528\u626b\u63cf\u4ee5\u914d\u7f6e\u4ee4\u724c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210QR\u7801<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>qrcode<\/code>\u5e93\u6765\u751f\u6210QR\u7801\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install qrcode[pil]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e00\u4e2aQR\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyotp<\/p>\n<p>import qrcode<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5bc6\u94a5<\/strong><\/h2>\n<p>secret = pyotp.random_base32()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTOTP\u5bf9\u8c61<\/strong><\/h2>\n<p>totp = pyotp.TOTP(secret)<\/p>\n<h2><strong>\u751f\u6210OTPAuth URL<\/strong><\/h2>\n<p>uri = totp.provisioning_uri(name=&quot;user@example.com&quot;, issuer_name=&quot;MyService&quot;)<\/p>\n<h2><strong>\u751f\u6210QR\u7801<\/strong><\/h2>\n<p>qr = qrcode.make(uri)<\/p>\n<p>qr.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528QR\u7801\u8fdb\u884c\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u4f8b\u5982Google Authenticator\u7b49\u5e94\u7528\u626b\u63cf\u751f\u6210\u7684QR\u7801\uff0c\u4ee5\u914d\u7f6e\u624b\u673a\u4ee4\u724c\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u4e00\u6b21\u6027\u5bc6\u7801\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u8f93\u5165\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6574\u5408TOTP\u548cQR\u7801\u5230\u5e94\u7528\u4e2d<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06TOTP\u548cQR\u7801\u751f\u6210\u6574\u5408\u5230\u6211\u4eec\u7684\u8ba4\u8bc1\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u53cc\u56e0\u7d20\u8ba4\u8bc1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u7528\u6237\u8d26\u6237\u5e76\u751f\u6210TOTP<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u6ce8\u518c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u5176\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684TOTP\u5bc6\u94a5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u626b\u63cfQR\u7801\u7684\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyotp<\/p>\n<p>import qrcode<\/p>\n<p>class User:<\/p>\n<p>    def __init__(self, em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l):<\/p>\n<p>        self.email = email<\/p>\n<p>        self.secret = pyotp.random_base32()<\/p>\n<p>    def generate_qr(self):<\/p>\n<p>        totp = pyotp.TOTP(self.secret)<\/p>\n<p>        uri = totp.provisioning_uri(name=self.email, issuer_name=&quot;MyService&quot;)<\/p>\n<p>        qr = qrcode.make(uri)<\/p>\n<p>        qr.show()<\/p>\n<p>    def verify_otp(self, otp_input):<\/p>\n<p>        totp = pyotp.TOTP(self.secret)<\/p>\n<p>        return totp.verify(otp_input)<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7528\u6237\u5e76\u751f\u6210QR\u7801<\/strong><\/h2>\n<p>new_user = User(email=&quot;user@example.com&quot;)<\/p>\n<p>new_user.generate_qr()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9a8c\u8bc1\u7528\u6237\u7684TOTP<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u767b\u5f55\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684TOTP\uff0c\u4ee5\u786e\u4fdd\u5176\u8eab\u4efd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684TOTP<\/p>\n<p>otp_input = input(&quot;\u8bf7\u8f93\u5165TOTP: &quot;)<\/p>\n<p>if new_user.verify_otp(otp_input):<\/p>\n<p>    print(&quot;\u767b\u5f55\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u767b\u5f55\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u63d0\u9ad8\u624b\u673a\u4ee4\u724c\u7684\u5b89\u5168\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u624b\u673a\u4ee4\u724c\u7684\u5b89\u5168\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u8f83\u77ed\u7684TOTP\u6709\u6548\u671f<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cTOTP\u5bc6\u7801\u7684\u6709\u6548\u671f\u4e3a30\u79d2\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8f83\u77ed\u7684\u6709\u6548\u671f\u6765\u63d0\u9ad8\u5b89\u5168\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">totp = pyotp.TOTP(secret, interval=15)  # \u8bbe\u7f6e15\u79d2\u7684\u6709\u6548\u671f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9650\u5236\u767b\u5f55\u5c1d\u8bd5\u6b21\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u5c1d\u8bd5\u6b21\u6570\uff0c\u4ee5\u9632\u6b62\u66b4\u529b\u7834\u89e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class User:<\/p>\n<p>    def __init__(self, email):<\/p>\n<p>        self.email = email<\/p>\n<p>        self.secret = pyotp.random_base32()<\/p>\n<p>        self.attempts = 0<\/p>\n<p>        self.max_attempts = 3<\/p>\n<p>    def verify_otp(self, otp_input):<\/p>\n<p>        if self.attempts &gt;= self.max_attempts:<\/p>\n<p>            print(&quot;\u5c1d\u8bd5\u6b21\u6570\u8fc7\u591a\uff0c\u8d26\u6237\u5df2\u9501\u5b9a&quot;)<\/p>\n<p>            return False<\/p>\n<p>        totp = pyotp.TOTP(self.secret)<\/p>\n<p>        if totp.verify(otp_input):<\/p>\n<p>            self.attempts = 0<\/p>\n<p>            return True<\/p>\n<p>        else:<\/p>\n<p>            self.attempts += 1<\/p>\n<p>            return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u5b89\u5168\u3001\u6709\u6548\u7684\u624b\u673a\u4ee4\u724c\u7cfb\u7edf\u3002\u901a\u8fc7\u7ed3\u5408TOTP\u3001HOTP\u548cQR\u7801\uff0c\u6211\u4eec\u80fd\u591f\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u9ad8\u7684\u5b89\u5168\u4fdd\u969c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u624b\u673a\u4ee4\u724c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528<code>pyotp<\/code>\u5e93\u6765\u751f\u6210\u624b\u673a\u4ee4\u724c\u3002\u9996\u5148\uff0c\u5b89\u88c5\u8be5\u5e93\uff1a<code>pip install pyotp<\/code>\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u57fa\u4e8e\u65f6\u95f4\u7684\u4e00\u6b21\u6027\u5bc6\u7801\uff08TOTP\uff09\uff1a  <\/p>\n<pre><code class=\"language-python\">import pyotp\n\n# \u751f\u6210\u4e00\u4e2a\u5bc6\u94a5\nsecret = pyotp.random_base32()\nprint(&quot;\u5bc6\u94a5:&quot;, secret)\n\n# \u521b\u5efa\u4e00\u4e2aTOTP\u5bf9\u8c61\ntotp = pyotp.TOTP(secret)\n\n# \u83b7\u53d6\u5f53\u524d\u6709\u6548\u7684\u4ee4\u724c\ntoken = totp.now()\nprint(&quot;\u5f53\u524d\u4ee4\u724c:&quot;, token)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u624b\u673a\u4ee4\u724c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u9a8c\u8bc1\u624b\u673a\u4ee4\u724c\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u5728\u751f\u6210\u4ee4\u724c\u540e\uff0c\u60a8\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u4ee4\u724c\u662f\u5426\u6709\u6548\u3002\u4f7f\u7528<code>pyotp<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9a8c\u8bc1\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u5047\u8bbe\u7528\u6237\u8f93\u5165\u7684\u4ee4\u724c\nuser_input = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u4ee4\u724c: &quot;)\n\n# \u9a8c\u8bc1\u4ee4\u724c\nis_valid = totp.verify(user_input)\nif is_valid:\n    print(&quot;\u4ee4\u724c\u6709\u6548&quot;)\nelse:\n    print(&quot;\u4ee4\u724c\u65e0\u6548&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u4ee4\u724c\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5c06\u624b\u673a\u4ee4\u724c\u4e0e\u7528\u6237\u8d26\u6237\u7ed1\u5b9a\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u552f\u4e00\u7684\u624b\u673a\u4ee4\u724c\uff0c\u60a8\u53ef\u4ee5\u5728\u7528\u6237\u6ce8\u518c\u65f6\u4e3a\u6bcf\u4e2a\u7528\u6237\u751f\u6210\u4e00\u4e2a\u72ec\u7279\u7684\u5bc6\u94a5\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u6bcf\u6b21\u7528\u6237\u9700\u8981\u751f\u6210\u4ee4\u724c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u6570\u636e\u5e93\u83b7\u53d6\u8be5\u7528\u6237\u7684\u5bc6\u94a5\uff0c\u8fdb\u800c\u751f\u6210\u4ee4\u724c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u5047\u8bbe\u7528\u6237\u7684\u5bc6\u94a5\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\nuser_secret = get_user_secret_from_db(user_id)  # \u4ece\u6570\u636e\u5e93\u83b7\u53d6\u7528\u6237\u5bc6\u94a5\ntotp = pyotp.TOTP(user_secret)\ntoken = totp.now()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u7528\u6237\u90fd\u62e5\u6709\u72ec\u7acb\u7684\u4ee4\u724c\uff0c\u589e\u5f3a\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u624b\u673a\u4ee4\u724c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528TOTP\uff08\u57fa\u4e8e\u65f6\u95f4\u7684\u4e00\u6b21\u6027\u5bc6\u7801\u7b97\u6cd5\uff09\u3001HOTP\uff08\u57fa\u4e8eHM [&hellip;]","protected":false},"author":3,"featured_media":1012846,"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\/1012844"}],"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=1012844"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012844\/revisions"}],"predecessor-version":[{"id":1012847,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012844\/revisions\/1012847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012846"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}