{"id":977990,"date":"2024-12-27T06:34:06","date_gmt":"2024-12-26T22:34:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977990.html"},"modified":"2024-12-27T06:34:08","modified_gmt":"2024-12-26T22:34:08","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e6%b7%bb%e5%8a%a0ssl","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977990.html","title":{"rendered":"\u5982\u4f55\u5728python\u6dfb\u52a0ssl"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203203\/2d1495f5-8d4a-4fc8-ab71-be84804f1bd3.webp\" alt=\"\u5982\u4f55\u5728python\u6dfb\u52a0ssl\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6dfb\u52a0SSL\u6709\u591a\u4e2a\u65b9\u6cd5\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684ssl\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982requests\u3001urllib3\uff09\u4ee5\u53ca\u4e3aFlask\u6216Django\u7b49\u6846\u67b6\u914d\u7f6eSSL\u8bc1\u4e66<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u6dfb\u52a0SSL\u5e76\u4f7f\u7528SSL\u8fdb\u884c\u5b89\u5168\u7684\u7f51\u7edc\u901a\u4fe1\uff0c\u5c24\u5176\u662f\u5982\u4f55\u5904\u7406\u8bc1\u4e66\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684SSL\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u81ea\u5e26\u7684<code>ssl<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u4e3a\u7f51\u7edc\u901a\u4fe1\u6dfb\u52a0SSL\u652f\u6301\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u624b\u52a8\u63a7\u5236SSL\u4e0a\u4e0b\u6587\u7684\u5e94\u7528\u7a0b\u5e8f\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u521b\u5efaSSL\u4e0a\u4e0b\u6587<\/h4>\n<\/p>\n<p><p>SSL\u4e0a\u4e0b\u6587\uff08<code>SSLContext<\/code>\uff09\u662fSSL\u914d\u7f6e\u7684\u6838\u5fc3\uff0c\u5b83\u51b3\u5b9a\u4e86SSL\u8fde\u63a5\u7684\u884c\u4e3a\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49SSL\u4e0a\u4e0b\u6587\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u8bc1\u4e66\u9a8c\u8bc1\u3001\u534f\u8bae\u7248\u672c\u4ee5\u53ca\u5bc6\u7801\u5957\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ssl<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4SSL\u4e0a\u4e0b\u6587<\/strong><\/h2>\n<p>context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)<\/p>\n<h2><strong>\u7981\u7528\u5bf9\u65e0\u6548\u8bc1\u4e66\u7684\u68c0\u67e5\uff08\u4e0d\u63a8\u8350\u7528\u4e8e\u751f\u4ea7\u73af\u5883\uff09<\/strong><\/h2>\n<h2><strong>context.check_hostname = False<\/strong><\/h2>\n<h2><strong>context.verify_mode = ssl.CERT_NONE<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c<strong>\u5f3a\u70c8\u5efa\u8bae\u542f\u7528\u8bc1\u4e66\u9a8c\u8bc1<\/strong>\uff0c\u4ee5\u786e\u4fdd\u8fde\u63a5\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528SSL\u4e0a\u4e0b\u6587\u521b\u5efa\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u6709\u4e86SSL\u4e0a\u4e0b\u6587\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u5b89\u5168\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>hostname = &#39;www.example.com&#39;<\/p>\n<p>port = 443<\/p>\n<h2><strong>\u521b\u5efaTCP\u8fde\u63a5<\/strong><\/h2>\n<p>sock = socket.create_connection((hostname, port))<\/p>\n<h2><strong>\u4f7f\u7528SSL\u4e0a\u4e0b\u6587\u5305\u88c5\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>with context.wrap_socket(sock, server_hostname=hostname) as ssock:<\/p>\n<p>    print(ssock.version())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u5bf9SSL\u8fde\u63a5\u8fdb\u884c\u7ec6\u7c92\u5ea6\u63a7\u5236\uff0c\u4f46\u53ef\u80fd\u4e0d\u5982\u4f7f\u7528\u66f4\u9ad8\u7ea7\u7684\u5e93\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528<code>requests<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>requests<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u5e93\uff0c\u652f\u6301SSL\u7684\u5f00\u7bb1\u5373\u7528\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u5904\u7406HTTPS\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/www.example.com&#39;<\/p>\n<h2><strong>\u7b80\u5355\u7684GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>requests<\/code>\u4f1a\u9a8c\u8bc1SSL\u8bc1\u4e66\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u8bc1\u4e66\uff0c\u53ef\u4ee5\u901a\u8fc7<code>verify<\/code>\u53c2\u6570\u6307\u5b9a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response = requests.get(url, verify=&#39;\/path\/to\/cert.pem&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528<code>urllib3<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>urllib3<\/code>\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684HTTP\u5e93\uff0c\u652f\u6301\u66f4\u591a\u7684\u529f\u80fd\u548c\u914d\u7f6e\u9009\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib3<\/p>\n<p>http = urllib3.PoolManager(<\/p>\n<p>    cert_reqs=&#39;CERT_REQUIRED&#39;,<\/p>\n<p>    ca_certs=&#39;\/path\/to\/certfile&#39;<\/p>\n<p>)<\/p>\n<p>url = &#39;https:\/\/www.example.com&#39;<\/p>\n<h2><strong>\u53d1\u8d77\u8bf7\u6c42<\/strong><\/h2>\n<p>response = http.request(&#39;GET&#39;, url)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u6570\u636e<\/strong><\/h2>\n<p>print(response.data.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>urllib3<\/code>\u5141\u8bb8\u60a8\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236SSL\u884c\u4e3a\uff0c\u5305\u62ec\u6307\u5b9a\u8bc1\u4e66\u6587\u4ef6\u548c\u7981\u7528\u8b66\u544a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e3aWeb\u6846\u67b6\u914d\u7f6eSSL<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fWeb\u6846\u67b6\uff0c\u5982Flask\u6216Django\uff0c\u5219\u9700\u8981\u4e3a\u5b83\u4eec\u914d\u7f6eSSL\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4e3aFlask\u5e94\u7528\u914d\u7f6eSSL<\/h4>\n<\/p>\n<p><p>Flask\u53ef\u4ee5\u901a\u8fc7<code>ssl_context<\/code>\u53c2\u6570\u5728\u8fd0\u884c\u65f6\u6dfb\u52a0SSL\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello():<\/p>\n<p>    return &quot;Hello, Secure World!&quot;<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    # \u6307\u5b9a\u8bc1\u4e66\u548c\u5bc6\u94a5\u6587\u4ef6<\/p>\n<p>    app.run(ssl_context=(&#39;cert.pem&#39;, &#39;key.pem&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4e3aDjango\u5e94\u7528\u914d\u7f6eSSL<\/h4>\n<\/p>\n<p><p>Django\u901a\u5e38\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u901a\u8fc7\u53cd\u5411\u4ee3\u7406\uff08\u5982Nginx\u6216Apache\uff09\u5904\u7406SSL\u3002\u8981\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u4f7f\u7528SSL\uff0c\u53ef\u4ee5\u4f7f\u7528<code>runsslserver<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django-sslserver<\/p>\n<p>python manage.py runsslserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001SSL\u8bc1\u4e66\u7ba1\u7406<\/h3>\n<\/p>\n<p><h4>4.1 \u751f\u6210\u81ea\u7b7e\u540d\u8bc1\u4e66<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528\u81ea\u7b7e\u540d\u8bc1\u4e66\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528Let&#39;s Encrypt\u83b7\u53d6\u514d\u8d39\u8bc1\u4e66<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u53ef\u4fe1\u7684\u8bc1\u4e66\u9881\u53d1\u673a\u6784\uff08CA\uff09\u63d0\u4f9b\u7684\u8bc1\u4e66\u662f\u5fc5\u8981\u7684\u3002Let&#39;s Encrypt\u63d0\u4f9b\u514d\u8d39\u7684SSL\u8bc1\u4e66\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo certbot certonly --standalone -d yourdomain.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u8bc1\u4e66\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><h4>5.1 \u9a8c\u8bc1SSL\u8bc1\u4e66<\/h4>\n<\/p>\n<p><p>\u9a8c\u8bc1\u8bc1\u4e66\u786e\u4fdd\u60a8\u8fde\u63a5\u7684\u662f\u9884\u671f\u7684\u670d\u52a1\u5668\u3002Python\u7684<code>ssl<\/code>\u6a21\u5757\u548c\u5927\u591a\u6570HTTP\u5e93\u9ed8\u8ba4\u542f\u7528\u8bc1\u4e66\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with context.wrap_socket(sock, server_hostname=hostname) as ssock:<\/p>\n<p>        print(ssock.getpeercert())<\/p>\n<p>except ssl.SSLError as e:<\/p>\n<p>    print(f&quot;SSL error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u5904\u7406\u8bc1\u4e66\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u81ea\u7b7e\u540d\u8bc1\u4e66\u6216\u5176\u4ed6\u8bc1\u4e66\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from requests.exceptions import SSLError<\/p>\n<p>url = &#39;https:\/\/self-signed.badssl.com\/&#39;<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>except SSLError as e:<\/p>\n<p>    print(f&quot;SSL error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u6dfb\u52a0\u548c\u7ba1\u7406SSL\uff0c\u4f7f\u60a8\u7684\u7f51\u7edc\u901a\u4fe1\u66f4\u52a0\u5b89\u5168\u3002\u786e\u4fdd\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u59cb\u7ec8\u4f7f\u7528\u6709\u6548\u548c\u53d7\u4fe1\u4efb\u7684\u8bc1\u4e66\uff0c\u5e76\u5b9a\u671f\u66f4\u65b0\u548c\u68c0\u67e5\u60a8\u7684SSL\u914d\u7f6e\u4ee5\u9632\u6b62\u6f0f\u6d1e\u548c\u653b\u51fb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u914d\u7f6eSSL\u8bc1\u4e66\u4ee5\u786e\u4fdd\u6570\u636e\u5b89\u5168\uff1f<\/strong><br \/>\u5728Python\u4e2d\u914d\u7f6eSSL\u8bc1\u4e66\u53ef\u4ee5\u901a\u8fc7<code>ssl<\/code>\u6a21\u5757\u5b8c\u6210\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u6709\u5408\u9002\u7684SSL\u8bc1\u4e66\u548c\u79c1\u94a5\u6587\u4ef6\u3002\u4f7f\u7528<code>ssl.create_default_context()<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aSSL\u4e0a\u4e0b\u6587\uff0c\u7136\u540e\u901a\u8fc7<code>load_cert_chain()<\/code>\u65b9\u6cd5\u52a0\u8f7d\u8bc1\u4e66\u548c\u79c1\u94a5\u3002\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u6240\u6709\u901a\u8fc7\u8be5\u4e0a\u4e0b\u6587\u7684\u8fde\u63a5\u90fd\u4f1a\u4f7f\u7528SSL\u8fdb\u884c\u52a0\u5bc6\uff0c\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Flask\u6216Django\u65f6\uff0c\u5982\u4f55\u542f\u7528SSL\uff1f<\/strong><br \/>\u82e5\u5728Flask\u6216Django\u6846\u67b6\u4e2d\u542f\u7528SSL\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9aSSL\u8bc1\u4e66\u548c\u79c1\u94a5\u6587\u4ef6\u6765\u5b9e\u73b0\u3002\u5728Flask\u4e2d\uff0c\u53ef\u4ee5\u5728<code>app.run()<\/code>\u65b9\u6cd5\u4e2d\u6dfb\u52a0<code>ssl_context<\/code>\u53c2\u6570\uff0c\u4f8b\u5982<code>ssl_context=(&#39;cert.pem&#39;, &#39;key.pem&#39;)<\/code>\u3002\u5728Django\u4e2d\uff0c\u5219\u9700\u8981\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528<code>gunicorn<\/code>\u6216<code>uwsgi<\/code>\u7b49WSGI\u670d\u52a1\u5668\uff0c\u914d\u7f6e\u76f8\u5e94\u7684SSL\u53c2\u6570\uff0c\u786e\u4fddHTTPS\u534f\u8bae\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u5904\u7406SSL\u8bc1\u4e66\u9a8c\u8bc1\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406SSL\u8bc1\u4e66\u9a8c\u8bc1\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7<code>ssl.SSLError<\/code>\u5f02\u5e38\u6765\u5b9e\u73b0\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u662f\u5426\u8981\u5ffd\u7565SSL\u9a8c\u8bc1\u9519\u8bef\uff0c\u5c3d\u7ba1\u8fd9\u5e76\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b89\u5168\u98ce\u9669\u3002\u4f7f\u7528<code>ssl._create_unverified_context()<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5ffd\u7565\u8bc1\u4e66\u9a8c\u8bc1\u7684\u4e0a\u4e0b\u6587\uff0c\u4f46\u5efa\u8bae\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u59cb\u7ec8\u4f7f\u7528\u6709\u6548\u7684SSL\u8bc1\u4e66\u4ee5\u4fdd\u969c\u5b89\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6dfb\u52a0SSL\u6709\u591a\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684ssl\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982requests\u3001urllib [&hellip;]","protected":false},"author":3,"featured_media":978000,"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\/977990"}],"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=977990"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977990\/revisions"}],"predecessor-version":[{"id":978003,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977990\/revisions\/978003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/978000"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}