{"id":10021264,"date":"2026-02-20T14:46:34","date_gmt":"2026-02-20T06:46:34","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021264"},"modified":"2026-02-20T14:46:34","modified_gmt":"2026-02-20T06:46:34","slug":"python-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%ef%bc%9asocket-tcp-udp-%e5%8d%8f%e8%ae%ae%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021264","title":{"rendered":"Python \u7f51\u7edc\u7f16\u7a0b\uff1aSocket\/TCP\/UDP \u534f\u8bae\u5b9e\u73b0"},"content":{"rendered":"<h2>Python \u7f51\u7edc\u7f16\u7a0b\uff1aSocket\/TCP\/UDP \u534f\u8bae\u5b9e\u73b0<\/h2>\n<p>\u7f51\u7edc\u7f16\u7a0b\u662f Python \u7684\u91cd\u8981\u5e94\u7528\u9886\u57df\uff0cSocket \u7f16\u7a0b\u5b9e\u73b0\u4e86\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u7840\u529f\u80fd\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 Python \u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3\u6280\u672f\uff0c\u5305\u62ec Socket \u7f16\u7a0b\u3001TCP \u534f\u8bae\u3001UDP \u534f\u8bae\u7b49\u3002<\/p>\n<h3>Socket \u57fa\u7840<\/h3>\n<p>Socket \u662f\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u672c\u5355\u5143\uff0c\u63d0\u4f9b\u4e86\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u63a5\u53e3\u3002<\/p>\n<pre><code class=\"python\">import socket\n\ndef socket_basic_demo():\n    print(&quot;Socket \u57fa\u7840\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u521b\u5efa Socket&quot;)\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    \n    print(f&quot;Socket \u5bb6\u65cf: {sock.family}&quot;)\n    print(f&quot;Socket \u7c7b\u578b: {sock.type}&quot;)\n    \n    sock.close()\n\nsocket_basic_demo()<\/code><\/pre>\n<h3>TCP \u670d\u52a1\u5668<\/h3>\n<p>TCP \u670d\u52a1\u5668\u63d0\u4f9b\u53ef\u9760\u7684\u9762\u5411\u8fde\u63a5\u7684\u670d\u52a1\u3002<\/p>\n<pre><code class=\"python\">def tcp_server_demo():\n    print(&quot;TCP \u670d\u52a1\u5668\u6f14\u793a:&quot;)\n    \n    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server_socket.bind(('localhost', 12345))\n    server_socket.listen(5)\n    \n    print(&quot;\u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u8fde\u63a5...&quot;)\n    \n    while True:\n        client_socket, address = server_socket.accept()\n        print(f&quot;\u5ba2\u6237\u7aef\u8fde\u63a5: {address}&quot;)\n        \n        data = client_socket.recv(1024)\n        print(f&quot;\u63a5\u6536\u6570\u636e: {data.decode()}&quot;)\n        \n        client_socket.send(b&quot;Hello Client!&quot;)\n        client_socket.close()\n\ntcp_server_demo()<\/code><\/pre>\n<h3>TCP \u5ba2\u6237\u7aef<\/h3>\n<p>TCP \u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u670d\u52a1\u5668\u5e76\u53d1\u9001\u6570\u636e\u3002<\/p>\n<pre><code class=\"python\">def tcp_client_demo():\n    print(&quot;TCP \u5ba2\u6237\u7aef\u6f14\u793a:&quot;)\n    \n    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    client_socket.connect(('localhost', 12345))\n    \n    client_socket.send(b&quot;Hello Server!&quot;)\n    \n    data = client_socket.recv(1024)\n    print(f&quot;\u63a5\u6536\u6570\u636e: {data.decode()}&quot;)\n    \n    client_socket.close()\n\ntcp_client_demo()<\/code><\/pre>\n<h3>UDP \u670d\u52a1\u5668<\/h3>\n<p>UDP \u670d\u52a1\u5668\u63d0\u4f9b\u65e0\u8fde\u63a5\u7684\u6570\u636e\u62a5\u670d\u52a1\u3002<\/p>\n<pre><code class=\"python\">def udp_server_demo():\n    print(&quot;UDP \u670d\u52a1\u5668\u6f14\u793a:&quot;)\n    \n    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n    server_socket.bind(('localhost', 12345))\n    \n    print(&quot;\u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u6570\u636e...&quot;)\n    \n    while True:\n        data, address = server_socket.recvfrom(1024)\n        print(f&quot;\u63a5\u6536\u6765\u81ea {address} \u7684\u6570\u636e: {data.decode()}&quot;)\n        \n        server_socket.sendto(b&quot;Hello Client!&quot;, address)\n\nudp_server_demo()<\/code><\/pre>\n<h3>UDP \u5ba2\u6237\u7aef<\/h3>\n<p>UDP \u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u62a5\u5230\u670d\u52a1\u5668\u3002<\/p>\n<pre><code class=\"python\">def udp_client_demo():\n    print(&quot;UDP \u5ba2\u6237\u7aef\u6f14\u793a:&quot;)\n    \n    client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n    \n    client_socket.sendto(b&quot;Hello Server!&quot;, ('localhost', 12345))\n    \n    data, address = client_socket.recvfrom(1024)\n    print(f&quot;\u63a5\u6536\u6570\u636e: {data.decode()}&quot;)\n    \n    client_socket.close()\n\nudp_client_demo()<\/code><\/pre>\n<h3>\u7f51\u7edc\u7f16\u7a0b\u67b6\u6784<\/h3>\n<pre style=\"display:none;\"><code class=\"mermaid\">graph TD\n    A[\u5e94\u7528\u5c42] --&gt; B[\u4f20\u8f93\u5c42]\n    B --&gt; C[\u7f51\u7edc\u5c42]\n    C --&gt; D[\u6570\u636e\u94fe\u8def\u5c42]\n    D --&gt; E[\u7269\u7406\u5c42]\n    B --&gt; F[TCP]\n    B --&gt; G[UDP]\n    F --&gt; H[\u53ef\u9760\u4f20\u8f93]\n    G --&gt; I[\u4e0d\u53ef\u9760\u4f20\u8f93]<\/code><\/pre>\n<h3>\u975e\u963b\u585e Socket<\/h3>\n<p>\u975e\u963b\u585e Socket \u5141\u8bb8\u7a0b\u5e8f\u5728\u7b49\u5f85 I\/O \u65f6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"python\">def nonblocking_socket_demo():\n    print(&quot;\u975e\u963b\u585e Socket \u6f14\u793a:&quot;)\n    \n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock.setblocking(False)\n    \n    try:\n        sock.connect(('localhost', 12345))\n    except BlockingIOError:\n        print(&quot;\u8fde\u63a5\u8fdb\u884c\u4e2d...&quot;)\n    \n    sock.close()\n\nnonblocking_socket_demo()<\/code><\/pre>\n<h3>\u591a\u8def\u590d\u7528<\/h3>\n<p>\u591a\u8def\u590d\u7528\u540c\u65f6\u76d1\u63a7\u591a\u4e2a Socket\u3002<\/p>\n<pre><code class=\"python\">import select\n\ndef multiplexing_demo():\n    print(&quot;\u591a\u8def\u590d\u7528\u6f14\u793a:&quot;)\n    \n    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server_socket.bind(('localhost', 12345))\n    server_socket.listen(5)\n    server_socket.setblocking(False)\n    \n    inputs = [server_socket]\n    \n    while inputs:\n        readable, _, _ = select.select(inputs, [], [])\n        \n        for sock in readable:\n            if sock is server_socket:\n                client_socket, address = sock.accept()\n                print(f&quot;\u5ba2\u6237\u7aef\u8fde\u63a5: {address}&quot;)\n                inputs.append(client_socket)\n            else:\n                data = sock.recv(1024)\n                if data:\n                    print(f&quot;\u63a5\u6536\u6570\u636e: {data.decode()}&quot;)\n                else:\n                    inputs.remove(sock)\n                    sock.close()\n\nmultiplexing_demo()<\/code><\/pre>\n<h3>SSL\/TLS \u52a0\u5bc6<\/h3>\n<p>SSL\/TLS \u52a0\u5bc6\u4fdd\u62a4\u7f51\u7edc\u901a\u4fe1\u5b89\u5168\u3002<\/p>\n<pre><code class=\"python\">import ssl\n\ndef ssl_demo():\n    print(&quot;SSL\/TLS \u52a0\u5bc6\u6f14\u793a:&quot;)\n    \n    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)\n    \n    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server_socket.bind(('localhost', 12345))\n    server_socket.listen(5)\n    \n    ssl_socket = context.wrap_socket(server_socket, server_side=True)\n    \n    print(&quot;SSL \u670d\u52a1\u5668\u542f\u52a8&quot;)\n\nssl_demo()<\/code><\/pre>\n<h3>HTTP \u5ba2\u6237\u7aef<\/h3>\n<p>HTTP \u5ba2\u6237\u7aef\u53d1\u9001 HTTP \u8bf7\u6c42\u3002<\/p>\n<pre><code class=\"python\">import urllib.request\n\ndef http_client_demo():\n    print(&quot;HTTP \u5ba2\u6237\u7aef\u6f14\u793a:&quot;)\n    \n    url = 'https:\/\/httpbin.org\/get'\n    \n    response = urllib.request.urlopen(url)\n    data = response.read()\n    \n    print(f&quot;\u72b6\u6001\u7801: {response.status}&quot;)\n    print(f&quot;\u54cd\u5e94\u957f\u5ea6: {len(data)}&quot;)\n\nhttp_client_demo()<\/code><\/pre>\n<h3>WebSocket \u901a\u4fe1<\/h3>\n<p>WebSocket \u63d0\u4f9b\u5168\u53cc\u5de5\u901a\u4fe1\u3002<\/p>\n<pre><code class=\"python\">import asyncio\nimport websockets\n\nasync def websocket_server(websocket, path):\n    async for message in websocket:\n        print(f&quot;\u63a5\u6536\u6d88\u606f: {message}&quot;)\n        await websocket.send(f&quot;Echo: {message}&quot;)\n\nasync def websocket_demo():\n    print(&quot;WebSocket \u901a\u4fe1\u6f14\u793a:&quot;)\n    \n    server = await websockets.serve(websocket_server, 'localhost', 8765)\n    await server.wait_closed()\n\nasyncio.run(websocket_demo())<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>Python \u7f51\u7edc\u7f16\u7a0b\u901a\u8fc7 Socket\u3001TCP\u3001UDP \u7b49\u6280\u672f\uff0c\u5b9e\u73b0\u4e86\u7075\u6d3b\u7684\u7f51\u7edc\u901a\u4fe1\u3002\u638c\u63e1\u8fd9\u4e9b\u7f51\u7edc\u7f16\u7a0b\u6280\u672f\uff0c\u5bf9\u4e8e\u6784\u5efa\u7f51\u7edc\u5e94\u7528\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u901a\u4fe1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u534f\u8bae\u548c\u6280\u672f\uff0c\u5e73\u8861\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002\u826f\u597d\u7684\u7f51\u7edc\u7f16\u7a0b\u5b9e\u8df5\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7684\u901a\u4fe1\u6548\u7387\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047614875\">https:\/\/segmentfault.com\/a\/1190000047614875<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7f51\u7edc\u7f16\u7a0b\uff1aSocket\/TCP\/UDP \u534f\u8bae\u5b9e\u73b0 \u7f51\u7edc\u7f16\u7a0b\u662f Python \u7684\u91cd\u8981\u5e94\u7528\u9886\u57df\uff0cSo [&hellip;]<\/p>\n","protected":false},"author":8,"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-10021264","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\/10021264","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\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021264"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021264\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}