{"id":1001976,"date":"2024-12-27T10:04:46","date_gmt":"2024-12-27T02:04:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001976.html"},"modified":"2024-12-27T10:04:48","modified_gmt":"2024-12-27T02:04:48","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e5%ae%9e%e7%8e%b0%e9%80%9a%e8%ae%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001976.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u5b9e\u73b0\u901a\u8baf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075921\/d17ca075-aa0a-49d1-877b-78a89e363b93.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u5b9e\u73b0\u901a\u8baf\" \/><\/p>\n<p><p> <strong>\u5229\u7528Python\u5b9e\u73b0\u901a\u8baf\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528Socket\u7f16\u7a0b\u3001\u5229\u7528HTTP\u534f\u8bae\u3001\u4f7f\u7528WebSocket\u3001\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\u5982Twisted\u7b49\u3002<\/strong>\u5176\u4e2d\uff0cSocket\u7f16\u7a0b\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u5b9e\u73b0TCP\/IP\u534f\u8bae\u7684\u901a\u8baf\u3002\u5728\u8fd9\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u5bf9Socket\u7f16\u7a0b\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002\u901a\u8fc7Socket\u7f16\u7a0b\uff0cPython\u53ef\u4ee5\u521b\u5efa\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u7f51\u7edc\u4e0a\u7684\u6570\u636e\u4ea4\u6362\u3002Socket\u7f16\u7a0b\u63d0\u4f9b\u4e86\u5e95\u5c42\u7684API\u63a5\u53e3\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u901a\u8baf\u8fc7\u7a0b\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u548c\u81ea\u5b9a\u4e49\u534f\u8bae\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SOCKET\u7f16\u7a0b\u5b9e\u73b0\u901a\u8baf<\/p>\n<\/p>\n<p><p>Socket\u7f16\u7a0b\u662f\u7f51\u7edc\u901a\u8baf\u7684\u57fa\u7840\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u8bbe\u5907\u4e0a\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002Python\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684Socket\u7f16\u7a0b\u63a5\u53e3\uff0c\u4f7f\u5f97\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u53d8\u5f97\u7b80\u5355\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001Socket\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>Socket\u662f\u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3\uff0c\u5b83\u63d0\u4f9b\u4e86\u7f51\u7edc\u901a\u8baf\u7684\u7aef\u70b9\u3002\u53ef\u4ee5\u5c06Socket\u770b\u4f5c\u662f\u4e00\u4e2a\u901a\u8baf\u7684\u201c\u63d2\u5ea7\u201d\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5b83\u8fdb\u884c\u6570\u636e\u7684\u8bfb\u5199\u3002Socket\u7f16\u7a0b\u6d89\u53ca\u5230\u4e24\u4e2a\u4e3b\u8981\u7aef\u70b9\uff1a\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u3002\u670d\u52a1\u5668\u7aef\u8d1f\u8d23\u76d1\u542c\u7279\u5b9a\u7aef\u53e3\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\uff1b\u5ba2\u6237\u7aef\u5219\u4e3b\u52a8\u8fde\u63a5\u5230\u670d\u52a1\u5668\u7aef\u7684\u6307\u5b9a\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u521b\u5efaSocket<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u521b\u5efaSocket\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>socket<\/code>\u6a21\u5757\u4e2d\u7684<code>socket()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2aSocket\u5bf9\u8c61\u3002Socket\u53ef\u4ee5\u5206\u4e3a\u6d41\u5f0fSocket\uff08TCP\uff09\u548c\u6570\u636e\u62a5Socket\uff08UDP\uff09\uff0c\u6d41\u5f0fSocket\u63d0\u4f9b\u9762\u5411\u8fde\u63a5\u7684\u3001\u53ef\u9760\u7684\u670d\u52a1\uff0c\u800c\u6570\u636e\u62a5Socket\u5219\u63d0\u4f9b\u65e0\u8fde\u63a5\u7684\u3001\u5c3d\u529b\u800c\u4e3a\u7684\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efaTCP\/IP Socket<\/strong><\/h2>\n<p>server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u7ed1\u5b9aSocket\u5230\u5730\u5740\u548c\u7aef\u53e3<\/strong><\/h2>\n<p>server_socket.bind((&#39;localhost&#39;, 8080))<\/p>\n<h2><strong>\u5f00\u59cb\u76d1\u542c\u8fde\u63a5<\/strong><\/h2>\n<p>server_socket.listen(5)<\/p>\n<p>print(&quot;\u670d\u52a1\u5668\u6b63\u5728\u7b49\u5f85\u8fde\u63a5...&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.3\u3001\u63a5\u53d7\u548c\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u670d\u52a1\u5668\u9700\u8981\u4e0d\u65ad\u5730\u63a5\u53d7\u548c\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7<code>accept()<\/code>\u65b9\u6cd5\u6765\u63a5\u53d7\u8fde\u63a5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u963b\u585e\u7a0b\u5e8f\uff0c\u76f4\u5230\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u8fdb\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<p>client_socket, client_address = server_socket.accept()<\/p>\n<p>print(f&quot;\u8fde\u63a5\u6765\u81ea: {client_address}&quot;)<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e<\/strong><\/h2>\n<p>data = client_socket.recv(1024)<\/p>\n<p>print(f&quot;\u63a5\u6536\u5230\u7684\u6570\u636e: {data.decode(&#39;utf-8&#39;)}&quot;)<\/p>\n<h2><strong>\u53d1\u9001\u6570\u636e<\/strong><\/h2>\n<p>client_socket.sendall(&quot;\u6b22\u8fce\u8fde\u63a5\u5230\u670d\u52a1\u5668&quot;.encode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>client_socket.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.4\u3001\u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u5668<\/p>\n<\/p>\n<p><p>\u5ba2\u6237\u7aef\u9700\u8981\u4e3b\u52a8\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff0c\u4f7f\u7528<code>connect()<\/code>\u65b9\u6cd5\u6765\u8fde\u63a5\u6307\u5b9a\u7684\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaTCP\/IP Socket<\/p>\n<p>client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/strong><\/h2>\n<p>client_socket.connect((&#39;localhost&#39;, 8080))<\/p>\n<h2><strong>\u53d1\u9001\u6570\u636e<\/strong><\/h2>\n<p>client_socket.sendall(&quot;\u4f60\u597d\uff0c\u670d\u52a1\u5668&quot;.encode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e<\/strong><\/h2>\n<p>response = client_socket.recv(1024)<\/p>\n<p>print(f&quot;\u4ece\u670d\u52a1\u5668\u63a5\u6536\u5230\u7684\u6570\u636e: {response.decode(&#39;utf-8&#39;)}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>client_socket.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528HTTP\u534f\u8bae\u5b9e\u73b0\u901a\u8baf<\/p>\n<\/p>\n<p><p>HTTP\u534f\u8bae\u662f\u7f51\u7edc\u901a\u8baf\u4e2d\u6700\u5e38\u7528\u7684\u534f\u8bae\u4e4b\u4e00\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0HTTP\u901a\u8baf\uff0c\u4f8b\u5982\u4f7f\u7528<code>http.client<\/code>\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93<code>requests<\/code>\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001\u4f7f\u7528http.client\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>http.client<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684HTTP\u5ba2\u6237\u7aef\u5e93\uff0c\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u548c\u63a5\u6536\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import http.client<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>connection = http.client.HTTPConnection(&#39;www.example.com&#39;)<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>connection.request(&#39;GET&#39;, &#39;\/&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u54cd\u5e94<\/strong><\/h2>\n<p>response = connection.getresponse()<\/p>\n<p>print(f&quot;\u72b6\u6001: {response.status}, \u539f\u56e0: {response.reason}&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>content = response.read()<\/p>\n<p>print(f&quot;\u5185\u5bb9: {content.decode(&#39;utf-8&#39;)}&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2\u3001\u4f7f\u7528requests\u5e93<\/p>\n<\/p>\n<p><p><code>requests<\/code>\u662fPython\u4e2d\u6700\u53d7\u6b22\u8fce\u7684HTTP\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u63a5\u53e3\uff0c\u9002\u5408\u8fdb\u884c\u5404\u79cdHTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;http:\/\/www.example.com&#39;)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u72b6\u6001\u7801<\/strong><\/h2>\n<p>print(f&quot;\u72b6\u6001\u7801: {response.status_code}&quot;)<\/p>\n<h2><strong>\u6253\u5370\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>print(f&quot;\u5185\u5bb9: {response.text}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528WebSocket\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf<\/p>\n<\/p>\n<p><p>WebSocket\u662f\u4e00\u79cd\u5728\u5355\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u8baf\u7684\u534f\u8bae\uff0c\u9002\u5408\u7528\u4e8e\u5b9e\u73b0\u5b9e\u65f6\u7684\u901a\u8baf\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001WebSocket\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>WebSocket\u901a\u8fc7\u4e00\u6b21\u63e1\u624b\u4fbf\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5efa\u7acb\u6301\u4e45\u8fde\u63a5\uff0c\u5141\u8bb8\u53cc\u65b9\u968f\u65f6\u53d1\u9001\u6570\u636e\u3002WebSocket\u534f\u8bae\u5728HTTP\u534f\u8bae\u4e4b\u4e0a\u8fdb\u884c\u5347\u7ea7\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u901a\u8baf\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>3.2\u3001\u4f7f\u7528websockets\u5e93<\/p>\n<\/p>\n<p><p>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>websockets<\/code>\u5e93\u6765\u5b9e\u73b0WebSocket\u901a\u8baf\uff0c\u8be5\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import websockets<\/p>\n<p>async def echo(websocket, path):<\/p>\n<p>    async for message in websocket:<\/p>\n<p>        aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t websocket.send(message)<\/p>\n<h2><strong>\u542f\u52a8WebSocket\u670d\u52a1\u5668<\/strong><\/h2>\n<p>start_server = websockets.serve(echo, &quot;localhost&quot;, 8765)<\/p>\n<h2><strong>\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>asyncio.get_event_loop().run_until_complete(start_server)<\/p>\n<p>asyncio.get_event_loop().run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.3\u3001\u5ba2\u6237\u7aef\u8fde\u63a5WebSocket\u670d\u52a1\u5668<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u540c\u6837\u7684<code>websockets<\/code>\u5e93\u6765\u5b9e\u73b0WebSocket\u5ba2\u6237\u7aef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import websockets<\/p>\n<p>async def hello():<\/p>\n<p>    uri = &quot;ws:\/\/localhost:8765&quot;<\/p>\n<p>    async with websockets.connect(uri) as websocket:<\/p>\n<p>        await websocket.send(&quot;\u4f60\u597d\uff0cWebSocket\u670d\u52a1\u5668&quot;)<\/p>\n<p>        response = await websocket.recv()<\/p>\n<p>        print(f&quot;\u63a5\u6536\u5230\u7684\u6d88\u606f: {response}&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>asyncio.get_event_loop().run_until_complete(hello())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5b9e\u73b0\u590d\u6742\u901a\u8baf<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u63d0\u5230\u7684\u51e0\u79cd\u65b9\u5f0f\uff0cPython\u4e2d\u8fd8\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u590d\u6742\u7684\u901a\u8baf\u9700\u6c42\uff0c\u5982Twisted\u3001ZeroMQ\u3001gRPC\u7b49\u3002<\/p>\n<\/p>\n<p><p>4.1\u3001\u4f7f\u7528Twisted\u5b9e\u73b0\u5f02\u6b65\u901a\u8baf<\/p>\n<\/p>\n<p><p>Twisted\u662f\u4e00\u4e2a\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u5f15\u64ce\u6846\u67b6\uff0c\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u9002\u5408\u4e8e\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from twisted.internet import reactor, protocol<\/p>\n<p>class Echo(protocol.Protocol):<\/p>\n<p>    def dataReceived(self, data):<\/p>\n<p>        self.transport.write(data)<\/p>\n<p>class EchoFactory(protocol.Factory):<\/p>\n<p>    def buildProtocol(self, addr):<\/p>\n<p>        return Echo()<\/p>\n<h2><strong>\u76d1\u542cTCP\u7aef\u53e3<\/strong><\/h2>\n<p>reactor.listenTCP(8000, EchoFactory())<\/p>\n<p>reactor.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4.2\u3001\u4f7f\u7528ZeroMQ\u5b9e\u73b0\u5206\u5e03\u5f0f\u901a\u8baf<\/p>\n<\/p>\n<p><p>ZeroMQ\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u901a\u8baf\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6d88\u606f\u6a21\u5f0f\uff0c\u9002\u5408\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zmq<\/p>\n<h2><strong>\u521b\u5efa\u4e0a\u4e0b\u6587\u548c\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>context = zmq.Context()<\/p>\n<p>socket = context.socket(zmq.REP)<\/p>\n<p>socket.bind(&quot;tcp:\/\/*:5555&quot;)<\/p>\n<p>while True:<\/p>\n<p>    # \u7b49\u5f85\u63a5\u6536\u6d88\u606f<\/p>\n<p>    message = socket.recv()<\/p>\n<p>    print(f&quot;\u63a5\u6536\u5230\u6d88\u606f: {message}&quot;)<\/p>\n<p>    # \u53d1\u9001\u54cd\u5e94<\/p>\n<p>    socket.send(b&quot;\u6d88\u606f\u5df2\u6536\u5230&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0cPython\u5f00\u53d1\u8005\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9e\u73b0\u5404\u79cd\u7f51\u7edc\u901a\u8baf\u5e94\u7528\uff0c\u4ece\u7b80\u5355\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u67b6\u6784\u5230\u590d\u6742\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u901a\u8baf\u9700\u6c42\u3002\u65e0\u8bba\u662f\u4f7f\u7528Socket\u7f16\u7a0b\u5b9e\u73b0\u5e95\u5c42\u63a7\u5236\uff0c\u8fd8\u662f\u5229\u7528HTTP\u548cWebSocket\u534f\u8bae\u5b9e\u73b0\u9ad8\u7ea7\u5e94\u7528\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u652f\u6301\u901a\u8baf\u5f00\u53d1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u7f51\u7edc\u901a\u8baf\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u5b9e\u73b0\u7f51\u7edc\u901a\u8baf\u65f6\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0cPython\u4e2d\u5e38\u7528\u7684\u6709socket\u3001asyncio\u548cwebsocket\u7b49\u3002\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684\u57fa\u672c\u7ed3\u6784\uff0c\u670d\u52a1\u5668\u76d1\u542c\u7279\u5b9a\u7aef\u53e3\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u5ba2\u6237\u7aef\u901a\u8fc7\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u8fdb\u884c\u8fde\u63a5\u3002\u901a\u8fc7socket\u5e93\uff0c\u4f60\u53ef\u4ee5\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\uff0c\u5904\u7406\u8fde\u63a5\u7684\u5173\u95ed\u548c\u9519\u8bef\u7b49\u3002\u6b64\u5916\uff0c\u4e86\u89e3TCP\u548cUDP\u534f\u8bae\u7684\u533a\u522b\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u901a\u8baf\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u5bc6\u6280\u672f\uff0c\u5982SSL\/TLS\u6765\u52a0\u5bc6\u6570\u636e\u4f20\u8f93\u3002Python\u7684ssl\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u6b64\u5916\uff0c\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\uff08\u5982SHA-256\uff09\u6765\u751f\u6210\u6570\u636e\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u5728\u63a5\u6536\u7aef\u8fdb\u884c\u6bd4\u5bf9\u3002\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u88ab\u7a83\u53d6\u6216\u7be1\u6539\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u901a\u8baf\u65f6\uff0c\u5982\u4f55\u5904\u7406\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u591a\u7ebf\u7a0b\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528threading\u6a21\u5757\u6765\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u5bf9\u4e8e\u5f02\u6b65\u64cd\u4f5c\uff0casyncio\u5e93\u662f\u4e00\u4e2a\u4f18\u79c0\u7684\u9009\u62e9\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u5355\u7ebf\u7a0b\u4e2d\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u901a\u8fc7async\u548cawait\u5173\u952e\u5b57\u6765\u7b80\u5316\u4ee3\u7801\u7ed3\u6784\u3002\u8fd9\u6837\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u8fd8\u80fd\u6709\u6548\u7ba1\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u5b9e\u73b0\u901a\u8baf\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528Socket\u7f16\u7a0b\u3001\u5229\u7528HTTP\u534f\u8bae\u3001\u4f7f\u7528WebSocket\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1001983,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001976"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1001976"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001976\/revisions"}],"predecessor-version":[{"id":1001984,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001976\/revisions\/1001984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001983"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}