{"id":204478,"date":"2025-05-29T11:35:31","date_gmt":"2025-05-29T03:35:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204478\/"},"modified":"2025-05-29T11:35:31","modified_gmt":"2025-05-29T03:35:31","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e5%88%a4%e6%96%ad%e7%bb%99%e5%ae%9a%e7%9a%84%e5%9f%9f%e5%90%8d%e6%98%af%e4%bd%bf%e7%94%a8http%e8%bf%98%e6%98%afhttps%e5%8d%8f%e8%ae%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204478\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u5224\u65ad\u7ed9\u5b9a\u7684\u57df\u540d\u662f\u4f7f\u7528HTTP\u8fd8\u662fHTTPS\u534f\u8bae\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528Python\u5224\u65ad\u7ed9\u5b9a\u7684\u57df\u540d\u662f\u4f7f\u7528HTTP\u8fd8\u662fHTTPS\u534f\u8bae\uff1f<\/h1>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731063630672def4e374cb.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u62fc\u63a5\u57df\u540d\u5224\u65adhttp\u8fd8\u662fhttps<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528python\u5224\u65ad\u7ed9\u5b9a\u7684\u57df\u540d\u662f\u4f7f\u7528http\u8fd8\u662fhttps\u534f\u8bae\uff1f\u6211\u4eec\u9700\u8981\u5c06\u57df\u540d\u62fc\u63a5\u6210\u5b8c\u6574\u7684url\uff08\u4f8b\u5982\uff1ahttp:\/\/www.baidu.com\uff09\uff0c\u4f46\u6211\u4eec\u4e0d\u77e5\u9053\u57df\u540d\u7684\u534f\u8bae\u7c7b\u578b\u3002<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u5bf9\u4e8e\u516c\u5171\u7f51\u7ad9\uff0c\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u540c\u7aef\u53e3\u6765\u8bc6\u522b\u534f\u8bae\u3002http\u534f\u8bae\u901a\u5e38\u4f7f\u7528\u7aef\u53e380\uff0c\u800chttps\u534f\u8bae\u901a\u5e38\u4f7f\u7528\u7aef\u53e3443\u3002<\/p>\n<pre>import socket\n\ndef get_protocol(domain):\n    \"\"\"\n    \u5224\u65ad\u57df\u540d\u4f7f\u7528\u7684\u534f\u8bae\uff08http\u6216https\uff09\n\n    args:\n        domain (str): \u9700\u8981\u5224\u5b9a\u7684\u57df\u540d\n\n    returns:\n        str: \u8bc6\u522b\u51fa\u7684\u534f\u8bae\uff08\"http\"\u6216\"https\"\uff09\n    \"\"\"\n\n    try:\n        # \u5c1d\u8bd5\u8fde\u63a5\u5230http\u7aef\u53e3\n        sock = socket.socket(socket.af_inet, socket.sock_stream)\n        sock.settimeout(1)\n        sock.connect((domain, 80))\n        sock.close()\n        return \"http\"\n    except socket.error:\n        pass\n\n    try:\n        # \u5c1d\u8bd5\u8fde\u63a5\u5230https\u7aef\u53e3\n        sock = socket.socket(socket.af_inet, socket.sock_stream)\n        sock.settimeout(1)\n        sock.connect((domain, 443))\n        sock.close()\n        return \"https\"\n    except socket.error:\n        pass\n\n    # \u5747\u672a\u8fde\u63a5\u6210\u529f\uff0c\u5219\u65e0\u6cd5\u8bc6\u522b\u534f\u8bae\n    return none<\/pre>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>domain = \"www.baidu.com\"\nprotocol = get_protocol(domain)\n\nif protocol == \"http\":\n    url = \"http:\/\/\" + domain\nelif protocol == \"https\":\n    url = \"https:\/\/\" + domain\nelse:\n    # \u65e0\u6cd5\u8bc6\u522b\u534f\u8bae\uff0c\u9700\u8981\u4eba\u5de5\u5224\u65ad\n    print(\"\u65e0\u6cd5\u8bc6\u522b\u534f\u8bae\uff0c\u8bf7\u624b\u52a8\u6307\u5b9a\uff1a\")\n    url = input()<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528Python\u5224\u65ad\u7ed9\u5b9a\u7684\u57df\u540d\u662f\u4f7f\u7528HTTP\u8fd8\u662fHTTPS\u534f\u8bae\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528Python\u5224\u65ad\u7ed9\u5b9a\u7684\u57df&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204478","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204478"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204478\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}