{"id":933095,"date":"2024-12-26T18:10:23","date_gmt":"2024-12-26T10:10:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933095.html"},"modified":"2024-12-26T18:10:26","modified_gmt":"2024-12-26T10:10:26","slug":"python%e5%a6%82%e4%bd%95udp%e5%8f%91%e9%80%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933095.html","title":{"rendered":"python\u5982\u4f55udp\u53d1\u9001"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070653\/79e73c7c-1b6d-46ba-bf16-4575bf8f4d57.webp\" alt=\"python\u5982\u4f55udp\u53d1\u9001\" \/><\/p>\n<p><p> <strong>Python\u901a\u8fc7\u4f7f\u7528socket\u5e93\u6765\u5b9e\u73b0UDP\u6570\u636e\u7684\u53d1\u9001\uff0cUDP\u662f\u4e00\u79cd\u65e0\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u9002\u5408\u4e8e\u9700\u8981\u5feb\u901f\u4f20\u8f93\u6570\u636e\u4e14\u5bf9\u6570\u636e\u5b8c\u6574\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\u3002\u8981\u5728Python\u4e2d\u4f7f\u7528UDP\u53d1\u9001\u6570\u636e\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aUDP\u5957\u63a5\u5b57\uff0c\u6307\u5b9a\u76ee\u6807\u5730\u5740\u548c\u7aef\u53e3\uff0c\u7136\u540e\u4f7f\u7528sendto\u65b9\u6cd5\u53d1\u9001\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u53d1\u9001UDP\u6570\u636e\u76f8\u5bf9\u7b80\u5355\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165socket\u5e93\u3001\u521b\u5efaUDP\u5957\u63a5\u5b57\u3001\u6784\u5efa\u76ee\u6807\u5730\u5740\u3001\u4f7f\u7528sendto\u65b9\u6cd5\u53d1\u9001\u6570\u636e\u3002\u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u6700\u5173\u952e\u7684\u662f\u7406\u89e3UDP\u534f\u8bae\u7684\u65e0\u8fde\u63a5\u7279\u6027\u548c\u5982\u4f55\u6b63\u786e\u5904\u7406\u6570\u636e\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001UDP\u4e0eTCP\u7684\u533a\u522b<\/p>\n<\/p>\n<p><p>UDP\uff08User Datagram Protocol\uff09\u548cTCP\uff08Transmission Control Protocol\uff09\u662f\u4e24\u79cd\u5e38\u7528\u7684\u4f20\u8f93\u5c42\u534f\u8bae\uff0c\u5b83\u4eec\u5404\u81ea\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65e0\u8fde\u63a5\u6027<\/strong>\uff1aUDP\u662f\u65e0\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u8fd9\u610f\u5473\u7740\u53d1\u9001\u6570\u636e\u4e4b\u524d\u4e0d\u9700\u8981\u5efa\u7acb\u8fde\u63a5\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0cTCP\u9700\u8981\u4e09\u6b21\u63e1\u624b\u6765\u5efa\u7acb\u8fde\u63a5\u3002\u8fd9\u4f7f\u5f97UDP\u5728\u9700\u8981\u5feb\u901f\u4f20\u8f93\u6570\u636e\u7684\u573a\u666f\u4e2d\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u4f20\u8f93\u53ef\u9760\u6027<\/strong>\uff1aUDP\u6ca1\u6709\u6570\u636e\u91cd\u4f20\u673a\u5236\uff0c\u6570\u636e\u5305\u53ef\u80fd\u4f1a\u4e22\u5931\u3001\u91cd\u590d\u6216\u4e71\u5e8f\u5230\u8fbe\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u9700\u8981\u9ad8\u53ef\u9760\u6027\u7684\u5e94\u7528\u3002TCP\u5219\u63d0\u4f9b\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\uff0c\u786e\u4fdd\u6570\u636e\u6309\u987a\u5e8f\u5230\u8fbe\u5e76\u65e0\u4e22\u5931\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u4f20\u8f93\u901f\u5ea6<\/strong>\uff1a\u7531\u4e8eUDP\u6ca1\u6709\u8fde\u63a5\u5efa\u7acb\u3001\u7ef4\u62a4\u548c\u5173\u95ed\u7684\u5f00\u9500\uff0c\u4f20\u8f93\u901f\u5ea6\u901a\u5e38\u6bd4TCP\u5feb\uff0c\u9002\u5408\u7528\u4e8e\u5b9e\u65f6\u5e94\u7528\uff0c\u5982\u89c6\u9891\u6d41\u3001\u5728\u7ebf\u6e38\u620f\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9996\u90e8\u5f00\u9500<\/strong>\uff1aUDP\u9996\u90e8\u5f00\u9500\u5c0f\uff0c\u4ec58\u5b57\u8282\uff0c\u800cTCP\u5934\u90e8\u81f3\u5c11\u670920\u5b57\u8282\u3002\u8fd9\u4f7f\u5f97UDP\u66f4\u52a0\u8f7b\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u521b\u5efaUDP\u5957\u63a5\u5b57<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u521b\u5efaUDP\u5957\u63a5\u5b57\u53ef\u4ee5\u4f7f\u7528socket\u5e93\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165socket\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528socket.socket()\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2aUDP\u5957\u63a5\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efaUDP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>AF_INET<\/strong>\uff1a\u8868\u793a\u4f7f\u7528IPv4\u5730\u5740\u3002<\/li>\n<li><strong>SOCK_DGRAM<\/strong>\uff1a\u8868\u793a\u4f7f\u7528UDP\u534f\u8bae\u3002<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u6307\u5b9a\u76ee\u6807\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<\/p>\n<p><p>\u5728UDP\u534f\u8bae\u4e2d\uff0c\u76ee\u6807\u5730\u5740\u548c\u7aef\u53e3\u7528\u4e8e\u6807\u8bc6\u6570\u636e\u5305\u7684\u63a5\u6536\u65b9\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5143\u7ec4\u6765\u8868\u793a\u76ee\u6807\u5730\u5740\u548c\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u76ee\u6807\u5730\u5740\u548c\u7aef\u53e3<\/p>\n<p>target_address = (&quot;127.0.0.1&quot;, 8080)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u53d1\u9001\u6570\u636e<\/p>\n<\/p>\n<p><p>\u4f7f\u7528UDP\u5957\u63a5\u5b57\u7684sendto\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u53d1\u9001\u5230\u6307\u5b9a\u7684\u76ee\u6807\u5730\u5740\u3002sendto\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u53d1\u9001\u7684\u6570\u636e\u548c\u76ee\u6807\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d1\u9001\u6570\u636e<\/p>\n<p>message = &quot;Hello, UDP!&quot;<\/p>\n<p>udp_socket.sendto(message.encode(), target_address)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u7f16\u7801<\/strong>\uff1a\u5728\u53d1\u9001\u6570\u636e\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u683c\u5f0f\u3002\u8fd9\u91cc\u4f7f\u7528\u4e86encode()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76ee\u6807\u5730\u5740<\/strong>\uff1a\u6307\u5b9a\u6570\u636e\u53d1\u9001\u7684\u76ee\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u6570\u636e\u53d1\u9001\u540e\uff0c\u6700\u597d\u5173\u95ed\u5957\u63a5\u5b57\u4ee5\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<p>udp_socket.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684UDP\u6570\u636e\u53d1\u9001\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def udp_send(message, address, port):<\/p>\n<p>    # \u521b\u5efaUDP\u5957\u63a5\u5b57<\/p>\n<p>    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<\/p>\n<p>    try:<\/p>\n<p>        # \u76ee\u6807\u5730\u5740<\/p>\n<p>        target_address = (address, port)<\/p>\n<p>        # \u53d1\u9001\u6570\u636e<\/p>\n<p>        udp_socket.sendto(message.encode(), target_address)<\/p>\n<p>        print(f&quot;Sent message: {message} to {address}:{port}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error occurred: {e}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        # \u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<p>        udp_socket.close()<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>udp_send(&quot;Hello, UDP!&quot;, &quot;127.0.0.1&quot;, 8080)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9e\u65f6\u5e94\u7528<\/strong>\uff1aUDP\u9002\u5408\u7528\u4e8e\u5b9e\u65f6\u6570\u636e\u4f20\u8f93\uff0c\u5982\u89c6\u9891\u6d41\u3001\u8bed\u97f3\u901a\u8bdd\u3001\u5728\u7ebf\u6e38\u620f\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e7f\u64ad\u548c\u591a\u64ad<\/strong>\uff1aUDP\u652f\u6301\u5e7f\u64ad\u548c\u591a\u64ad\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5c06\u6570\u636e\u53d1\u9001\u5230\u591a\u4e2a\u63a5\u6536\u65b9\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5b8c\u6574\u6027<\/strong>\uff1a\u7531\u4e8eUDP\u4e0d\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u987a\u5e8f\uff0c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u81ea\u884c\u5b9e\u73b0\u6570\u636e\u6821\u9a8c\u548c\u91cd\u4f20\u673a\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u73af\u5883<\/strong>\uff1a\u5728\u4e0d\u53ef\u9760\u7684\u7f51\u7edc\u73af\u5883\u4e2d\uff0cUDP\u6570\u636e\u5305\u4e22\u5931\u7684\u6982\u7387\u8f83\u9ad8\uff0c\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0UDP\u6570\u636e\u7684\u53d1\u9001\uff0c\u5e76\u6839\u636e\u5177\u4f53\u5e94\u7528\u9700\u6c42\u8c03\u6574\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>UDP\u53d1\u9001\u5728Python\u4e2d\u9700\u8981\u54ea\u4e9b\u6b65\u9aa4\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fdb\u884cUDP\u53d1\u9001\u901a\u5e38\u9700\u8981\u4f7f\u7528socket\u6a21\u5757\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2aUDP\u5957\u63a5\u5b57\uff0c\u901a\u8fc7\u6307\u5b9aAF_INET\u5730\u5740\u65cf\u548cSOCK_DGRAM\u7c7b\u578b\u6765\u5b9e\u73b0\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528sendto()\u65b9\u6cd5\u5c06\u6570\u636e\u53d1\u9001\u5230\u76ee\u6807IP\u5730\u5740\u548c\u7aef\u53e3\u3002\u786e\u4fdd\u5728\u53d1\u9001\u6570\u636e\u4e4b\u524d\uff0c\u76ee\u6807\u4e3b\u673a\u662f\u53ef\u8fbe\u7684\uff0c\u5e76\u4e14\u9632\u706b\u5899\u8bbe\u7f6e\u5141\u8bb8UDP\u6d41\u91cf\u3002<\/p>\n<p><strong>UDP\u53d1\u9001\u6709\u4ec0\u4e48\u4f18\u52bf\u548c\u52a3\u52bf\uff1f<\/strong><br \/>UDP\u53d1\u9001\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5176\u4f4e\u5ef6\u8fdf\u548c\u9ad8\u6548\u7387\uff0c\u9002\u5408\u5bf9\u5b9e\u65f6\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u5e94\u7528\uff0c\u5982\u89c6\u9891\u6d41\u548c\u5728\u7ebf\u6e38\u620f\u3002\u7136\u800c\uff0cUDP\u6ca1\u6709\u5efa\u7acb\u8fde\u63a5\u7684\u673a\u5236\uff0c\u4e0d\u4fdd\u8bc1\u6570\u636e\u5305\u7684\u987a\u5e8f\u548c\u5b8c\u6574\u6027\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u4e22\u5931\u6570\u636e\u6216\u63a5\u6536\u4e71\u5e8f\u7684\u6570\u636e\u5305\u3002\u8fd9\u4f7f\u5f97UDP\u4e0d\u9002\u5408\u5bf9\u6570\u636e\u5b8c\u6574\u6027\u8981\u6c42\u6781\u9ad8\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406UDP\u53d1\u9001\u65f6\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fdb\u884cUDP\u53d1\u9001\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u7f51\u7edc\u4e0d\u53ef\u8fbe\u6216\u76ee\u6807\u4e3b\u673a\u672a\u54cd\u5e94\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7socket.error\u548c\u5176\u4ed6\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u901a\u8fc7\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u95ee\u9898\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u80fd\u591f\u6839\u636e\u9700\u8981\u8fdb\u884c\u91cd\u8bd5\u6216\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u901a\u8fc7\u4f7f\u7528socket\u5e93\u6765\u5b9e\u73b0UDP\u6570\u636e\u7684\u53d1\u9001\uff0cUDP\u662f\u4e00\u79cd\u65e0\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u9002\u5408\u4e8e\u9700\u8981\u5feb\u901f\u4f20\u8f93\u6570\u636e\u4e14\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":933101,"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\/933095"}],"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=933095"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933095\/revisions"}],"predecessor-version":[{"id":933102,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933095\/revisions\/933102"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933101"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}