{"id":10021227,"date":"2026-02-01T02:39:50","date_gmt":"2026-01-31T18:39:50","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021227"},"modified":"2026-02-01T02:39:50","modified_gmt":"2026-01-31T18:39:50","slug":"python%e4%b8%ad%e7%9a%84%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%a8%a1%e5%9e%8b%e4%b8%8e%e5%a5%97%e6%8e%a5%e5%ad%97api","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021227","title":{"rendered":"Python\u4e2d\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u4e0e\u5957\u63a5\u5b57API"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u4e0e\u5957\u63a5\u5b57API<\/h2>\n<h3>1. \u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u7f51\u7edc\u7f16\u7a0b\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7528\u4e8e\u5b9e\u73b0\u8ba1\u7b97\u673a\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u7406\u89e3\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u662f\u638c\u63e1Python\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u3002<\/p>\n<h4>1.1 \u7f51\u7edc\u534f\u8bae<\/h4>\n<p>\u7f51\u7edc\u534f\u8bae\u662f\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u800c\u5efa\u7acb\u7684\u89c4\u5219\u3001\u6807\u51c6\u6216\u7ea6\u5b9a\u7684\u96c6\u5408\u3002\u5e38\u89c1\u7684\u7f51\u7edc\u534f\u8bae\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>TCP\/IP\u534f\u8bae\u65cf<\/strong>\uff1aInternet\u7684\u57fa\u7840\u534f\u8bae\uff0c\u5305\u62ecTCP\u3001UDP\u3001IP\u7b49<\/li>\n<li><strong>HTTP\/HTTPS<\/strong>\uff1a\u5e94\u7528\u5c42\u534f\u8bae\uff0c\u7528\u4e8eWeb\u901a\u4fe1<\/li>\n<li><strong>FTP<\/strong>\uff1a\u6587\u4ef6\u4f20\u8f93\u534f\u8bae<\/li>\n<li><strong>SMTP\/POP3\/IMAP<\/strong>\uff1a\u7535\u5b50\u90ae\u4ef6\u534f\u8bae<\/li>\n<li><strong>DNS<\/strong>\uff1a\u57df\u540d\u7cfb\u7edf\u534f\u8bae<\/li>\n<\/ul>\n<h4>1.2 \u7f51\u7edc\u6a21\u578b<\/h4>\n<p>\u7f51\u7edc\u6a21\u578b\u662f\u5bf9\u7f51\u7edc\u534f\u8bae\u7684\u5206\u5c42\u63cf\u8ff0\uff0c\u5e38\u89c1\u7684\u7f51\u7edc\u6a21\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>OSI\u4e03\u5c42\u6a21\u578b<\/strong>\uff1a\u7269\u7406\u5c42\u3001\u6570\u636e\u94fe\u8def\u5c42\u3001\u7f51\u7edc\u5c42\u3001\u4f20\u8f93\u5c42\u3001\u4f1a\u8bdd\u5c42\u3001\u8868\u793a\u5c42\u3001\u5e94\u7528\u5c42<\/li>\n<li><strong>TCP\/IP\u56db\u5c42\u6a21\u578b<\/strong>\uff1a\u7f51\u7edc\u63a5\u53e3\u5c42\u3001\u7f51\u7edc\u5c42\u3001\u4f20\u8f93\u5c42\u3001\u5e94\u7528\u5c42<\/li>\n<\/ul>\n<h4>1.3 \u5957\u63a5\u5b57<\/h4>\n<p>\u5957\u63a5\u5b57\uff08Socket\uff09\u662f\u7f51\u7edc\u901a\u4fe1\u7684\u7aef\u70b9\uff0c\u662f\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u7840\u3002\u5957\u63a5\u5b57\u53ef\u4ee5\u5206\u4e3a\uff1a<\/p>\n<ul>\n<li><strong>\u6d41\u5f0f\u5957\u63a5\u5b57\uff08SOCK_STREAM\uff09<\/strong>\uff1a\u57fa\u4e8eTCP\u534f\u8bae\uff0c\u63d0\u4f9b\u53ef\u9760\u7684\u3001\u9762\u5411\u8fde\u63a5\u7684\u901a\u4fe1<\/li>\n<li><strong>\u6570\u636e\u62a5\u5957\u63a5\u5b57\uff08SOCK_DGRAM\uff09<\/strong>\uff1a\u57fa\u4e8eUDP\u534f\u8bae\uff0c\u63d0\u4f9b\u4e0d\u53ef\u9760\u7684\u3001\u65e0\u8fde\u63a5\u7684\u901a\u4fe1<\/li>\n<li><strong>\u539f\u59cb\u5957\u63a5\u5b57\uff08SOCK_RAW\uff09<\/strong>\uff1a\u76f4\u63a5\u8bbf\u95ee\u7f51\u7edc\u5c42\u534f\u8bae\uff0c\u7528\u4e8e\u7279\u6b8a\u7528\u9014<\/li>\n<\/ul>\n<h4>1.4 \u7f51\u7edc\u5730\u5740<\/h4>\n<p>\u7f51\u7edc\u5730\u5740\u7528\u4e8e\u6807\u8bc6\u7f51\u7edc\u4e2d\u7684\u8bbe\u5907\uff0c\u5e38\u89c1\u7684\u7f51\u7edc\u5730\u5740\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>IPv4\u5730\u5740<\/strong>\uff1a32\u4f4d\u5730\u5740\uff0c\u683c\u5f0f\u4e3a\u70b9\u5206\u5341\u8fdb\u5236\uff08\u5982192.168.1.1\uff09<\/li>\n<li><strong>IPv6\u5730\u5740<\/strong>\uff1a128\u4f4d\u5730\u5740\uff0c\u683c\u5f0f\u4e3a\u5341\u516d\u8fdb\u5236\uff08\u59822001:0db8:85a3:0000:0000:8a2e:0370:7334\uff09<\/li>\n<li><strong>\u7aef\u53e3\u53f7<\/strong>\uff1a16\u4f4d\u6574\u6570\uff0c\u7528\u4e8e\u6807\u8bc6\u5e94\u7528\u7a0b\u5e8f\uff08\u598280\u7aef\u53e3\u7528\u4e8eHTTP\uff09<\/li>\n<\/ul>\n<h4>1.5 \u7f51\u7edc\u7f16\u7a0b\u6a21\u578b<\/h4>\n<p>\u5e38\u89c1\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u6a21\u578b<\/strong>\uff1a\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u54cd\u5e94\u8bf7\u6c42<\/li>\n<li><strong>\u5bf9\u7b49\u6a21\u578b\uff08P2P\uff09<\/strong>\uff1a\u7f51\u7edc\u4e2d\u7684\u8282\u70b9\u65e2\u662f\u5ba2\u6237\u7aef\u53c8\u662f\u670d\u52a1\u5668<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u793a\u4f8b\n\nimport socket\nimport sys\n\n# 1. \u67e5\u770bPython\u652f\u6301\u7684\u5957\u63a5\u5b57\u7c7b\u578b\nprint(&quot;Python\u652f\u6301\u7684\u5957\u63a5\u5b57\u7c7b\u578b\uff1a&quot;)\nprint(f&quot;\u6d41\u5f0f\u5957\u63a5\u5b57\uff08TCP\uff09: {socket.SOCK_STREAM}&quot;)\nprint(f&quot;\u6570\u636e\u62a5\u5957\u63a5\u5b57\uff08UDP\uff09: {socket.SOCK_DGRAM}&quot;)\nprint(f&quot;\u539f\u59cb\u5957\u63a5\u5b57: {socket.SOCK_RAW}&quot;)\n\n# 2. \u67e5\u770b\u672c\u5730\u4e3b\u673a\u540d\u548cIP\u5730\u5740\nprint(&quot;\/n\u672c\u5730\u4e3b\u673a\u4fe1\u606f\uff1a&quot;)\ntry:\n    hostname = socket.gethostname()\n    print(f&quot;\u4e3b\u673a\u540d: {hostname}&quot;)\n    \n    # \u83b7\u53d6IPv4\u5730\u5740\n    ipv4_addresses = socket.gethostbyname_ex(hostname)[2]\n    print(&quot;IPv4\u5730\u5740:&quot;)\n    for ip in ipv4_addresses:\n        print(f&quot;  {ip}&quot;)\n    \n    # \u83b7\u53d6IPv6\u5730\u5740\uff08\u5982\u679c\u652f\u6301\uff09\n    try:\n        ipv6_addresses = socket.getaddrinfo(hostname, None, socket.AF_INET6)\n        print(&quot;IPv6\u5730\u5740:&quot;)\n        for info in ipv6_addresses:\n            print(f&quot;  {info[4][0]}&quot;)\n    except socket.gaierror:\n        print(&quot;IPv6\u5730\u5740: \u4e0d\u652f\u6301&quot;)\nexcept Exception as e:\n    print(f&quot;\u83b7\u53d6\u4e3b\u673a\u4fe1\u606f\u5931\u8d25: {e}&quot;)\n\n# 3. \u6d4b\u8bd5\u7f51\u7edc\u8fde\u63a5\nprint(&quot;\/n\u6d4b\u8bd5\u7f51\u7edc\u8fde\u63a5\uff1a&quot;)\ndef test_connection(host, port):\n    &quot;&quot;&quot;\u6d4b\u8bd5\u7f51\u7edc\u8fde\u63a5&quot;&quot;&quot;\n    try:\n        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n            s.settimeout(2)\n            result = s.connect_ex((host, port))\n            if result == 0:\n                print(f&quot;\u8fde\u63a5 {host}:{port} \u6210\u529f&quot;)\n            else:\n                print(f&quot;\u8fde\u63a5 {host}:{port} \u5931\u8d25: {result}&quot;)\n    except Exception as e:\n        print(f&quot;\u6d4b\u8bd5\u8fde\u63a5\u5931\u8d25: {e}&quot;)\n\n# \u6d4b\u8bd5\u5e38\u89c1\u670d\u52a1\n test_connection(&quot;www.baidu.com&quot;, 80)  # HTTP\ntest_connection(&quot;smtp.163.com&quot;, 25)    # SMTP\ntest_connection(&quot;pop.163.com&quot;, 110)     # POP3\n\n# 4. \u89e3\u6790URL\nprint(&quot;\/n\u89e3\u6790URL\uff1a&quot;)\ndef parse_url(url):\n    &quot;&quot;&quot;\u89e3\u6790URL&quot;&quot;&quot;\n    from urllib.parse import urlparse\n    parsed = urlparse(url)\n    print(f&quot;URL: {url}&quot;)\n    print(f&quot;\u534f\u8bae: {parsed.scheme}&quot;)\n    print(f&quot;\u4e3b\u673a: {parsed.netloc}&quot;)\n    print(f&quot;\u8def\u5f84: {parsed.path}&quot;)\n    print(f&quot;\u67e5\u8be2: {parsed.query}&quot;)\n\nparse_url(&quot;https:\/\/www.python.org\/downloads\/?ref=sidebar&quot;)\n\n# 5. \u67e5\u770b\u7aef\u53e3\u4f7f\u7528\u60c5\u51b5\nprint(&quot;\/n\u67e5\u770b\u7aef\u53e3\u4f7f\u7528\u60c5\u51b5\uff1a&quot;)\ntry:\n    # \u5c1d\u8bd5\u7ed1\u5b9a\u7aef\u53e38080\n    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n        try:\n            s.bind((&quot;localhost&quot;, 8080))\n            print(&quot;\u7aef\u53e38080\u53ef\u7528&quot;)\n        except OSError as e:\n            print(f&quot;\u7aef\u53e38080\u4e0d\u53ef\u7528: {e}&quot;)\nexcept Exception as e:\n    print(f&quot;\u67e5\u770b\u7aef\u53e3\u5931\u8d25: {e}&quot;)<\/code><\/pre>\n<h3>2. \u5957\u63a5\u5b57API\u7684\u4f7f\u7528<\/h3>\n<p>Python\u7684<code>socket<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5957\u63a5\u5b57API\uff0c\u7528\u4e8e\u5b9e\u73b0\u7f51\u7edc\u7f16\u7a0b\u3002\u7406\u89e3\u5957\u63a5\u5b57API\u7684\u4f7f\u7528\u662f\u638c\u63e1Python\u7f51\u7edc\u7f16\u7a0b\u7684\u5173\u952e\u3002<\/p>\n<h4>2.1 \u521b\u5efa\u5957\u63a5\u5b57<\/h4>\n<p>\u4f7f\u7528<code>socket.socket()<\/code>\u51fd\u6570\u521b\u5efa\u5957\u63a5\u5b57\uff1a<\/p>\n<pre><code class=\"python\"># \u521b\u5efaIPv4\u3001TCP\u5957\u63a5\u5b57\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# \u521b\u5efaIPv6\u3001TCP\u5957\u63a5\u5b57\ns = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)\n\n# \u521b\u5efaIPv4\u3001UDP\u5957\u63a5\u5b57\ns = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<\/code><\/pre>\n<h4>2.2 \u5957\u63a5\u5b57\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<p>\u5957\u63a5\u5b57\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u7ed1\u5b9a\u5730\u5740<\/strong>\uff1a<code>bind(address)<\/code><\/li>\n<li><strong>\u76d1\u542c\u8fde\u63a5<\/strong>\uff1a<code>listen(backlog)<\/code><\/li>\n<li><strong>\u63a5\u53d7\u8fde\u63a5<\/strong>\uff1a<code>accept()<\/code><\/li>\n<li><strong>\u53d1\u8d77\u8fde\u63a5<\/strong>\uff1a<code>connect(address)<\/code><\/li>\n<li><strong>\u53d1\u9001\u6570\u636e<\/strong>\uff1a<code>send(data)<\/code>\u3001<code>sendall(data)<\/code><\/li>\n<li><strong>\u63a5\u6536\u6570\u636e<\/strong>\uff1a<code>recv(bufsize)<\/code><\/li>\n<li><strong>\u5173\u95ed\u8fde\u63a5<\/strong>\uff1a<code>close()<\/code><\/li>\n<\/ul>\n<h4>2.3 TCP\u670d\u52a1\u5668<\/h4>\n<p>TCP\u670d\u52a1\u5668\u7684\u57fa\u672c\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u5957\u63a5\u5b57<\/li>\n<li>\u7ed1\u5b9a\u5730\u5740<\/li>\n<li>\u76d1\u542c\u8fde\u63a5<\/li>\n<li>\u63a5\u53d7\u8fde\u63a5<\/li>\n<li>\u6536\u53d1\u6570\u636e<\/li>\n<li>\u5173\u95ed\u8fde\u63a5<\/li>\n<\/ol>\n<h4>2.4 TCP\u5ba2\u6237\u7aef<\/h4>\n<p>TCP\u5ba2\u6237\u7aef\u7684\u57fa\u672c\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u5957\u63a5\u5b57<\/li>\n<li>\u8fde\u63a5\u670d\u52a1\u5668<\/li>\n<li>\u6536\u53d1\u6570\u636e<\/li>\n<li>\u5173\u95ed\u8fde\u63a5<\/li>\n<\/ol>\n<h4>2.5 UDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef<\/h4>\n<p>UDP\u662f\u65e0\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u6240\u4ee5UDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u6d41\u7a0b\u6bd4TCP\u7b80\u5355\uff1a<\/p>\n<ul>\n<li><strong>UDP\u670d\u52a1\u5668<\/strong>\uff1a\u521b\u5efa\u5957\u63a5\u5b57 \u2192 \u7ed1\u5b9a\u5730\u5740 \u2192 \u6536\u53d1\u6570\u636e \u2192 \u5173\u95ed\u8fde\u63a5<\/li>\n<li><strong>UDP\u5ba2\u6237\u7aef<\/strong>\uff1a\u521b\u5efa\u5957\u63a5\u5b57 \u2192 \u6536\u53d1\u6570\u636e \u2192 \u5173\u95ed\u8fde\u63a5<\/li>\n<\/ul>\n<h4>2.6 \u5957\u63a5\u5b57\u9009\u9879<\/h4>\n<p>\u5957\u63a5\u5b57\u9009\u9879\u7528\u4e8e\u914d\u7f6e\u5957\u63a5\u5b57\u7684\u884c\u4e3a\uff0c\u5e38\u89c1\u7684\u5957\u63a5\u5b57\u9009\u9879\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>SO_REUSEADDR<\/strong>\uff1a\u5141\u8bb8\u91cd\u7528\u5730\u5740<\/li>\n<li><strong>SO_RCVBUF<\/strong>\uff1a\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>SO_SNDBUF<\/strong>\uff1a\u53d1\u9001\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>SO_TIMEOUT<\/strong>\uff1a\u8d85\u65f6\u65f6\u95f4<\/li>\n<\/ul>\n<h4>2.7 \u5957\u63a5\u5b57API\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u5957\u63a5\u5b57API\u7684\u4e00\u4e9b\u5e38\u89c1\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u5957\u63a5\u5b57API\u7684\u4f7f\u7528\u793a\u4f8b\n\nimport socket\nimport sys\n\n# 1. TCP\u670d\u52a1\u5668\u793a\u4f8b\nprint(&quot;TCP\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\n\ndef tcp_server(host='localhost', port=8888):\n    &quot;&quot;&quot;TCP\u670d\u52a1\u5668&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        print(&quot;\u521b\u5efa\u5957\u63a5\u5b57\u6210\u529f&quot;)\n        \n        # \u8bbe\u7f6e\u5957\u63a5\u5b57\u9009\u9879\n        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        print(&quot;\u8bbe\u7f6e\u5957\u63a5\u5b57\u9009\u9879\u6210\u529f&quot;)\n        \n        # \u7ed1\u5b9a\u5730\u5740\n        server_socket.bind((host, port))\n        print(f&quot;\u7ed1\u5b9a\u5730\u5740 {host}:{port} \u6210\u529f&quot;)\n        \n        # \u76d1\u542c\u8fde\u63a5\n        server_socket.listen(5)\n        print(f&quot;\u76d1\u542c\u7aef\u53e3 {port} \u6210\u529f&quot;)\n        \n        print(&quot;\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5...&quot;)\n        \n        # \u63a5\u53d7\u8fde\u63a5\n        client_socket, client_address = server_socket.accept()\n        print(f&quot;\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5: {client_address}&quot;)\n        \n        # \u6536\u53d1\u6570\u636e\n        while True:\n            # \u63a5\u6536\u6570\u636e\n            data = client_socket.recv(1024)\n            if not data:\n                break\n            print(f&quot;\u6536\u5230\u5ba2\u6237\u7aef\u6570\u636e: {data.decode('utf-8')}&quot;)\n            \n            # \u53d1\u9001\u6570\u636e\n            response = f&quot;\u670d\u52a1\u5668\u6536\u5230: {data.decode('utf-8')}&quot;\n            client_socket.sendall(response.encode('utf-8'))\n            print(f&quot;\u53d1\u9001\u6570\u636e\u5230\u5ba2\u6237\u7aef: {response}&quot;)\n        \n        # \u5173\u95ed\u8fde\u63a5\n        client_socket.close()\n        server_socket.close()\n        print(&quot;\u670d\u52a1\u5668\u5173\u95ed&quot;)\n        \n    except Exception as e:\n        print(f&quot;\u670d\u52a1\u5668\u9519\u8bef: {e}&quot;)\n        if 'server_socket' in locals():\n            server_socket.close()\n\n# 2. TCP\u5ba2\u6237\u7aef\u793a\u4f8b\nprint(&quot;\/nTCP\u5ba2\u6237\u7aef\u793a\u4f8b\uff1a&quot;)\n\ndef tcp_client(host='localhost', port=8888):\n    &quot;&quot;&quot;TCP\u5ba2\u6237\u7aef&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        print(&quot;\u521b\u5efa\u5957\u63a5\u5b57\u6210\u529f&quot;)\n        \n        # \u8fde\u63a5\u670d\u52a1\u5668\n        client_socket.connect((host, port))\n        print(f&quot;\u8fde\u63a5\u670d\u52a1\u5668 {host}:{port} \u6210\u529f&quot;)\n        \n        # \u6536\u53d1\u6570\u636e\n        while True:\n            # \u8f93\u5165\u6570\u636e\n            message = input(&quot;\u8bf7\u8f93\u5165\u8981\u53d1\u9001\u7684\u6570\u636e\uff08\u8f93\u5165exit\u9000\u51fa\uff09: &quot;)\n            if message == &quot;exit&quot;:\n                break\n            \n            # \u53d1\u9001\u6570\u636e\n            client_socket.sendall(message.encode('utf-8'))\n            print(f&quot;\u53d1\u9001\u6570\u636e\u5230\u670d\u52a1\u5668: {message}&quot;)\n            \n            # \u63a5\u6536\u6570\u636e\n            data = client_socket.recv(1024)\n            print(f&quot;\u6536\u5230\u670d\u52a1\u5668\u6570\u636e: {data.decode('utf-8')}&quot;)\n        \n        # \u5173\u95ed\u8fde\u63a5\n        client_socket.close()\n        print(&quot;\u5ba2\u6237\u7aef\u5173\u95ed&quot;)\n        \n    except Exception as e:\n        print(f&quot;\u5ba2\u6237\u7aef\u9519\u8bef: {e}&quot;)\n        if 'client_socket' in locals():\n            client_socket.close()\n\n# 3. UDP\u670d\u52a1\u5668\u793a\u4f8b\nprint(&quot;\/nUDP\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\n\ndef udp_server(host='localhost', port=8888):\n    &quot;&quot;&quot;UDP\u670d\u52a1\u5668&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n        print(&quot;\u521b\u5efa\u5957\u63a5\u5b57\u6210\u529f&quot;)\n        \n        # \u7ed1\u5b9a\u5730\u5740\n        server_socket.bind((host, port))\n        print(f&quot;\u7ed1\u5b9a\u5730\u5740 {host}:{port} \u6210\u529f&quot;)\n        \n        print(&quot;UDP\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u6570\u636e...&quot;)\n        \n        # \u6536\u53d1\u6570\u636e\n        while True:\n            # \u63a5\u6536\u6570\u636e\n            data, client_address = server_socket.recvfrom(1024)\n            print(f&quot;\u6536\u5230\u5ba2\u6237\u7aef {client_address} \u7684\u6570\u636e: {data.decode('utf-8')}&quot;)\n            \n            # \u53d1\u9001\u6570\u636e\n            response = f&quot;\u670d\u52a1\u5668\u6536\u5230: {data.decode('utf-8')}&quot;\n            server_socket.sendto(response.encode('utf-8'), client_address)\n            print(f&quot;\u53d1\u9001\u6570\u636e\u5230\u5ba2\u6237\u7aef {client_address}: {response}&quot;)\n        \n        # \u5173\u95ed\u8fde\u63a5\n        server_socket.close()\n        print(&quot;\u670d\u52a1\u5668\u5173\u95ed&quot;)\n        \n    except Exception as e:\n        print(f&quot;\u670d\u52a1\u5668\u9519\u8bef: {e}&quot;)\n        if 'server_socket' in locals():\n            server_socket.close()\n\n# 4. UDP\u5ba2\u6237\u7aef\u793a\u4f8b\nprint(&quot;\/nUDP\u5ba2\u6237\u7aef\u793a\u4f8b\uff1a&quot;)\n\ndef udp_client(host='localhost', port=8888):\n    &quot;&quot;&quot;UDP\u5ba2\u6237\u7aef&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n        print(&quot;\u521b\u5efa\u5957\u63a5\u5b57\u6210\u529f&quot;)\n        \n        # \u6536\u53d1\u6570\u636e\n        while True:\n            # \u8f93\u5165\u6570\u636e\n            message = input(&quot;\u8bf7\u8f93\u5165\u8981\u53d1\u9001\u7684\u6570\u636e\uff08\u8f93\u5165exit\u9000\u51fa\uff09: &quot;)\n            if message == &quot;exit&quot;:\n                break\n            \n            # \u53d1\u9001\u6570\u636e\n            client_socket.sendto(message.encode('utf-8'), (host, port))\n            print(f&quot;\u53d1\u9001\u6570\u636e\u5230\u670d\u52a1\u5668 {host}:{port}: {message}&quot;)\n            \n            # \u63a5\u6536\u6570\u636e\n            data, server_address = client_socket.recvfrom(1024)\n            print(f&quot;\u6536\u5230\u670d\u52a1\u5668 {server_address} \u7684\u6570\u636e: {data.decode('utf-8')}&quot;)\n        \n        # \u5173\u95ed\u8fde\u63a5\n        client_socket.close()\n        print(&quot;\u5ba2\u6237\u7aef\u5173\u95ed&quot;)\n        \n    except Exception as e:\n        print(f&quot;\u5ba2\u6237\u7aef\u9519\u8bef: {e}&quot;)\n        if 'client_socket' in locals():\n            client_socket.close()\n\n# 5. \u5957\u63a5\u5b57\u9009\u9879\u793a\u4f8b\nprint(&quot;\/n\u5957\u63a5\u5b57\u9009\u9879\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    # \u521b\u5efa\u5957\u63a5\u5b57\n    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    print(&quot;\u521b\u5efa\u5957\u63a5\u5b57\u6210\u529f&quot;)\n    \n    # \u83b7\u53d6\u5957\u63a5\u5b57\u9009\u9879\n    reuse_addr = s.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)\n    print(f&quot;SO_REUSEADDR: {reuse_addr}&quot;)\n    \n    rcvbuf = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)\n    print(f&quot;SO_RCVBUF: {rcvbuf} \u5b57\u8282&quot;)\n    \n    sndbuf = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)\n    print(f&quot;SO_SNDBUF: {sndbuf} \u5b57\u8282&quot;)\n    \n    # \u8bbe\u7f6e\u5957\u63a5\u5b57\u9009\u9879\n    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n    print(&quot;\u8bbe\u7f6eSO_REUSEADDR=1\u6210\u529f&quot;)\n    \n    # \u8bbe\u7f6e\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f\n    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8192)\n    print(&quot;\u8bbe\u7f6eSO_RCVBUF=8192\u6210\u529f&quot;)\n    \n    # \u518d\u6b21\u83b7\u53d6\u5957\u63a5\u5b57\u9009\u9879\n    reuse_addr = s.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)\n    print(f&quot;\u4fee\u6539\u540e SO_REUSEADDR: {reuse_addr}&quot;)\n    \n    rcvbuf = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)\n    print(f&quot;\u4fee\u6539\u540e SO_RCVBUF: {rcvbuf} \u5b57\u8282&quot;)\n    \n    # \u5173\u95ed\u5957\u63a5\u5b57\n    s.close()\n    print(&quot;\u5173\u95ed\u5957\u63a5\u5b57\u6210\u529f&quot;)\n    \nexcept Exception as e:\n    print(f&quot;\u5957\u63a5\u5b57\u9009\u9879\u64cd\u4f5c\u9519\u8bef: {e}&quot;)\n    if 's' in locals():\n        s.close()\n\nprint(&quot;\/n\u6ce8\u610f\uff1a\u4ee5\u4e0a\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u793a\u4f8b\u9700\u8981\u5206\u522b\u8fd0\u884c\uff0c\u5148\u542f\u52a8\u670d\u52a1\u5668\uff0c\u518d\u542f\u52a8\u5ba2\u6237\u7aef&quot;)<\/code><\/pre>\n<h3>3. \u7f51\u7edc\u7f16\u7a0b\u6a21\u578b<\/h3>\n<p>Python\u652f\u6301\u591a\u79cd\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\uff0c\u6bcf\u79cd\u6a21\u578b\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u3002\u7406\u89e3\u8fd9\u4e9b\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u5bf9\u4e8e\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>3.1 \u963b\u585e\u5f0fI\/O\u6a21\u578b<\/h4>\n<p>\u963b\u585e\u5f0fI\/O\u6a21\u578b\u662f\u6700\u57fa\u672c\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\uff0c\u5b83\u7684\u7279\u70b9\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u963b\u585e<\/strong>\uff1a\u5f53\u6267\u884cI\/O\u64cd\u4f5c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u963b\u585e\u76f4\u5230\u64cd\u4f5c\u5b8c\u6210<\/li>\n<li><strong>\u7b80\u5355<\/strong>\uff1a\u5b9e\u73b0\u7b80\u5355\uff0c\u6613\u4e8e\u7406\u89e3<\/li>\n<li><strong>\u6548\u7387\u4f4e<\/strong>\uff1a\u5728\u5904\u7406\u591a\u4e2a\u8fde\u63a5\u65f6\uff0c\u9700\u8981\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b<\/li>\n<\/ul>\n<h4>3.2 \u975e\u963b\u585e\u5f0fI\/O\u6a21\u578b<\/h4>\n<p>\u975e\u963b\u585e\u5f0fI\/O\u6a21\u578b\u7684\u7279\u70b9\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u975e\u963b\u585e<\/strong>\uff1a\u5f53\u6267\u884cI\/O\u64cd\u4f5c\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u963b\u585e\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56de<\/li>\n<li><strong>\u8f6e\u8be2<\/strong>\uff1a\u9700\u8981\u4e0d\u65ad\u8f6e\u8be2\u68c0\u67e5I\/O\u64cd\u4f5c\u662f\u5426\u5b8c\u6210<\/li>\n<li><strong>CPU\u5bc6\u96c6<\/strong>\uff1a\u8f6e\u8be2\u4f1a\u6d88\u8017\u5927\u91cfCPU\u8d44\u6e90<\/li>\n<\/ul>\n<h4>3.3 \u591a\u8def\u590d\u7528I\/O\u6a21\u578b<\/h4>\n<p>\u591a\u8def\u590d\u7528I\/O\u6a21\u578b\u7684\u7279\u70b9\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u4e8b\u4ef6\u9a71\u52a8<\/strong>\uff1a\u4f7f\u7528select\u3001poll\u3001epoll\u7b49\u7cfb\u7edf\u8c03\u7528\u76d1\u63a7\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8fde\u63a5\uff0c\u800c\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u7ebf\u7a0b\u6216\u8fdb\u7a0b<\/li>\n<li><strong>\u590d\u6742<\/strong>\uff1a\u5b9e\u73b0\u76f8\u5bf9\u590d\u6742<\/li>\n<\/ul>\n<h4>3.4 \u4fe1\u53f7\u9a71\u52a8I\/O\u6a21\u578b<\/h4>\n<p>\u4fe1\u53f7\u9a71\u52a8I\/O\u6a21\u578b\u7684\u7279\u70b9\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u4fe1\u53f7\u901a\u77e5<\/strong>\uff1a\u5f53I\/O\u64cd\u4f5c\u51c6\u5907\u5c31\u7eea\u65f6\uff0c\u7cfb\u7edf\u4f1a\u53d1\u9001\u4fe1\u53f7\u901a\u77e5\u8fdb\u7a0b<\/li>\n<li><strong>\u5f02\u6b65<\/strong>\uff1a\u8fdb\u7a0b\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u76f4\u5230\u6536\u5230\u4fe1\u53f7<\/li>\n<li><strong>\u4e0d\u5e38\u7528<\/strong>\uff1a\u5728Python\u4e2d\u4e0d\u5e38\u7528<\/li>\n<\/ul>\n<h4>3.5 \u5f02\u6b65I\/O\u6a21\u578b<\/h4>\n<p>\u5f02\u6b65I\/O\u6a21\u578b\u7684\u7279\u70b9\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u5b8c\u5168\u5f02\u6b65<\/strong>\uff1a\u5f53\u6267\u884cI\/O\u64cd\u4f5c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u7acb\u5373\u8fd4\u56de\uff0c\u5f53\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u7cfb\u7edf\u4f1a\u901a\u77e5\u8fdb\u7a0b<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u5927\u91cf\u8fde\u63a5<\/li>\n<li><strong>\u590d\u6742<\/strong>\uff1a\u5b9e\u73b0\u76f8\u5bf9\u590d\u6742<\/li>\n<\/ul>\n<h4>3.6 Python\u4e2d\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b<\/h4>\n<p>Python\u652f\u6301\u4ee5\u4e0b\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u591a\u7ebf\u7a0b\u6a21\u578b<\/strong>\uff1a\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b<\/li>\n<li><strong>\u591a\u8fdb\u7a0b\u6a21\u578b<\/strong>\uff1a\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b<\/li>\n<li><strong>I\/O\u591a\u8def\u590d\u7528\u6a21\u578b<\/strong>\uff1a\u4f7f\u7528select\u3001poll\u3001epoll\u7b49\u7cfb\u7edf\u8c03\u7528<\/li>\n<li><strong>\u5f02\u6b65I\/O\u6a21\u578b<\/strong>\uff1a\u4f7f\u7528asyncio\u5e93<\/li>\n<\/ul>\n<h4>3.7 \u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662fPython\u4e2d\u5e38\u89c1\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u793a\u4f8b\n\nimport socket\nimport threading\nimport multiprocessing\nimport select\nimport asyncio\n\n# 1. \u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u793a\u4f8b\nprint(&quot;\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\n\ndef handle_client(client_socket, client_address):\n    &quot;&quot;&quot;\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5&quot;&quot;&quot;\n    print(f&quot;\u65b0\u7ebf\u7a0b\u5904\u7406\u5ba2\u6237\u7aef: {client_address}&quot;)\n    try:\n        while True:\n            # \u63a5\u6536\u6570\u636e\n            data = client_socket.recv(1024)\n            if not data:\n                break\n            print(f&quot;\u6536\u5230\u5ba2\u6237\u7aef {client_address} \u7684\u6570\u636e: {data.decode('utf-8')}&quot;)\n            \n            # \u53d1\u9001\u6570\u636e\n            response = f&quot;\u670d\u52a1\u5668\u6536\u5230: {data.decode('utf-8')}&quot;\n            client_socket.sendall(response.encode('utf-8'))\n    except Exception as e:\n        print(f&quot;\u5904\u7406\u5ba2\u6237\u7aef\u9519\u8bef: {e}&quot;)\n    finally:\n        client_socket.close()\n        print(f&quot;\u5ba2\u6237\u7aef {client_address} \u8fde\u63a5\u5173\u95ed&quot;)\n\ndef threaded_server(host='localhost', port=8888):\n    &quot;&quot;&quot;\u591a\u7ebf\u7a0b\u670d\u52a1\u5668&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        server_socket.bind((host, port))\n        server_socket.listen(5)\n        print(f&quot;\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u76d1\u542c {host}:{port}&quot;)\n        \n        while True:\n            # \u63a5\u53d7\u8fde\u63a5\n            client_socket, client_address = server_socket.accept()\n            print(f&quot;\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5: {client_address}&quot;)\n            \n            # \u521b\u5efa\u7ebf\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\n            client_thread = threading.Thread(\n                target=handle_client, \n                args=(client_socket, client_address)\n            )\n            client_thread.daemon = True\n            client_thread.start()\n            \n    except Exception as e:\n        print(f&quot;\u670d\u52a1\u5668\u9519\u8bef: {e}&quot;)\n    finally:\n        server_socket.close()\n        print(&quot;\u670d\u52a1\u5668\u5173\u95ed&quot;)\n\n# 2. \u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u793a\u4f8b\nprint(&quot;\/n\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\n\ndef process_server(host='localhost', port=8889):\n    &quot;&quot;&quot;\u591a\u8fdb\u7a0b\u670d\u52a1\u5668&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        server_socket.bind((host, port))\n        server_socket.listen(5)\n        print(f&quot;\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u76d1\u542c {host}:{port}&quot;)\n        \n        while True:\n            # \u63a5\u53d7\u8fde\u63a5\n            client_socket, client_address = server_socket.accept()\n            print(f&quot;\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5: {client_address}&quot;)\n            \n            # \u521b\u5efa\u8fdb\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\n            client_process = multiprocessing.Process(\n                target=handle_client, \n                args=(client_socket, client_address)\n            )\n            client_process.daemon = True\n            client_process.start()\n            \n            # \u5173\u95ed\u7236\u8fdb\u7a0b\u4e2d\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\n            client_socket.close()\n            \n    except Exception as e:\n        print(f&quot;\u670d\u52a1\u5668\u9519\u8bef: {e}&quot;)\n    finally:\n        server_socket.close()\n        print(&quot;\u670d\u52a1\u5668\u5173\u95ed&quot;)\n\n# 3. I\/O\u591a\u8def\u590d\u7528\u670d\u52a1\u5668\u793a\u4f8b\nprint(&quot;\/nI\/O\u591a\u8def\u590d\u7528\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\n\ndef multiplex_server(host='localhost', port=8890):\n    &quot;&quot;&quot;I\/O\u591a\u8def\u590d\u7528\u670d\u52a1\u5668&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        server_socket.bind((host, port))\n        server_socket.listen(5)\n        server_socket.setblocking(False)  # \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\n        print(f&quot;I\/O\u591a\u8def\u590d\u7528\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u76d1\u542c {host}:{port}&quot;)\n        \n        # \u521d\u59cb\u5316\u5957\u63a5\u5b57\u5217\u8868\n        sockets = [server_socket]\n        \n        while True:\n            # \u4f7f\u7528select\u76d1\u63a7\u5957\u63a5\u5b57\n            readable, writable, exceptional = select.select(sockets, [], sockets)\n            \n            # \u5904\u7406\u53ef\u8bfb\u5957\u63a5\u5b57\n            for sock in readable:\n                if sock == server_socket:\n                    # \u63a5\u53d7\u65b0\u8fde\u63a5\n                    client_socket, client_address = server_socket.accept()\n                    client_socket.setblocking(False)  # \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\n                    sockets.append(client_socket)\n                    print(f&quot;\u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5: {client_address}&quot;)\n                else:\n                    # \u63a5\u6536\u5ba2\u6237\u7aef\u6570\u636e\n                    try:\n                        data = sock.recv(1024)\n                        if data:\n                            print(f&quot;\u6536\u5230\u5ba2\u6237\u7aef\u6570\u636e: {data.decode('utf-8')}&quot;)\n                            # \u53d1\u9001\u54cd\u5e94\n                            response = f&quot;\u670d\u52a1\u5668\u6536\u5230: {data.decode('utf-8')}&quot;\n                            sock.sendall(response.encode('utf-8'))\n                        else:\n                            # \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5\n                            print(f&quot;\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5&quot;)\n                            sockets.remove(sock)\n                            sock.close()\n                    except Exception as e:\n                        # \u5ba2\u6237\u7aef\u9519\u8bef\n                        print(f&quot;\u5ba2\u6237\u7aef\u9519\u8bef: {e}&quot;)\n                        sockets.remove(sock)\n                        sock.close()\n            \n            # \u5904\u7406\u5f02\u5e38\u5957\u63a5\u5b57\n            for sock in exceptional:\n                print(f&quot;\u5957\u63a5\u5b57\u5f02\u5e38&quot;)\n                sockets.remove(sock)\n                sock.close()\n        \n    except Exception as e:\n        print(f&quot;\u670d\u52a1\u5668\u9519\u8bef: {e}&quot;)\n    finally:\n        server_socket.close()\n        print(&quot;\u670d\u52a1\u5668\u5173\u95ed&quot;)\n\n# 4. \u5f02\u6b65I\/O\u670d\u52a1\u5668\u793a\u4f8b\nprint(&quot;\/n\u5f02\u6b65I\/O\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\n\nasync def handle_async_client(reader, writer):\n    &quot;&quot;&quot;\u5904\u7406\u5f02\u6b65\u5ba2\u6237\u7aef\u8fde\u63a5&quot;&quot;&quot;\n    client_address = writer.get_extra_info('peername')\n    print(f&quot;\u63a5\u53d7\u5f02\u6b65\u5ba2\u6237\u7aef\u8fde\u63a5: {client_address}&quot;)\n    \n    try:\n        while True:\n            # \u63a5\u6536\u6570\u636e\n            data = await reader.read(1024)\n            if not data:\n                break\n            message = data.decode('utf-8')\n            print(f&quot;\u6536\u5230\u5ba2\u6237\u7aef {client_address} \u7684\u6570\u636e: {message}&quot;)\n            \n            # \u53d1\u9001\u6570\u636e\n            response = f&quot;\u670d\u52a1\u5668\u6536\u5230: {message}&quot;\n            writer.write(response.encode('utf-8'))\n            await writer.drain()\n    except Exception as e:\n        print(f&quot;\u5904\u7406\u5ba2\u6237\u7aef\u9519\u8bef: {e}&quot;)\n    finally:\n        print(f&quot;\u5173\u95ed\u5ba2\u6237\u7aef\u8fde\u63a5: {client_address}&quot;)\n        writer.close()\n        await writer.wait_closed()\n\nasync def async_server(host='localhost', port=8891):\n    &quot;&quot;&quot;\u5f02\u6b65I\/O\u670d\u52a1\u5668&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u670d\u52a1\u5668\n        server = await asyncio.start_server(\n            handle_async_client, \n            host, \n            port\n        )\n        \n        # \u83b7\u53d6\u670d\u52a1\u5668\u5730\u5740\n        addr = server.sockets[0].getsockname()\n        print(f&quot;\u5f02\u6b65I\/O\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u76d1\u542c {addr}&quot;)\n        \n        # \u542f\u52a8\u670d\u52a1\u5668\n        async with server:\n            await server.serve_forever()\n            \n    except Exception as e:\n        print(f&quot;\u670d\u52a1\u5668\u9519\u8bef: {e}&quot;)\n\n# 5. \u542f\u52a8\u670d\u52a1\u5668\uff08\u6ce8\u610f\uff1a\u5b9e\u9645\u8fd0\u884c\u65f6\u53ea\u9700\u8981\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u5668\uff09\nprint(&quot;\/n\u542f\u52a8\u670d\u52a1\u5668\u793a\u4f8b\uff1a&quot;)\nprint(&quot;\u6ce8\u610f\uff1a\u4ee5\u4e0b\u4ee3\u7801\u4ec5\u4f5c\u4e3a\u793a\u4f8b\uff0c\u5b9e\u9645\u8fd0\u884c\u65f6\u9700\u8981\u5355\u72ec\u8fd0\u884c\u670d\u52a1\u5668&quot;)\n\n# \u542f\u52a8\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\n# threading.Thread(target=threaded_server, daemon=True).start()\n\n# \u542f\u52a8\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\n# multiprocessing.Process(target=process_server, daemon=True).start()\n\n# \u542f\u52a8I\/O\u591a\u8def\u590d\u7528\u670d\u52a1\u5668\n# threading.Thread(target=multiplex_server, daemon=True).start()\n\n# \u542f\u52a8\u5f02\u6b65I\/O\u670d\u52a1\u5668\n# asyncio.run(async_server())\n\nprint(&quot;\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\u7ed3\u675f&quot;)<\/code><\/pre>\n<h3>4. \u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b<\/h3>\n<p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b\u7684\u5e93\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u7b80\u5316\u7f51\u7edc\u7f16\u7a0b\u7684\u590d\u6742\u6027\u3002<\/p>\n<h4>4.1 \u9ad8\u7ea7\u5957\u63a5\u5b57\u64cd\u4f5c<\/h4>\n<h5>4.1.1 \u5957\u63a5\u5b57\u8d85\u65f6<\/h5>\n<p>\u5957\u63a5\u5b57\u8d85\u65f6\u7528\u4e8e\u8bbe\u7f6eI\/O\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u907f\u514d\u7a0b\u5e8f\u65e0\u9650\u671f\u963b\u585e\uff1a<\/p>\n<pre><code class=\"python\"># \u8bbe\u7f6e\u5957\u63a5\u5b57\u8d85\u65f6\ns.settimeout(5)  # 5\u79d2\u8d85\u65f6\n\n# \u83b7\u53d6\u5957\u63a5\u5b57\u8d85\u65f6\ntimeout = s.gettimeout()<\/code><\/pre>\n<h5>4.1.2 \u5957\u63a5\u5b57\u5730\u5740\u91cd\u7528<\/h5>\n<p>\u5957\u63a5\u5b57\u5730\u5740\u91cd\u7528\u7528\u4e8e\u5141\u8bb8\u5728\u5957\u63a5\u5b57\u5173\u95ed\u540e\u7acb\u5373\u91cd\u7528\u76f8\u540c\u7684\u5730\u5740\u548c\u7aef\u53e3\uff1a<\/p>\n<pre><code class=\"python\"># \u8bbe\u7f6e\u5730\u5740\u91cd\u7528\ns.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)<\/code><\/pre>\n<h5>4.1.3 \u5957\u63a5\u5b57\u7f13\u51b2\u533a<\/h5>\n<p>\u5957\u63a5\u5b57\u7f13\u51b2\u533a\u7528\u4e8e\u63a7\u5236\u6570\u636e\u7684\u6536\u53d1\u901f\u5ea6\uff1a<\/p>\n<pre><code class=\"python\"># \u83b7\u53d6\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f\nrecv_buf = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)\n\n# \u8bbe\u7f6e\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f\ns.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8192)\n\n# \u83b7\u53d6\u53d1\u9001\u7f13\u51b2\u533a\u5927\u5c0f\nsend_buf = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)\n\n# \u8bbe\u7f6e\u53d1\u9001\u7f13\u51b2\u533a\u5927\u5c0f\ns.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 8192)<\/code><\/pre>\n<h4>4.2 \u7f51\u7edc\u5e93<\/h4>\n<p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7f51\u7edc\u5e93\uff0c\u7528\u4e8e\u7b80\u5316\u7f51\u7edc\u7f16\u7a0b\uff1a<\/p>\n<h5>4.2.1 socketserver\u6a21\u5757<\/h5>\n<p><code>socketserver<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6846\u67b6\uff0c\u7528\u4e8e\u521b\u5efa\u7f51\u7edc\u670d\u52a1\u5668\uff1a<\/p>\n<ul>\n<li><strong>TCPServer<\/strong>\uff1aTCP\u670d\u52a1\u5668<\/li>\n<li><strong>UDPServer<\/strong>\uff1aUDP\u670d\u52a1\u5668<\/li>\n<li><strong>ThreadingTCPServer<\/strong>\uff1a\u591a\u7ebf\u7a0bTCP\u670d\u52a1\u5668<\/li>\n<li><strong>ForkingTCPServer<\/strong>\uff1a\u591a\u8fdb\u7a0bTCP\u670d\u52a1\u5668<\/li>\n<\/ul>\n<h5>4.2.2 http\u6a21\u5757<\/h5>\n<p><code>http<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86HTTP\u534f\u8bae\u7684\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li><strong>http.server<\/strong>\uff1aHTTP\u670d\u52a1\u5668<\/li>\n<li><strong>http.client<\/strong>\uff1aHTTP\u5ba2\u6237\u7aef<\/li>\n<\/ul>\n<h5>4.2.3 urllib\u6a21\u5757<\/h5>\n<p><code>urllib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86URL\u5904\u7406\u7684\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><strong>urllib.request<\/strong>\uff1a\u6253\u5f00\u548c\u8bfb\u53d6URL<\/li>\n<li><strong>urllib.error<\/strong>\uff1a\u5904\u7406URLLib\u7684\u9519\u8bef<\/li>\n<li><strong>urllib.parse<\/strong>\uff1a\u89e3\u6790URL<\/li>\n<li><strong>urllib.robotparser<\/strong>\uff1a\u89e3\u6790robots.txt\u6587\u4ef6<\/li>\n<\/ul>\n<h5>4.2.4 requests\u5e93<\/h5>\n<p><code>requests<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u7b80\u5316HTTP\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"python\">import requests\n\nresponse = requests.get('https:\/\/www.baidu.com')\nprint(response.status_code)\nprint(response.text)<\/code><\/pre>\n<h5>4.2.5 asyncio\u5e93<\/h5>\n<p><code>asyncio<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f02\u6b65I\/O\u7684\u652f\u6301\uff0c\u7528\u4e8e\u5904\u7406\u5e76\u53d1\u7f51\u7edc\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"python\">import asyncio\nimport aiohttp\n\nasync def fetch_url(url):\n    async with aiohttp.ClientSession() as session:\n        async with session.get(url) as response:\n            return await response.text()\n\nasync def main():\n    html = await fetch_url('https:\/\/www.baidu.com')\n    print(html[:100])\n\nasyncio.run(main())<\/code><\/pre>\n<h4>4.3 \u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b\u7684\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b\u793a\u4f8b\n\nimport socket\nimport socketserver\nimport http.server\nimport urllib.request\nimport urllib.parse\nimport urllib.error\nimport threading\nimport time\n\n# 1. socketserver\u6a21\u5757\u793a\u4f8b\nprint(&quot;socketserver\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\nclass MyTCPHandler(socketserver.BaseRequestHandler):\n    &quot;&quot;&quot;TCP\u8bf7\u6c42\u5904\u7406\u5668&quot;&quot;&quot;\n    def handle(self):\n        # \u63a5\u6536\u6570\u636e\n        self.data = self.request.recv(1024).strip()\n        print(f&quot;\u6536\u5230\u6765\u81ea {self.client_address} \u7684\u6570\u636e: {self.data.decode('utf-8')}&quot;)\n        \n        # \u53d1\u9001\u54cd\u5e94\n        response = f&quot;\u670d\u52a1\u5668\u6536\u5230: {self.data.decode('utf-8')}&quot;\n        self.request.sendall(response.encode('utf-8'))\n        print(f&quot;\u53d1\u9001\u54cd\u5e94\u5230 {self.client_address}: {response}&quot;)\n\ndef start_socketserver():\n    &quot;&quot;&quot;\u542f\u52a8socketserver&quot;&quot;&quot;\n    HOST, PORT = &quot;localhost&quot;, 9999\n    \n    # \u521b\u5efa\u670d\u52a1\u5668\n    with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:\n        print(f&quot;socketserver\u542f\u52a8\u6210\u529f\uff0c\u76d1\u542c {HOST}:{PORT}&quot;)\n        # \u542f\u52a8\u670d\u52a1\u5668\n        server.serve_forever()\n\n# \u542f\u52a8socketserver\uff08\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\uff09\n# threading.Thread(target=start_socketserver, daemon=True).start()\n# time.sleep(1)  # \u7b49\u5f85\u670d\u52a1\u5668\u542f\u52a8\n\n# \u6d4b\u8bd5socketserver\ndef test_socketserver():\n    &quot;&quot;&quot;\u6d4b\u8bd5socketserver&quot;&quot;&quot;\n    HOST, PORT = &quot;localhost&quot;, 9999\n    \n    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n        sock.connect((HOST, PORT))\n        sock.sendall(b&quot;Hello, socketserver!&quot;)\n        response = sock.recv(1024)\n        print(f&quot;\u6536\u5230\u54cd\u5e94: {response.decode('utf-8')}&quot;)\n\n# 2. http.server\u6a21\u5757\u793a\u4f8b\nprint(&quot;\/nhttp.server\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\ndef start_http_server():\n    &quot;&quot;&quot;\u542f\u52a8HTTP\u670d\u52a1\u5668&quot;&quot;&quot;\n    HOST, PORT = &quot;localhost&quot;, 8000\n    \n    # \u521b\u5efa\u670d\u52a1\u5668\n    handler = http.server.SimpleHTTPRequestHandler\n    with socketserver.TCPServer((HOST, PORT), handler) as httpd:\n        print(f&quot;HTTP\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\uff0c\u76d1\u542c {HOST}:{PORT}&quot;)\n        print(f&quot;\u8bbf\u95ee\u5730\u5740: http:\/\/{HOST}:{PORT}&quot;)\n        # \u542f\u52a8\u670d\u52a1\u5668\n        httpd.serve_forever()\n\n# \u542f\u52a8HTTP\u670d\u52a1\u5668\uff08\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\uff09\n# threading.Thread(target=start_http_server, daemon=True).start()\n# time.sleep(1)  # \u7b49\u5f85\u670d\u52a1\u5668\u542f\u52a8\n\n# 3. urllib\u6a21\u5757\u793a\u4f8b\nprint(&quot;\/nurllib\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\ndef test_urllib():\n    &quot;&quot;&quot;\u6d4b\u8bd5urllib&quot;&quot;&quot;\n    # \u53d1\u9001GET\u8bf7\u6c42\n    url = &quot;https:\/\/www.baidu.com&quot;\n    print(f&quot;\u53d1\u9001GET\u8bf7\u6c42\u5230: {url}&quot;)\n    \n    try:\n        with urllib.request.urlopen(url) as response:\n            # \u83b7\u53d6\u54cd\u5e94\u72b6\u6001\u7801\n            print(f&quot;\u54cd\u5e94\u72b6\u6001\u7801: {response.getcode()}&quot;)\n            \n            # \u83b7\u53d6\u54cd\u5e94\u5934\n            print(&quot;\u54cd\u5e94\u5934:&quot;)\n            for key, value in response.getheaders():\n                print(f&quot;  {key}: {value}&quot;)\n            \n            # \u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\n            content = response.read()\n            print(f&quot;\u54cd\u5e94\u5185\u5bb9\u957f\u5ea6: {len(content)} \u5b57\u8282&quot;)\n            print(f&quot;\u54cd\u5e94\u5185\u5bb9\u524d100\u4e2a\u5b57\u7b26: {content.decode('utf-8')[:100]}...&quot;)\n            \n    except urllib.error.URLError as e:\n        print(f&quot;URL\u9519\u8bef: {e}&quot;)\n    except Exception as e:\n        print(f&quot;\u9519\u8bef: {e}&quot;)\n\n# \u6d4b\u8bd5urllib\n# test_urllib()\n\n# 4. \u89e3\u6790URL\u793a\u4f8b\nprint(&quot;\/n\u89e3\u6790URL\u793a\u4f8b\uff1a&quot;)\n\ndef parse_url_example():\n    &quot;&quot;&quot;\u89e3\u6790URL&quot;&quot;&quot;\n    url = &quot;https:\/\/www.python.org:8080\/downloads\/?ref=sidebar#latest&quot;\n    print(f&quot;\u539f\u59cbURL: {url}&quot;)\n    \n    # \u89e3\u6790URL\n    parsed = urllib.parse.urlparse(url)\n    print(&quot;\u89e3\u6790\u7ed3\u679c:&quot;)\n    print(f&quot;  \u534f\u8bae: {parsed.scheme}&quot;)\n    print(f&quot;  \u7f51\u7edc\u4f4d\u7f6e: {parsed.netloc}&quot;)\n    print(f&quot;  \u8def\u5f84: {parsed.path}&quot;)\n    print(f&quot;  \u53c2\u6570: {parsed.params}&quot;)\n    print(f&quot;  \u67e5\u8be2: {parsed.query}&quot;)\n    print(f&quot;  \u7247\u6bb5: {parsed.fragment}&quot;)\n    \n    # \u5206\u89e3\u7f51\u7edc\u4f4d\u7f6e\n    netloc = parsed.netloc\n    if '@' in netloc:\n        auth, netloc = netloc.split('@', 1)\n        print(f&quot;  \u8ba4\u8bc1\u4fe1\u606f: {auth}&quot;)\n    \n    if ':' in netloc:\n        host, port = netloc.split(':', 1)\n        print(f&quot;  \u4e3b\u673a: {host}&quot;)\n        print(f&quot;  \u7aef\u53e3: {port}&quot;)\n    else:\n        print(f&quot;  \u4e3b\u673a: {netloc}&quot;)\n        print(f&quot;  \u7aef\u53e3: \u65e0&quot;)\n    \n    # \u6784\u5efaURL\n    new_url = urllib.parse.urlunparse((\n        'https', 'www.example.com', '\/path', '', 'q=test', 'fragment'\n    ))\n    print(f&quot;\/n\u6784\u5efa\u7684\u65b0URL: {new_url}&quot;)\n\n# \u89e3\u6790URL\nparse_url_example()\n\n# 5. \u53d1\u9001POST\u8bf7\u6c42\u793a\u4f8b\nprint(&quot;\/n\u53d1\u9001POST\u8bf7\u6c42\u793a\u4f8b\uff1a&quot;)\n\ndef send_post_request():\n    &quot;&quot;&quot;\u53d1\u9001POST\u8bf7\u6c42&quot;&quot;&quot;\n    url = &quot;http:\/\/httpbin.org\/post&quot;\n    data = {\n        &quot;name&quot;: &quot;Python&quot;,\n        &quot;version&quot;: &quot;3.10&quot;\n    }\n    \n    # \u7f16\u7801\u6570\u636e\n    encoded_data = urllib.parse.urlencode(data).encode('utf-8')\n    print(f&quot;\u53d1\u9001POST\u8bf7\u6c42\u5230: {url}&quot;)\n    print(f&quot;\u53d1\u9001\u6570\u636e: {data}&quot;)\n    \n    try:\n        # \u521b\u5efa\u8bf7\u6c42\n        req = urllib.request.Request(url, data=encoded_data, method='POST')\n        req.add_header('Content-Type', 'application\/x-www-form-urlencoded')\n        \n        with urllib.request.urlopen(req) as response:\n            # \u83b7\u53d6\u54cd\u5e94\n            content = response.read()\n            print(f&quot;\u54cd\u5e94\u72b6\u6001\u7801: {response.getcode()}&quot;)\n            print(f&quot;\u54cd\u5e94\u5185\u5bb9: {content.decode('utf-8')}&quot;)\n            \n    except urllib.error.URLError as e:\n        print(f&quot;URL\u9519\u8bef: {e}&quot;)\n    except Exception as e:\n        print(f&quot;\u9519\u8bef: {e}&quot;)\n\n# \u53d1\u9001POST\u8bf7\u6c42\n# send_post_request()\n\nprint(&quot;\/n\u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b\u793a\u4f8b\u7ed3\u675f&quot;)<\/code><\/pre>\n<h3>5. \u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<p>\u7f51\u7edc\u7f16\u7a0b\u662fPython\u7f16\u7a0b\u4e2d\u7684\u91cd\u8981\u90e8\u5206\uff0c\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002<\/p>\n<h4>5.1 \u9519\u8bef\u5904\u7406<\/h4>\n<p>\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5e94\u8be5\u6355\u83b7\u548c\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\uff1a<\/p>\n<ul>\n<li><strong>\u8fde\u63a5\u9519\u8bef<\/strong>\uff1a<code>ConnectionError<\/code>\u3001<code>TimeoutError<\/code><\/li>\n<li><strong>\u5730\u5740\u9519\u8bef<\/strong>\uff1a<code>socket.gaierror<\/code><\/li>\n<li><strong>\u534f\u8bae\u9519\u8bef<\/strong>\uff1a<code>ProtocolError<\/code><\/li>\n<li><strong>\u6570\u636e\u9519\u8bef<\/strong>\uff1a<code>ValueError<\/code>\u3001<code>TypeError<\/code><\/li>\n<\/ul>\n<h4>5.2 \u8d85\u65f6\u8bbe\u7f6e<\/h4>\n<p>\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u907f\u514d\u7a0b\u5e8f\u65e0\u9650\u671f\u963b\u585e\uff1a<\/p>\n<ul>\n<li><strong>\u8fde\u63a5\u8d85\u65f6<\/strong>\uff1a\u8bbe\u7f6e\u8fde\u63a5\u670d\u52a1\u5668\u7684\u8d85\u65f6\u65f6\u95f4<\/li>\n<li><strong>\u8bfb\u53d6\u8d85\u65f6<\/strong>\uff1a\u8bbe\u7f6e\u8bfb\u53d6\u6570\u636e\u7684\u8d85\u65f6\u65f6\u95f4<\/li>\n<li><strong>\u5199\u5165\u8d85\u65f6<\/strong>\uff1a\u8bbe\u7f6e\u5199\u5165\u6570\u636e\u7684\u8d85\u65f6\u65f6\u95f4<\/li>\n<\/ul>\n<h4>5.3 \u8d44\u6e90\u7ba1\u7406<\/h4>\n<p>\u6b63\u786e\u7ba1\u7406\u7f51\u7edc\u8d44\u6e90\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\uff1a<\/p>\n<ul>\n<li><strong>\u5173\u95ed\u8fde\u63a5<\/strong>\uff1a\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u5957\u63a5\u5b57<\/li>\n<li><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong>\uff1a\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u5173\u95ed\u5957\u63a5\u5b57<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u5f02\u5e38\u5904\u7406\u4e2d\u786e\u4fdd\u5173\u95ed\u8d44\u6e90<\/li>\n<\/ul>\n<h4>5.4 \u5e76\u53d1\u5904\u7406<\/h4>\n<p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u591a\u4e2a\u8fde\u63a5\u7684\u573a\u666f\uff0c\u5e94\u8be5\u4f7f\u7528\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u5355\u7ebf\u7a0b<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5c11\u91cf\u8fde\u63a5\u7684\u573a\u666f<\/li>\n<li><strong>\u591a\u7ebf\u7a0b<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u4e2d\u7b49\u6570\u91cf\u8fde\u63a5\u7684\u573a\u666f<\/li>\n<li><strong>\u591a\u8fdb\u7a0b<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u573a\u666f<\/li>\n<li><strong>I\/O\u591a\u8def\u590d\u7528<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u8fde\u63a5\u7684\u573a\u666f<\/li>\n<li><strong>\u5f02\u6b65I\/O<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u7684\u573a\u666f<\/li>\n<\/ul>\n<h4>5.5 \u5b89\u5168\u8003\u8651<\/h4>\n<p>\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u5b89\u5168\u662f\u975e\u5e38\u91cd\u8981\u7684\uff1a<\/p>\n<ul>\n<li><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong>\uff1a\u9a8c\u8bc1\u6240\u6709\u8f93\u5165\u6570\u636e\uff0c\u907f\u514d\u6ce8\u5165\u653b\u51fb<\/li>\n<li><strong>\u52a0\u5bc6\u901a\u4fe1<\/strong>\uff1a\u4f7f\u7528HTTPS\u3001SSL\/TLS\u7b49\u52a0\u5bc6\u534f\u8bae<\/li>\n<li><strong>\u8eab\u4efd\u9a8c\u8bc1<\/strong>\uff1a\u5b9e\u73b0\u9002\u5f53\u7684\u8eab\u4efd\u9a8c\u8bc1\u673a\u5236<\/li>\n<li><strong>\u8bbf\u95ee\u63a7\u5236<\/strong>\uff1a\u5b9e\u73b0\u9002\u5f53\u7684\u8bbf\u95ee\u63a7\u5236\u673a\u5236<\/li>\n<li><strong>\u9632\u6b62DDoS\u653b\u51fb<\/strong>\uff1a\u5b9e\u73b0\u901f\u7387\u9650\u5236\u7b49\u673a\u5236<\/li>\n<\/ul>\n<h4>5.6 \u6027\u80fd\u4f18\u5316<\/h4>\n<p>\u7f51\u7edc\u7f16\u7a0b\u7684\u6027\u80fd\u4f18\u5316\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/strong>\uff1a\u6839\u636e\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/li>\n<li><strong>\u4f18\u5316\u7f13\u51b2\u533a\u5927\u5c0f<\/strong>\uff1a\u6839\u636e\u6570\u636e\u5927\u5c0f\u8c03\u6574\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>\u51cf\u5c11\u7f51\u7edc\u5f80\u8fd4<\/strong>\uff1a\u6279\u91cf\u5904\u7406\u6570\u636e\uff0c\u51cf\u5c11\u7f51\u7edc\u5f80\u8fd4\u6b21\u6570<\/li>\n<li><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u91cd\u7528\u8fde\u63a5\uff0c\u51cf\u5c11\u8fde\u63a5\u5efa\u7acb\u7684\u5f00\u9500<\/li>\n<li><strong>\u538b\u7f29\u6570\u636e<\/strong>\uff1a\u4f7f\u7528\u538b\u7f29\u7b97\u6cd5\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u91cf<\/li>\n<li><strong>\u4f7f\u7528CDN<\/strong>\uff1a\u5bf9\u4e8e\u9759\u6001\u5185\u5bb9\uff0c\u4f7f\u7528CDN\u52a0\u901f<\/li>\n<\/ul>\n<h4>5.7 \u4ee3\u7801\u7ec4\u7ec7<\/h4>\n<p>\u826f\u597d\u7684\u4ee3\u7801\u7ec4\u7ec7\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\uff1a<\/p>\n<ul>\n<li><strong>\u6a21\u5757\u5316<\/strong>\uff1a\u5c06\u4ee3\u7801\u5206\u89e3\u4e3a\u591a\u4e2a\u6a21\u5757<\/li>\n<li><strong>\u5c01\u88c5<\/strong>\uff1a\u5c01\u88c5\u7f51\u7edc\u64cd\u4f5c\u4e3a\u51fd\u6570\u6216\u7c7b<\/li>\n<li><strong>\u6587\u6863<\/strong>\uff1a\u4e3a\u4ee3\u7801\u6dfb\u52a0\u9002\u5f53\u7684\u6587\u6863<\/li>\n<li><strong>\u6d4b\u8bd5<\/strong>\uff1a\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\u786e\u4fdd\u529f\u80fd\u6b63\u786e<\/li>\n<\/ul>\n<h4>5.8 \u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\n\nimport socket\nimport time\nimport ssl\nimport threading\nfrom concurrent.futures import ThreadPoolExecutor\n\n# 1. \u9519\u8bef\u5904\u7406\u793a\u4f8b\nprint(&quot;\u9519\u8bef\u5904\u7406\u793a\u4f8b\uff1a&quot;)\n\ndef safe_connect(host, port, timeout=5):\n    &quot;&quot;&quot;\u5b89\u5168\u8fde\u63a5\u793a\u4f8b&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        \n        # \u8bbe\u7f6e\u8d85\u65f6\n        sock.settimeout(timeout)\n        \n        # \u8fde\u63a5\u670d\u52a1\u5668\n        sock.connect((host, port))\n        print(f&quot;\u8fde\u63a5 {host}:{port} \u6210\u529f&quot;)\n        \n        # \u5173\u95ed\u8fde\u63a5\n        sock.close()\n        return True\n        \n    except socket.timeout:\n        print(f&quot;\u8fde\u63a5 {host}:{port} \u8d85\u65f6&quot;)\n        return False\n    except socket.gaierror:\n        print(f&quot;\u89e3\u6790 {host} \u5931\u8d25&quot;)\n        return False\n    except ConnectionRefusedError:\n        print(f&quot;\u8fde\u63a5 {host}:{port} \u88ab\u62d2\u7edd&quot;)\n        return False\n    except Exception as e:\n        print(f&quot;\u8fde\u63a5 {host}:{port} \u5931\u8d25: {e}&quot;)\n        return False\n\n# \u6d4b\u8bd5\u8fde\u63a5\nsafe_connect(&quot;www.baidu.com&quot;, 80)\nsafe_connect(&quot;www.nonexistentdomain12345.com&quot;, 80)\nsafe_connect(&quot;www.baidu.com&quot;, 8888)  # \u4e0d\u5b58\u5728\u7684\u7aef\u53e3\n\n# 2. \u8d85\u65f6\u8bbe\u7f6e\u793a\u4f8b\nprint(&quot;\/n\u8d85\u65f6\u8bbe\u7f6e\u793a\u4f8b\uff1a&quot;)\n\ndef test_timeout():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u8d85\u65f6\u8bbe\u7f6e&quot;&quot;&quot;\n    host, port = &quot;www.baidu.com&quot;, 80\n    \n    # \u6d4b\u8bd5\u4e0d\u540c\u7684\u8d85\u65f6\u8bbe\u7f6e\n    timeouts = [1, 3, 5]\n    for timeout in timeouts:\n        start_time = time.time()\n        result = safe_connect(host, port, timeout)\n        end_time = time.time()\n        print(f&quot;\u8d85\u65f6\u8bbe\u7f6e {timeout} \u79d2\uff0c\u5b9e\u9645\u8017\u65f6 {end_time - start_time:.2f} \u79d2&quot;)\n\n# \u6d4b\u8bd5\u8d85\u65f6\ntest_timeout()\n\n# 3. \u8d44\u6e90\u7ba1\u7406\u793a\u4f8b\nprint(&quot;\/n\u8d44\u6e90\u7ba1\u7406\u793a\u4f8b\uff1a&quot;)\n\n# \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\nclass SocketContext:\n    &quot;&quot;&quot;\u5957\u63a5\u5b57\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&quot;&quot;&quot;\n    def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM):\n        self.family = family\n        self.type = type\n        self.sock = None\n    \n    def __enter__(self):\n        &quot;&quot;&quot;\u8fdb\u5165\u4e0a\u4e0b\u6587&quot;&quot;&quot;\n        self.sock = socket.socket(self.family, self.type)\n        return self.sock\n    \n    def __exit__(self, exc_type, exc_val, exc_tb):\n        &quot;&quot;&quot;\u9000\u51fa\u4e0a\u4e0b\u6587&quot;&quot;&quot;\n        if self.sock:\n            self.sock.close()\n            print(&quot;\u5957\u63a5\u5b57\u5df2\u5173\u95ed&quot;)\n\n# \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\nprint(&quot;\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff1a&quot;)\nwith SocketContext() as sock:\n    sock.settimeout(3)\n    try:\n        sock.connect((&quot;www.baidu.com&quot;, 80))\n        print(&quot;\u8fde\u63a5\u6210\u529f&quot;)\n        # \u53d1\u9001HTTP\u8bf7\u6c42\n        sock.sendall(b&quot;GET \/ HTTP\/1.1\/r\/nHost: www.baidu.com\/r\/nConnection: close\/r\/n\/r\/n&quot;)\n        # \u63a5\u6536\u54cd\u5e94\n        data = sock.recv(1024)\n        print(f&quot;\u6536\u5230\u54cd\u5e94\uff1a{data.decode('utf-8')[:100]}...&quot;)\n    except Exception as e:\n        print(f&quot;\u9519\u8bef\uff1a{e}&quot;)\n\n# 4. \u5e76\u53d1\u5904\u7406\u793a\u4f8b\nprint(&quot;\/n\u5e76\u53d1\u5904\u7406\u793a\u4f8b\uff1a&quot;)\n\ndef check_website(url):\n    &quot;&quot;&quot;\u68c0\u67e5\u7f51\u7ad9\u662f\u5426\u53ef\u8bbf\u95ee&quot;&quot;&quot;\n    try:\n        # \u89e3\u6790URL\n        from urllib.parse import urlparse\n        parsed = urlparse(url)\n        host = parsed.netloc\n        port = parsed.port or (443 if parsed.scheme == 'https' else 80)\n        \n        # \u8fde\u63a5\u7f51\u7ad9\n        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n            sock.settimeout(3)\n            sock.connect((host, port))\n            \n            # \u5982\u679c\u662fHTTPS\uff0c\u8fdb\u884cSSL\u63e1\u624b\n            if parsed.scheme == 'https':\n                context = ssl.create_default_context()\n                with context.wrap_socket(sock, server_hostname=host) as ssock:\n                    # \u53d1\u9001HTTP\u8bf7\u6c42\n                    ssock.sendall(f&quot;GET {parsed.path or '\/'} HTTP\/1.1\/r\/nHost: {host}\/r\/nConnection: close\/r\/n\/r\/n&quot;.encode('utf-8'))\n                    # \u63a5\u6536\u54cd\u5e94\n                    data = ssock.recv(1024)\n            else:\n                # \u53d1\u9001HTTP\u8bf7\u6c42\n                sock.sendall(f&quot;GET {parsed.path or '\/'} HTTP\/1.1\/r\/nHost: {host}\/r\/nConnection: close\/r\/n\/r\/n&quot;.encode('utf-8'))\n                # \u63a5\u6536\u54cd\u5e94\n                data = sock.recv(1024)\n        \n        print(f&quot;{url} - \u53ef\u8bbf\u95ee&quot;)\n        return True\n        \n    except Exception as e:\n        print(f&quot;{url} - \u4e0d\u53ef\u8bbf\u95ee: {e}&quot;)\n        return False\n\n# \u6d4b\u8bd5\u7f51\u7ad9\u5217\u8868\nwebsites = [\n    &quot;https:\/\/www.baidu.com&quot;,\n    &quot;https:\/\/www.google.com&quot;,\n    &quot;https:\/\/www.python.org&quot;,\n    &quot;https:\/\/www.github.com&quot;,\n    &quot;https:\/\/www.nonexistentdomain12345.com&quot;\n]\n\n# \u4e32\u884c\u68c0\u67e5\nprint(&quot;\/n\u4e32\u884c\u68c0\u67e5\u7f51\u7ad9\uff1a&quot;)\nstart_time = time.time()\nfor website in websites:\n    check_website(website)\nend_time = time.time()\nprint(f&quot;\u4e32\u884c\u68c0\u67e5\u8017\u65f6: {end_time - start_time:.2f} \u79d2&quot;)\n\n# \u5e76\u53d1\u68c0\u67e5\nprint(&quot;\/n\u5e76\u53d1\u68c0\u67e5\u7f51\u7ad9\uff1a&quot;)\nstart_time = time.time()\nwith ThreadPoolExecutor(max_workers=5) as executor:\n    executor.map(check_website, websites)\nend_time = time.time()\nprint(f&quot;\u5e76\u53d1\u68c0\u67e5\u8017\u65f6: {end_time - start_time:.2f} \u79d2&quot;)\n\n# 5. \u5b89\u5168\u901a\u4fe1\u793a\u4f8b\nprint(&quot;\/n\u5b89\u5168\u901a\u4fe1\u793a\u4f8b\uff1a&quot;)\n\ndef secure_communication():\n    &quot;&quot;&quot;\u5b89\u5168\u901a\u4fe1\u793a\u4f8b&quot;&quot;&quot;\n    host, port = &quot;www.baidu.com&quot;, 443\n    \n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        \n        # \u521b\u5efaSSL\u4e0a\u4e0b\u6587\n        context = ssl.create_default_context()\n        \n        # \u5305\u88c5\u5957\u63a5\u5b57\n        with context.wrap_socket(sock, server_hostname=host) as ssock:\n            # \u8fde\u63a5\u670d\u52a1\u5668\n            ssock.connect((host, port))\n            print(f&quot;SSL\u8fde\u63a5 {host}:{port} \u6210\u529f&quot;)\n            \n            # \u83b7\u53d6\u8bc1\u4e66\u4fe1\u606f\n            cert = ssock.getpeercert()\n            print(&quot;\/n\u670d\u52a1\u5668\u8bc1\u4e66\u4fe1\u606f\uff1a&quot;)\n            print(f&quot;\u4e3b\u9898: {dict(x[0] for x in cert['subject'])}&quot;)\n            print(f&quot;\u9881\u53d1\u8005: {dict(x[0] for x in cert['issuer'])}&quot;)\n            print(f&quot;\u6709\u6548\u671f: \u4ece {cert['notBefore']} \u5230 {cert['notAfter']}&quot;)\n            \n            # \u53d1\u9001HTTP\u8bf7\u6c42\n            request = &quot;GET \/ HTTP\/1.1\/r\/n&quot;\n            request += f&quot;Host: {host}\/r\/n&quot;\n            request += &quot;Connection: close\/r\/n&quot;\n            request += &quot;\/r\/n&quot;\n            ssock.sendall(request.encode('utf-8'))\n            print(&quot;\/n\u53d1\u9001HTTPS\u8bf7\u6c42&quot;)\n            \n            # \u63a5\u6536\u54cd\u5e94\n            response = b&quot;&quot;\n            while True:\n                data = ssock.recv(1024)\n                if not data:\n                    break\n                response += data\n            \n            # \u89e3\u6790\u54cd\u5e94\n            response_str = response.decode('utf-8')\n            print(f&quot;\/n\u6536\u5230\u54cd\u5e94\uff0c\u72b6\u6001\u7801: {response_str.split('\/r\/n')[0]}&quot;)\n            print(f&quot;\u54cd\u5e94\u5934\u6570\u91cf: {len([line for line in response_str.split('\/r\/n') if line]) - 1}&quot;)\n            \n    except Exception as e:\n        print(f&quot;\u5b89\u5168\u901a\u4fe1\u5931\u8d25: {e}&quot;)\n\n# \u6d4b\u8bd5\u5b89\u5168\u901a\u4fe1\nsecure_communication()\n\n# 6. \u8fde\u63a5\u6c60\u793a\u4f8b\nprint(&quot;\/n\u8fde\u63a5\u6c60\u793a\u4f8b\uff1a&quot;)\n\nclass ConnectionPool:\n    &quot;&quot;&quot;\u7b80\u5355\u7684\u8fde\u63a5\u6c60&quot;&quot;&quot;\n    def __init__(self, host, port, max_connections=5):\n        self.host = host\n        self.port = port\n        self.max_connections = max_connections\n        self.pool = []\n        self.lock = threading.Lock()\n        \n        # \u521d\u59cb\u5316\u8fde\u63a5\u6c60\n        for _ in range(max_connections):\n            try:\n                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n                sock.connect((host, port))\n                self.pool.append(sock)\n            except Exception as e:\n                print(f&quot;\u521d\u59cb\u5316\u8fde\u63a5\u5931\u8d25: {e}&quot;)\n        \n        print(f&quot;\u8fde\u63a5\u6c60\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u53ef\u7528\u8fde\u63a5\u6570: {len(self.pool)}&quot;)\n    \n    def get_connection(self):\n        &quot;&quot;&quot;\u83b7\u53d6\u8fde\u63a5&quot;&quot;&quot;\n        with self.lock:\n            if self.pool:\n                return self.pool.pop()\n            else:\n                # \u521b\u5efa\u65b0\u8fde\u63a5\n                try:\n                    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n                    sock.connect((self.host, self.port))\n                    print(&quot;\u521b\u5efa\u65b0\u8fde\u63a5&quot;)\n                    return sock\n                except Exception as e:\n                    print(f&quot;\u521b\u5efa\u8fde\u63a5\u5931\u8d25: {e}&quot;)\n                    return None\n    \n    def return_connection(self, sock):\n        &quot;&quot;&quot;\u8fd4\u56de\u8fde\u63a5&quot;&quot;&quot;\n        with self.lock:\n            if len(self.pool) &lt; self.max_connections:\n                self.pool.append(sock)\n            else:\n                # \u8fde\u63a5\u6c60\u5df2\u6ee1\uff0c\u5173\u95ed\u8fde\u63a5\n                sock.close()\n    \n    def close_all(self):\n        &quot;&quot;&quot;\u5173\u95ed\u6240\u6709\u8fde\u63a5&quot;&quot;&quot;\n        with self.lock:\n            for sock in self.pool:\n                try:\n                    sock.close()\n                except Exception:\n                    pass\n            self.pool = []\n        print(&quot;\u8fde\u63a5\u6c60\u5df2\u5173\u95ed&quot;)\n\n# \u4f7f\u7528\u8fde\u63a5\u6c60\ndef use_connection_pool():\n    &quot;&quot;&quot;\u4f7f\u7528\u8fde\u63a5\u6c60&quot;&quot;&quot;\n    pool = ConnectionPool(&quot;www.baidu.com&quot;, 80, max_connections=3)\n    \n    # \u6a21\u62df\u591a\u4e2a\u7ebf\u7a0b\u4f7f\u7528\u8fde\u63a5\u6c60\n    def worker(task_id):\n        &quot;&quot;&quot;\u5de5\u4f5c\u7ebf\u7a0b&quot;&quot;&quot;\n        sock = pool.get_connection()\n        if sock:\n            try:\n                # \u53d1\u9001\u8bf7\u6c42\n                request = f&quot;GET \/ HTTP\/1.1\/r\/nHost: www.baidu.com\/r\/nConnection: keep-alive\/r\/n\/r\/n&quot;\n                sock.sendall(request.encode('utf-8'))\n                \n                # \u63a5\u6536\u54cd\u5e94\n                data = sock.recv(1024)\n                print(f&quot;\u4efb\u52a1 {task_id} \u6536\u5230\u54cd\u5e94: {data.decode('utf-8')[:50]}...&quot;)\n                \n                # \u6a21\u62df\u5904\u7406\u65f6\u95f4\n                time.sleep(1)\n                \n            except Exception as e:\n                print(f&quot;\u4efb\u52a1 {task_id} \u9519\u8bef: {e}&quot;)\n            finally:\n                # \u8fd4\u56de\u8fde\u63a5\n                pool.return_connection(sock)\n    \n    # \u521b\u5efa\u591a\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\n    threads = []\n    for i in range(10):\n        t = threading.Thread(target=worker, args=(i,))\n        threads.append(t)\n        t.start()\n    \n    # \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\n    for t in threads:\n        t.join()\n    \n    # \u5173\u95ed\u8fde\u63a5\u6c60\n    pool.close_all()\n\n# \u6d4b\u8bd5\u8fde\u63a5\u6c60\n# use_connection_pool()\n\nprint(&quot;\/n\u7f51\u7edc\u7f16\u7a0b\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\u7ed3\u675f&quot;)<\/code><\/pre>\n<h3>6. \u5e38\u89c1\u7f51\u7edc\u7f16\u7a0b\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u5728Python\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\u3002\u7406\u89e3\u8fd9\u4e9b\u95ee\u9898\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u5bf9\u4e8e\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>6.1 \u5e38\u89c1\u95ee\u9898<\/h4>\n<ul>\n<li><strong>\u8fde\u63a5\u8d85\u65f6<\/strong>\uff1a\u8fde\u63a5\u670d\u52a1\u5668\u65f6\u8d85\u65f6<\/li>\n<li><strong>\u8fde\u63a5\u88ab\u62d2\u7edd<\/strong>\uff1a\u670d\u52a1\u5668\u62d2\u7edd\u8fde\u63a5<\/li>\n<li><strong>DNS\u89e3\u6790\u5931\u8d25<\/strong>\uff1a\u65e0\u6cd5\u89e3\u6790\u57df\u540d<\/li>\n<li><strong>SSL\u8bc1\u4e66\u9519\u8bef<\/strong>\uff1aSSL\u8bc1\u4e66\u9a8c\u8bc1\u5931\u8d25<\/li>\n<li><strong>\u6570\u636e\u4f20\u8f93\u4e0d\u5b8c\u6574<\/strong>\uff1a\u63a5\u6536\u7684\u6570\u636e\u4e0d\u5b8c\u6574<\/li>\n<li><strong>\u5e76\u53d1\u8fde\u63a5\u6570\u9650\u5236<\/strong>\uff1a\u8d85\u8fc7\u7cfb\u7edf\u7684\u5e76\u53d1\u8fde\u63a5\u6570\u9650\u5236<\/li>\n<li><strong>\u7aef\u53e3\u5360\u7528<\/strong>\uff1a\u7aef\u53e3\u5df2\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528<\/li>\n<li><strong>\u7f51\u7edc\u4e0d\u7a33\u5b9a<\/strong>\uff1a\u7f51\u7edc\u8fde\u63a5\u4e0d\u7a33\u5b9a\uff0c\u9891\u7e41\u65ad\u5f00<\/li>\n<li><strong>\u9632\u706b\u5899\u9650\u5236<\/strong>\uff1a\u9632\u706b\u5899\u963b\u6b62\u4e86\u8fde\u63a5<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u7f51\u7edc\u64cd\u4f5c\u6027\u80fd\u4e0d\u4f73<\/li>\n<\/ul>\n<h4>6.2 \u89e3\u51b3\u65b9\u6848<\/h4>\n<h5>6.2.1 \u8fde\u63a5\u8d85\u65f6<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u8fde\u63a5\u670d\u52a1\u5668\u65f6\u8d85\u65f6\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4<\/strong>\uff1a\u6839\u636e\u7f51\u7edc\u73af\u5883\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4<\/li>\n<li><strong>\u91cd\u8bd5\u673a\u5236<\/strong>\uff1a\u5b9e\u73b0\u91cd\u8bd5\u673a\u5236\uff0c\u5728\u8d85\u65f6\u540e\u91cd\u65b0\u5c1d\u8bd5\u8fde\u63a5<\/li>\n<li><strong>\u5f02\u6b65I\/O<\/strong>\uff1a\u4f7f\u7528\u5f02\u6b65I\/O\u907f\u514d\u963b\u585e<\/li>\n<\/ul>\n<h5>6.2.2 \u8fde\u63a5\u88ab\u62d2\u7edd<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u670d\u52a1\u5668\u62d2\u7edd\u8fde\u63a5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u670d\u52a1\u5668\u72b6\u6001<\/strong>\uff1a\u786e\u4fdd\u670d\u52a1\u5668\u6b63\u5728\u8fd0\u884c<\/li>\n<li><strong>\u68c0\u67e5\u7aef\u53e3\u914d\u7f6e<\/strong>\uff1a\u786e\u4fdd\u670d\u52a1\u5668\u76d1\u542c\u5728\u6b63\u786e\u7684\u7aef\u53e3<\/li>\n<li><strong>\u68c0\u67e5\u9632\u706b\u5899<\/strong>\uff1a\u786e\u4fdd\u9632\u706b\u5899\u6ca1\u6709\u963b\u6b62\u8fde\u63a5<\/li>\n<li><strong>\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5<\/strong>\uff1a\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38<\/li>\n<\/ul>\n<h5>6.2.3 DNS\u89e3\u6790\u5931\u8d25<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u65e0\u6cd5\u89e3\u6790\u57df\u540d\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u57df\u540d\u662f\u5426\u6b63\u786e<\/strong>\uff1a\u786e\u4fdd\u57df\u540d\u62fc\u5199\u6b63\u786e<\/li>\n<li><strong>\u68c0\u67e5DNS\u670d\u52a1\u5668<\/strong>\uff1a\u786e\u4fddDNS\u670d\u52a1\u5668\u6b63\u5e38\u5de5\u4f5c<\/li>\n<li><strong>\u4f7f\u7528IP\u5730\u5740<\/strong>\uff1a\u5982\u679c\u53ef\u80fd\uff0c\u76f4\u63a5\u4f7f\u7528IP\u5730\u5740<\/li>\n<li><strong>\u7f13\u5b58DNS\u7ed3\u679c<\/strong>\uff1a\u5b9e\u73b0DNS\u7ed3\u679c\u7f13\u5b58\uff0c\u51cf\u5c11DNS\u89e3\u6790\u6b21\u6570<\/li>\n<\/ul>\n<h5>6.2.4 SSL\u8bc1\u4e66\u9519\u8bef<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1aSSL\u8bc1\u4e66\u9a8c\u8bc1\u5931\u8d25\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u6709\u6548\u7684\u8bc1\u4e66<\/strong>\uff1a\u786e\u4fdd\u670d\u52a1\u5668\u4f7f\u7528\u6709\u6548\u7684SSL\u8bc1\u4e66<\/li>\n<li><strong>\u66f4\u65b0\u8bc1\u4e66\u5e93<\/strong>\uff1a\u66f4\u65b0\u672c\u5730\u7684\u8bc1\u4e66\u5e93<\/li>\n<li><strong>\u7981\u7528\u8bc1\u4e66\u9a8c\u8bc1<\/strong>\uff1a\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u7981\u7528\u8bc1\u4e66\u9a8c\u8bc1\uff08\u4e0d\u63a8\u8350\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528\uff09<\/li>\n<li><strong>\u6307\u5b9aCA\u8bc1\u4e66<\/strong>\uff1a\u6307\u5b9a\u6b63\u786e\u7684CA\u8bc1\u4e66<\/li>\n<\/ul>\n<h5>6.2.5 \u6570\u636e\u4f20\u8f93\u4e0d\u5b8c\u6574<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u63a5\u6536\u7684\u6570\u636e\u4e0d\u5b8c\u6574\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5faa\u73af\u63a5\u6536<\/strong>\uff1a\u5b9e\u73b0\u5faa\u73af\u63a5\u6536\uff0c\u76f4\u5230\u6536\u5230\u5b8c\u6574\u7684\u6570\u636e<\/li>\n<li><strong>\u4f7f\u7528\u56fa\u5b9a\u957f\u5ea6<\/strong>\uff1a\u5982\u679c\u6570\u636e\u957f\u5ea6\u56fa\u5b9a\uff0c\u4f7f\u7528\u56fa\u5b9a\u957f\u5ea6\u63a5\u6536<\/li>\n<li><strong>\u4f7f\u7528\u5206\u9694\u7b26<\/strong>\uff1a\u4f7f\u7528\u5206\u9694\u7b26\u6807\u8bb0\u6570\u636e\u7ed3\u675f<\/li>\n<li><strong>\u4f7f\u7528\u957f\u5ea6\u524d\u7f00<\/strong>\uff1a\u5728\u6570\u636e\u524d\u6dfb\u52a0\u957f\u5ea6\u524d\u7f00<\/li>\n<\/ul>\n<h5>6.2.6 \u5e76\u53d1\u8fde\u63a5\u6570\u9650\u5236<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u8d85\u8fc7\u7cfb\u7edf\u7684\u5e76\u53d1\u8fde\u63a5\u6570\u9650\u5236\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u91cd\u7528\u8fde\u63a5\uff0c\u51cf\u5c11\u8fde\u63a5\u6570<\/li>\n<li><strong>\u4f7f\u7528\u5f02\u6b65I\/O<\/strong>\uff1a\u4f7f\u7528\u5f02\u6b65I\/O\u5904\u7406\u66f4\u591a\u8fde\u63a5<\/li>\n<li><strong>\u8c03\u6574\u7cfb\u7edf\u53c2\u6570<\/strong>\uff1a\u8c03\u6574\u7cfb\u7edf\u7684\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u9650\u5236<\/li>\n<li><strong>\u8d1f\u8f7d\u5747\u8861<\/strong>\uff1a\u4f7f\u7528\u8d1f\u8f7d\u5747\u8861\u5206\u6563\u8fde\u63a5<\/li>\n<\/ul>\n<h5>6.2.7 \u7aef\u53e3\u5360\u7528<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u7aef\u53e3\u5df2\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u4e0d\u540c\u7684\u7aef\u53e3<\/strong>\uff1a\u4f7f\u7528\u672a\u88ab\u5360\u7528\u7684\u7aef\u53e3<\/li>\n<li><strong>\u5173\u95ed\u5360\u7528\u7aef\u53e3\u7684\u8fdb\u7a0b<\/strong>\uff1a\u5173\u95ed\u5360\u7528\u7aef\u53e3\u7684\u8fdb\u7a0b<\/li>\n<li><strong>\u4f7f\u7528SO_REUSEADDR\u9009\u9879<\/strong>\uff1a\u5141\u8bb8\u91cd\u7528\u5730\u5740<\/li>\n<\/ul>\n<h5>6.2.8 \u7f51\u7edc\u4e0d\u7a33\u5b9a<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u7f51\u7edc\u8fde\u63a5\u4e0d\u7a33\u5b9a\uff0c\u9891\u7e41\u65ad\u5f00\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5b9e\u73b0\u91cd\u8fde\u673a\u5236<\/strong>\uff1a\u5728\u8fde\u63a5\u65ad\u5f00\u540e\u81ea\u52a8\u91cd\u8fde<\/li>\n<li><strong>\u4f7f\u7528\u5fc3\u8df3\u673a\u5236<\/strong>\uff1a\u5b9a\u671f\u53d1\u9001\u5fc3\u8df3\u5305\u4fdd\u6301\u8fde\u63a5<\/li>\n<li><strong>\u589e\u52a0\u8d85\u65f6\u65f6\u95f4<\/strong>\uff1a\u589e\u52a0\u8d85\u65f6\u65f6\u95f4\uff0c\u5bb9\u5fcd\u7f51\u7edc\u5ef6\u8fdf<\/li>\n<li><strong>\u4f7f\u7528\u53ef\u9760\u7684\u534f\u8bae<\/strong>\uff1a\u4f7f\u7528TCP\u7b49\u53ef\u9760\u7684\u534f\u8bae<\/li>\n<\/ul>\n<h5>6.2.9 \u9632\u706b\u5899\u9650\u5236<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u9632\u706b\u5899\u963b\u6b62\u4e86\u8fde\u63a5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u914d\u7f6e\u9632\u706b\u5899<\/strong>\uff1a\u914d\u7f6e\u9632\u706b\u5899\u5141\u8bb8\u8fde\u63a5<\/li>\n<li><strong>\u4f7f\u7528\u5e38\u7528\u7aef\u53e3<\/strong>\uff1a\u4f7f\u7528\u5e38\u7528\u7684\u7aef\u53e3\uff0c\u598280\u3001443<\/li>\n<li><strong>\u4f7f\u7528\u4ee3\u7406<\/strong>\uff1a\u901a\u8fc7\u4ee3\u7406\u670d\u52a1\u5668\u8fde\u63a5<\/li>\n<\/ul>\n<h5>6.2.10 \u6027\u80fd\u95ee\u9898<\/h5>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u7f51\u7edc\u64cd\u4f5c\u6027\u80fd\u4e0d\u4f73\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/strong>\uff1a\u6839\u636e\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/li>\n<li><strong>\u4f18\u5316\u7f13\u51b2\u533a\u5927\u5c0f<\/strong>\uff1a\u6839\u636e\u6570\u636e\u5927\u5c0f\u8c03\u6574\u7f13\u51b2\u533a\u5927\u5c0f<\/li>\n<li><strong>\u51cf\u5c11\u7f51\u7edc\u5f80\u8fd4<\/strong>\uff1a\u6279\u91cf\u5904\u7406\u6570\u636e\uff0c\u51cf\u5c11\u7f51\u7edc\u5f80\u8fd4\u6b21\u6570<\/li>\n<li><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u91cd\u7528\u8fde\u63a5\uff0c\u51cf\u5c11\u8fde\u63a5\u5efa\u7acb\u7684\u5f00\u9500<\/li>\n<li><strong>\u538b\u7f29\u6570\u636e<\/strong>\uff1a\u4f7f\u7528\u538b\u7f29\u7b97\u6cd5\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u91cf<\/li>\n<li><strong>\u4f7f\u7528CDN<\/strong>\uff1a\u5bf9\u4e8e\u9759\u6001\u5185\u5bb9\uff0c\u4f7f\u7528CDN\u52a0\u901f<\/li>\n<\/ul>\n<h4>6.3 \u5e38\u89c1\u7f51\u7edc\u7f16\u7a0b\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u5e38\u89c1\u7f51\u7edc\u7f16\u7a0b\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u5e38\u89c1\u7f51\u7edc\u7f16\u7a0b\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\u793a\u4f8b\n\nimport socket\nimport time\nimport ssl\nimport random\n\n# 1. \u8fde\u63a5\u8d85\u65f6\u89e3\u51b3\u65b9\u6848\nprint(&quot;\u8fde\u63a5\u8d85\u65f6\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\ndef retry_connect(host, port, max_retries=3, timeout=3):\n    &quot;&quot;&quot;\u5e26\u91cd\u8bd5\u673a\u5236\u7684\u8fde\u63a5&quot;&quot;&quot;\n    for i in range(max_retries):\n        try:\n            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            sock.settimeout(timeout)\n            sock.connect((host, port))\n            print(f&quot;\u8fde\u63a5 {host}:{port} \u6210\u529f&quot;)\n            return sock\n        except socket.timeout:\n            print(f&quot;\u8fde\u63a5 {host}:{port} \u8d85\u65f6\uff0c\u7b2c {i+1} \u6b21\u91cd\u8bd5&quot;)\n            time.sleep(1)  # \u7b49\u5f851\u79d2\u540e\u91cd\u8bd5\n        except Exception as e:\n            print(f&quot;\u8fde\u63a5 {host}:{port} \u5931\u8d25: {e}&quot;)\n            break\n    return None\n\n# \u6d4b\u8bd5\u91cd\u8bd5\u8fde\u63a5\nsock = retry_connect(&quot;www.baidu.com&quot;, 80)\nif sock:\n    sock.close()\n\n# 2. \u6570\u636e\u4f20\u8f93\u4e0d\u5b8c\u6574\u89e3\u51b3\u65b9\u6848\nprint(&quot;\/n\u6570\u636e\u4f20\u8f93\u4e0d\u5b8c\u6574\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\ndef recv_all(sock, buffer_size=1024):\n    &quot;&quot;&quot;\u63a5\u6536\u5b8c\u6574\u7684\u6570\u636e&quot;&quot;&quot;\n    data = b&quot;&quot;\n    while True:\n        part = sock.recv(buffer_size)\n        data += part\n        if len(part) &lt; buffer_size:\n            # \u6570\u636e\u63a5\u6536\u5b8c\u6210\n            break\n    return data\n\ndef test_recv_all():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u63a5\u6536\u5b8c\u6574\u7684\u6570\u636e&quot;&quot;&quot;\n    host, port = &quot;www.baidu.com&quot;, 80\n    \n    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n        sock.connect((host, port))\n        \n        # \u53d1\u9001HTTP\u8bf7\u6c42\n        request = &quot;GET \/ HTTP\/1.1\/r\/n&quot;\n        request += f&quot;Host: {host}\/r\/n&quot;\n        request += &quot;Connection: close\/r\/n&quot;\n        request += &quot;\/r\/n&quot;\n        sock.sendall(request.encode('utf-8'))\n        \n        # \u63a5\u6536\u5b8c\u6574\u7684\u54cd\u5e94\n        response = recv_all(sock)\n        print(f&quot;\u63a5\u6536\u5230\u5b8c\u6574\u7684\u54cd\u5e94\uff0c\u957f\u5ea6: {len(response)} \u5b57\u8282&quot;)\n        print(f&quot;\u54cd\u5e94\u72b6\u6001\u884c: {response.decode('utf-8').split('\/r\/n')[0]}&quot;)\n\n# \u6d4b\u8bd5\u63a5\u6536\u5b8c\u6574\u7684\u6570\u636e\ntest_recv_all()\n\n# 3. SSL\u8bc1\u4e66\u9519\u8bef\u89e3\u51b3\u65b9\u6848\nprint(&quot;\/nSSL\u8bc1\u4e66\u9519\u8bef\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\ndef secure_connect_with_cert(host, port):\n    &quot;&quot;&quot;\u5b89\u5168\u8fde\u63a5\uff08\u5904\u7406\u8bc1\u4e66\u9519\u8bef\uff09&quot;&quot;&quot;\n    try:\n        # \u521b\u5efa\u5957\u63a5\u5b57\n        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        \n        # \u521b\u5efaSSL\u4e0a\u4e0b\u6587\n        context = ssl.create_default_context()\n        \n        # \u5305\u88c5\u5957\u63a5\u5b57\n        with context.wrap_socket(sock, server_hostname=host) as ssock:\n            # \u8fde\u63a5\u670d\u52a1\u5668\n            ssock.connect((host, port))\n            print(f&quot;SSL\u8fde\u63a5 {host}:{port} \u6210\u529f&quot;)\n            \n            # \u53d1\u9001HTTP\u8bf7\u6c42\n            request = &quot;GET \/ HTTP\/1.1\/r\/n&quot;\n            request += f&quot;Host: {host}\/r\/n&quot;\n            request += &quot;Connection: close\/r\/n&quot;\n            request += &quot;\/r\/n&quot;\n            ssock.sendall(request.encode('utf-8'))\n            \n            # \u63a5\u6536\u54cd\u5e94\n            response = recv_all(ssock)\n            print(f&quot;\u6536\u5230\u54cd\u5e94\uff0c\u72b6\u6001\u7801: {response.decode('utf-8').split('\/r\/n')[0]}&quot;)\n            \n    except ssl.SSLCertVerificationError as e:\n        print(f&quot;SSL\u8bc1\u4e66\u9a8c\u8bc1\u5931\u8d25: {e}&quot;)\n        # \u53ef\u4ee5\u9009\u62e9\u521b\u5efa\u4e0d\u9a8c\u8bc1\u8bc1\u4e66\u7684\u4e0a\u4e0b\u6587\n        print(&quot;\u5c1d\u8bd5\u4e0d\u9a8c\u8bc1\u8bc1\u4e66...&quot;)\n        try:\n            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            # \u521b\u5efa\u4e0d\u9a8c\u8bc1\u8bc1\u4e66\u7684\u4e0a\u4e0b\u6587\n            unsafe_context = ssl._create_unverified_context()\n            with unsafe_context.wrap_socket(sock, server_hostname=host) as ssock:\n                ssock.connect((host, port))\n                print(f&quot;\u4e0d\u9a8c\u8bc1\u8bc1\u4e66\u7684SSL\u8fde\u63a5 {host}:{port} \u6210\u529f&quot;)\n        except Exception as e:\n            print(f&quot;\u4e0d\u9a8c\u8bc1\u8bc1\u4e66\u7684\u8fde\u63a5\u5931\u8d25: {e}&quot;)\n    except Exception as e:\n        print(f&quot;\u5b89\u5168\u8fde\u63a5\u5931\u8d25: {e}&quot;)\n\n# \u6d4b\u8bd5\u5b89\u5168\u8fde\u63a5\nsecure_connect_with_cert(&quot;www.baidu.com&quot;, 443)\n\n# 4. \u7aef\u53e3\u5360\u7528\u89e3\u51b3\u65b9\u6848\nprint(&quot;\/n\u7aef\u53e3\u5360\u7528\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\ndef find_free_port(start_port=8000, end_port=9000):\n    &quot;&quot;&quot;\u67e5\u627e\u53ef\u7528\u7aef\u53e3&quot;&quot;&quot;\n    for port in range(start_port, end_port):\n        try:\n            # \u5c1d\u8bd5\u7ed1\u5b9a\u7aef\u53e3\n            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n                sock.bind(('localhost', port))\n                print(f&quot;\u7aef\u53e3 {port} \u53ef\u7528&quot;)\n                return port\n        except OSError:\n            # \u7aef\u53e3\u5df2\u88ab\u5360\u7528\n            pass\n    print(&quot;\u6ca1\u6709\u627e\u5230\u53ef\u7528\u7aef\u53e3&quot;)\n    return None\n\n# \u67e5\u627e\u53ef\u7528\u7aef\u53e3\nport = find_free_port()\nif port:\n    print(f&quot;\u4f7f\u7528\u53ef\u7528\u7aef\u53e3: {port}&quot;)\n\n# 5. \u7f51\u7edc\u4e0d\u7a33\u5b9a\u89e3\u51b3\u65b9\u6848\nprint(&quot;\/n\u7f51\u7edc\u4e0d\u7a33\u5b9a\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\nclass ReconnectingSocket:\n    &quot;&quot;&quot;\u652f\u6301\u81ea\u52a8\u91cd\u8fde\u7684\u5957\u63a5\u5b57&quot;&quot;&quot;\n    def __init__(self, host, port, max_retries=3, timeout=3):\n        self.host = host\n        self.port = port\n        self.max_retries = max_retries\n        self.timeout = timeout\n        self.sock = None\n        self.connect()\n    \n    def connect(self):\n        &quot;&quot;&quot;\u8fde\u63a5\u670d\u52a1\u5668&quot;&quot;&quot;\n        for i in range(self.max_retries):\n            try:\n                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n                self.sock.settimeout(self.timeout)\n                self.sock.connect((self.host, self.port))\n                print(f&quot;\u8fde\u63a5 {self.host}:{self.port} \u6210\u529f&quot;)\n                return True\n            except Exception as e:\n                print(f&quot;\u8fde\u63a5 {self.host}:{self.port} \u5931\u8d25: {e}&quot;)\n                time.sleep(1)\n        return False\n    \n    def send(self, data):\n        &quot;&quot;&quot;\u53d1\u9001\u6570\u636e&quot;&quot;&quot;\n        try:\n            if self.sock:\n                self.sock.sendall(data)\n                return True\n        except Exception as e:\n            print(f&quot;\u53d1\u9001\u6570\u636e\u5931\u8d25: {e}&quot;)\n            # \u5c1d\u8bd5\u91cd\u8fde\n            if self.connect():\n                self.sock.sendall(data)\n                return True\n        return False\n    \n    def recv(self, buffer_size=1024):\n        &quot;&quot;&quot;\u63a5\u6536\u6570\u636e&quot;&quot;&quot;\n        try:\n            if self.sock:\n                return self.sock.recv(buffer_size)\n        except Exception as e:\n            print(f&quot;\u63a5\u6536\u6570\u636e\u5931\u8d25: {e}&quot;)\n            # \u5c1d\u8bd5\u91cd\u8fde\n            self.connect()\n        return b&quot;&quot;\n    \n    def close(self):\n        &quot;&quot;&quot;\u5173\u95ed\u8fde\u63a5&quot;&quot;&quot;\n        if self.sock:\n            try:\n                self.sock.close()\n            except Exception:\n                pass\n        print(&quot;\u8fde\u63a5\u5df2\u5173\u95ed&quot;)\n\n# \u6d4b\u8bd5\u81ea\u52a8\u91cd\u8fde\ndef test_reconnecting_socket():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u81ea\u52a8\u91cd\u8fde&quot;&quot;&quot;\n    rsock = ReconnectingSocket(&quot;www.baidu.com&quot;, 80)\n    \n    # \u53d1\u9001\u6570\u636e\n    request = &quot;GET \/ HTTP\/1.1\/r\/n&quot;\n    request += &quot;Host: www.baidu.com\/r\/n&quot;\n    request += &quot;Connection: close\/r\/n&quot;\n    request += &quot;\/r\/n&quot;\n    \n    if rsock.send(request.encode('utf-8')):\n        # \u63a5\u6536\u6570\u636e\n        data = rsock.recv(1024)\n        print(f&quot;\u6536\u5230\u6570\u636e: {data.decode('utf-8')[:100]}...&quot;)\n    \n    # \u5173\u95ed\u8fde\u63a5\n    rsock.close()\n\n# \u6d4b\u8bd5\u81ea\u52a8\u91cd\u8fde\ntest_reconnecting_socket()\n\n# 6. \u6027\u80fd\u4f18\u5316\u89e3\u51b3\u65b9\u6848\nprint(&quot;\/n\u6027\u80fd\u4f18\u5316\u89e3\u51b3\u65b9\u6848\uff1a&quot;)\n\n# \u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u6027\u80fd\ndef test_buffer_size():\n    &quot;&quot;&quot;\u6d4b\u8bd5\u4e0d\u540c\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u6027\u80fd&quot;&quot;&quot;\n    host, port = &quot;www.baidu.com&quot;, 80\n    buffer_sizes = [1024, 2048, 4096, 8192, 16384]\n    \n    for buffer_size in buffer_sizes:\n        start_time = time.time()\n        \n        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n            sock.connect((host, port))\n            \n            # \u53d1\u9001HTTP\u8bf7\u6c42\n            request = &quot;GET \/ HTTP\/1.1\/r\/n&quot;\n            request += f&quot;Host: {host}\/r\/n&quot;\n            request += &quot;Connection: close\/r\/n&quot;\n            request += &quot;\/r\/n&quot;\n            sock.sendall(request.encode('utf-8'))\n            \n            # \u63a5\u6536\u54cd\u5e94\n            data = b&quot;&quot;\n            while True:\n                part = sock.recv(buffer_size)\n                if not part:\n                    break\n                data += part\n        \n        elapsed_time = time.time() - start_time\n        print(f&quot;\u7f13\u51b2\u533a\u5927\u5c0f {buffer_size} \u5b57\u8282: {elapsed_time:.4f} \u79d2&quot;)\n\n# \u6d4b\u8bd5\u7f13\u51b2\u533a\u5927\u5c0f\ntest_buffer_size()\n\n# 7. \u603b\u7ed3\n\n\u672c\u6587\u8be6\u7ec6\u5206\u6790\u4e86Python\u4e2d\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u4e0e\u5957\u63a5\u5b57API\uff0c\u5305\u62ec\uff1a\n\n- **\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5**\uff1a\u7f51\u7edc\u534f\u8bae\u3001\u7f51\u7edc\u6a21\u578b\u3001\u5957\u63a5\u5b57\u3001\u7f51\u7edc\u5730\u5740\n- **\u5957\u63a5\u5b57API\u7684\u4f7f\u7528**\uff1a\u521b\u5efa\u5957\u63a5\u5b57\u3001\u7ed1\u5b9a\u5730\u5740\u3001\u76d1\u542c\u8fde\u63a5\u3001\u63a5\u53d7\u8fde\u63a5\u3001\u53d1\u8d77\u8fde\u63a5\u3001\u53d1\u9001\u6570\u636e\u3001\u63a5\u6536\u6570\u636e\u3001\u5173\u95ed\u8fde\u63a5\n- **\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b**\uff1a\u963b\u585e\u5f0fI\/O\u6a21\u578b\u3001\u975e\u963b\u585e\u5f0fI\/O\u6a21\u578b\u3001\u591a\u8def\u590d\u7528I\/O\u6a21\u578b\u3001\u4fe1\u53f7\u9a71\u52a8I\/O\u6a21\u578b\u3001\u5f02\u6b65I\/O\u6a21\u578b\n- **\u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b**\uff1asocketserver\u6a21\u5757\u3001http\u6a21\u5757\u3001urllib\u6a21\u5757\u3001requests\u5e93\u3001asyncio\u5e93\n- **\u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5**\uff1a\u9519\u8bef\u5904\u7406\u3001\u8d85\u65f6\u8bbe\u7f6e\u3001\u8d44\u6e90\u7ba1\u7406\u3001\u5e76\u53d1\u5904\u7406\u3001\u5b89\u5168\u8003\u8651\u3001\u6027\u80fd\u4f18\u5316\u3001\u4ee3\u7801\u7ec4\u7ec7\n- **\u5e38\u89c1\u7f51\u7edc\u7f16\u7a0b\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848**\uff1a\u8fde\u63a5\u8d85\u65f6\u3001\u8fde\u63a5\u88ab\u62d2\u7edd\u3001DNS\u89e3\u6790\u5931\u8d25\u3001SSL\u8bc1\u4e66\u9519\u8bef\u3001\u6570\u636e\u4f20\u8f93\u4e0d\u5b8c\u6574\u3001\u5e76\u53d1\u8fde\u63a5\u6570\u9650\u5236\u3001\u7aef\u53e3\u5360\u7528\u3001\u7f51\u7edc\u4e0d\u7a33\u5b9a\u3001\u9632\u706b\u5899\u9650\u5236\u3001\u6027\u80fd\u95ee\u9898\n\nPython\u7684\u7f51\u7edc\u7f16\u7a0b\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u5404\u79cd\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u7b80\u5355\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5e94\u7528\u5230\u590d\u6742\u7684Web\u670d\u52a1\u5668\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5e94\u8be5\u80fd\u591f\uff1a\n\n1. \u7406\u89e3\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u548c\u539f\u7406\n2. \u638c\u63e1\u5957\u63a5\u5b57API\u7684\u4f7f\u7528\u65b9\u6cd5\n3. \u4e86\u89e3\u4e0d\u540c\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u53ca\u5176\u9002\u7528\u573a\u666f\n4. \u719f\u7ec3\u4f7f\u7528Python\u7684\u7f51\u7edc\u5e93\u548c\u5de5\u5177\n5. \u9075\u5faa\u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\n6. \u89e3\u51b3\u5e38\u89c1\u7684\u7f51\u7edc\u7f16\u7a0b\u95ee\u9898\n7. \u4f18\u5316\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\n\n\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u548c\u6280\u672f\uff0c\u9075\u5faaPython\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002\u540c\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u4fdd\u6301\u5b66\u4e60\u7684\u6001\u5ea6\uff0c\u5173\u6ce8Python\u7684\u6700\u65b0\u53d1\u5c55\uff0c\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u3002\n\n## 8. \u53c2\u8003\u6587\u732e\n\n1. Python Documentation: socket \u2014 Low-level networking interface\n2. Python Documentation: socketserver \u2014 A framework for network servers\n3. Python Documentation: http \u2014 HTTP modules\n4. Python Documentation: urllib \u2014 URL handling modules\n5. Python Documentation: ssl \u2014 TLS\/SSL wrapper for socket objects\n6. Python Documentation: asyncio \u2014 Asynchronous I\/O\n7. Real Python: Python Socket Programming Guide\n8. Real Python: Python Networking Basics\n9. Real Python: A Guide to Python's socket Module\n10. MDN Web Docs: HTTP Overview\n\n## 9. \u7ed3\u8bed\n\nPython\u7684\u7f51\u7edc\u7f16\u7a0b\u662fPython\u7f16\u7a0b\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86Python\u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u6280\u672f\u3002\n\n\u5728\u7f16\u5199Python\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\uff1a\n\n- **\u6b63\u786e\u7406\u89e3\u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5**\uff1a\u4e86\u89e3\u7f51\u7edc\u534f\u8bae\u3001\u7f51\u7edc\u6a21\u578b\u3001\u5957\u63a5\u5b57\u7b49\u57fa\u672c\u6982\u5ff5\n- **\u9009\u62e9\u5408\u9002\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b**\uff1a\u6839\u636e\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\n- **\u4f7f\u7528\u5408\u9002\u7684\u7f51\u7edc\u5e93**\uff1a\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7f51\u7edc\u5e93\u548c\u5de5\u5177\n- **\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406**\uff1a\u6355\u83b7\u548c\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\n- **\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4**\uff1a\u907f\u514d\u7a0b\u5e8f\u65e0\u9650\u671f\u963b\u585e\n- **\u6b63\u786e\u7ba1\u7406\u7f51\u7edc\u8d44\u6e90**\uff1a\u786e\u4fdd\u7f51\u7edc\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\n- **\u8003\u8651\u5b89\u5168\u56e0\u7d20**\uff1a\u5b9e\u73b0\u9002\u5f53\u7684\u5b89\u5168\u63aa\u65bd\n- **\u4f18\u5316\u6027\u80fd**\uff1a\u6839\u636e\u9700\u8981\u4f18\u5316\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\n- **\u7ec4\u7ec7\u597d\u4ee3\u7801**\uff1a\u4fdd\u6301\u4ee3\u7801\u7684\u6e05\u6670\u548c\u53ef\u7ef4\u62a4\u6027\n\n\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u539f\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u7f51\u7edc\u7f16\u7a0b\u529f\u80fd\uff0c\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u3001\u9ad8\u6548\u548c\u53ef\u7ef4\u62a4\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u7f51\u7edc\u7f16\u7a0b\u4e0d\u4ec5\u662f\u4e00\u79cd\u57fa\u672c\u7684\u7f16\u7a0b\u6280\u80fd\uff0c\u66f4\u662f\u4e00\u79cd\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u5728Web\u5f00\u53d1\u3001\u5206\u5e03\u5f0f\u7cfb\u7edf\u3001\u7f51\u7edc\u5de5\u5177\u7b49\u65b9\u9762\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\n\n\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3Python\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u4e0e\u5957\u63a5\u5b57API\uff0c\u638c\u63e1\u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ece\u800c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7f16\u5199\u51fa\u66f4\u9ad8\u8d28\u91cf\u7684Python\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585264\">https:\/\/segmentfault.com\/a\/1190000047585264<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u7f51\u7edc\u7f16\u7a0b\u6a21\u578b\u4e0e\u5957\u63a5\u5b57API 1. \u7f51\u7edc\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5 \u5728Python\u7f16\u7a0b\u4e2d\uff0c\u7f51\u7edc\u7f16\u7a0b\u662f\u4e00\u79cd\u5e38\u89c1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021227","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021227"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021227\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}