{"id":173089,"date":"2024-05-08T18:04:17","date_gmt":"2024-05-08T10:04:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173089.html"},"modified":"2024-05-08T18:04:20","modified_gmt":"2024-05-08T10:04:20","slug":"%e7%be%a4%e5%8f%91%e7%9f%ad%e4%bf%a1%e5%b9%b3%e5%8f%b0%e7%9a%84%e6%8e%a5%e5%8f%a3%e6%94%af%e6%8c%81python%e8%af%ad%e9%9f%b3%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173089.html","title":{"rendered":"\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\u652f\u6301python\u8bed\u97f3\u5417"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27044844\/0b02b048-e455-4aa7-9894-0874cb565a18.webp\" alt=\"\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\u652f\u6301python\u8bed\u97f3\u5417\" \/><\/p>\n<p><p><strong>\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\u901a\u5e38\u652f\u6301Python\u8bed\u8a00<\/strong>\uff0c\u56e0\u4e3aPython\u5177\u5907\u7b80\u6d01\u6613\u8bfb\u7684\u4ee3\u7801\u3001\u5f3a\u5927\u7684\u5e93\u652f\u6301\u548c\u5e7f\u6cdb\u7684\u793e\u533a\u8d44\u6e90\uff0c\u4f7f\u5f97\u5b83\u5728\u7f16\u5199\u63a5\u53e3\u8c03\u7528\u811a\u672c\u65f6\u683c\u5916\u53d7\u6b22\u8fce\u3002\u5927\u591a\u6570\u77ed\u4fe1\u5e73\u53f0\u901a\u8fc7\u63d0\u4f9bRESTful API\u63a5\u53e3\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528Python\u7f16\u5199\u4ee3\u7801\u6765\u5b9e\u73b0\u5982\u53d1\u9001\u77ed\u4fe1\u3001\u67e5\u8be2\u4f59\u989d\u3001\u83b7\u53d6\u53d1\u9001\u62a5\u544a\u7b49\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9bAPI\u63a5\u53e3\uff0c\u7528\u6237\u53ef\u4ee5\u7f16\u5199\u81ea\u5df1\u7684Python\u811a\u672c\u5bf9\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u8fdb\u884c\u64cd\u4f5c\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u77ed\u4fe1\u53d1\u9001\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u53e3\u7684\u5177\u4f53\u652f\u6301\u548c\u4f7f\u7528\uff0c\u9700\u8981\u67e5\u9605\u7279\u5b9a\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u63d0\u4f9b\u7684\u5f00\u53d1\u8005\u6587\u6863\u3002\u8fd9\u4e9b\u6587\u6863\u901a\u5e38\u8be6\u7ec6\u63cf\u8ff0\u4e86API\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u53c2\u6570\u8bf4\u660e\u53ca\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u5f97\u5373\u4f7f\u662fPython\u521d\u5b66\u8005\u4e5f\u80fd\u8f83\u5bb9\u6613\u5730\u4e0a\u624b\u5b9e\u73b0\u57fa\u7840\u7684\u63a5\u53e3\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u63a5\u53e3\u7684\u7ed3\u6784\uff0cPython\u5728\u5176\u4e2d\u626e\u6f14\u7684\u89d2\u8272\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4ee3\u7801\u4ee5\u5b9e\u73b0\u7fa4\u53d1\u77ed\u4fe1\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0API\u63a5\u53e3\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u901a\u5e38\u901a\u8fc7API\uff08Application Programming Interface\uff0c\u5e94\u7528\u7a0b\u5e8f\u7f16\u7a0b\u63a5\u53e3\uff09\u63d0\u4f9b\u670d\u52a1\u3002\u8fd9\u4e9bAPI\u9075\u5faa\u4e00\u5b9a\u7684\u7f51\u7edc\u534f\u8bae\u5982HTTP\/HTTPS\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u4e0e\u77ed\u4fe1\u5e73\u53f0\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p><strong>API\u63a5\u53e3\u7684\u4e3b\u8981\u529f\u80fd\u901a\u5e38\u5305\u62ec<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u53d1\u9001\u77ed\u4fe1<\/strong>\uff1a\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u77ed\u4fe1\u5185\u5bb9\u548c\u63a5\u6536\u8005\u624b\u673a\u53f7\u7801\uff0c\u4ece\u800c\u6279\u91cf\u53d1\u9001\u77ed\u4fe1\u3002<\/li>\n<li><strong>\u67e5\u8be2\u4f59\u989d<\/strong>\uff1a\u63d0\u4f9b\u7528\u6237\u67e5\u8be2\u8d26\u6237\u4f59\u989d\u7684\u529f\u80fd\uff0c\u786e\u4fdd\u53d1\u9001\u64cd\u4f5c\u53ef\u987a\u5229\u8fdb\u884c\u3002<\/li>\n<li><strong>\u83b7\u53d6\u53d1\u9001\u62a5\u544a<\/strong>\uff1a\u5141\u8bb8\u7528\u6237\u83b7\u53d6\u77ed\u4fe1\u53d1\u9001\u7684\u72b6\u6001\u62a5\u544a\uff0c\u7528\u4e8e\u76d1\u63a7\u77ed\u4fe1\u53d1\u9001\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001Python\u5728\u7fa4\u53d1\u77ed\u4fe1\u63a5\u53e3\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>Python\u7531\u4e8e\u5176\u8bed\u6cd5\u7b80\u5355\u3001\u5e93\u8d44\u6e90\u4e30\u5bcc\uff0c\u5728\u8fdb\u884cAPI\u8c03\u7528\u65f6\u6709\u7740\u5f97\u5929\u72ec\u539a\u7684\u4f18\u52bf\u3002\u901a\u8fc7\u4f7f\u7528Python\u63d0\u4f9b\u7684requests\u5e93\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u7b80\u6d01\u7684\u4ee3\u7801\u6765\u8c03\u7528\u7fa4\u53d1\u77ed\u4fe1\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u8c03\u7528API\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u4f7f\u7528pip\u7b49\u5305\u7ba1\u7406\u5de5\u5177\u5b89\u88c5requests\u7b49\u5e93\u3002<\/li>\n<li><strong>\u4e86\u89e3API\u6587\u6863<\/strong>\uff1a\u9605\u8bfb\u77ed\u4fe1\u5e73\u53f0\u63d0\u4f9b\u7684API\u6587\u6863\uff0c\u4e86\u89e3\u9700\u8981\u4f7f\u7528\u7684URL\u3001\u53c2\u6570\u548c\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u7f16\u5199\u8bf7\u6c42\u4ee3\u7801<\/strong>\uff1a\u4f7f\u7528Python\u7f16\u5199\u8bf7\u6c42\u4ee3\u7801\uff0c\u5305\u62ec\u6784\u5efa\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u548c\u5f02\u5e38\u5904\u7406\u7b49\u3002<\/li>\n<li><strong>\u6267\u884c\u6d4b\u8bd5<\/strong>\uff1a\u5728\u5b89\u5168\u7684\u6d4b\u8bd5\u73af\u5883\u4e0b\u6d4b\u8bd5\u7f16\u5199\u7684\u4ee3\u7801\uff0c\u786e\u4fdd\u529f\u80fd\u7b26\u5408\u9884\u671f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5b9e\u73b0\u7fa4\u53d1\u77ed\u4fe1\u529f\u80fd\u7684Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u5b9e\u73b0\u7fa4\u53d1\u77ed\u4fe1\u7684\u529f\u80fd\uff0c\u9996\u5148\u9700\u8981\u7406\u89e3API\u7684\u8c03\u7528\u8fc7\u7a0b\u3002\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u822c\u6027\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u5c55\u73b0\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528\u7fa4\u53d1\u77ed\u4fe1\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import json<\/p>\n<p>API_URL = &quot;https:\/\/api.smsplatform.com\/send&quot;  # \u793a\u4f8bAPI URL<\/p>\n<p>API_KEY = &quot;your_api_key&quot;  # \u7528\u6237\u7684API\u5bc6\u94a5\uff0c\u9700\u8981\u66ff\u6362\u4e3a\u5b9e\u9645\u503c<\/p>\n<h2><strong>\u53d1\u9001\u77ed\u4fe1\u51fd\u6570<\/strong><\/h2>\n<p>def send_mass_sms(phone_numbers, message):<\/p>\n<p>    headers = {&quot;Content-Type&quot;: &quot;application\/json&quot;}<\/p>\n<p>    data = {<\/p>\n<p>        &quot;apiKey&quot;: API_KEY,<\/p>\n<p>        &quot;numbers&quot;: phone_numbers,<\/p>\n<p>        &quot;message&quot;: message<\/p>\n<p>    }<\/p>\n<p>    response = requests.post(API_URL, headers=headers, data=json.dumps(data))<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        return response.json()<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se Exception(f&quot;Error: {response.status_code}, {response.text}&quot;)<\/p>\n<h2><strong>\u4e3b\u529f\u80fd<\/strong><\/h2>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    phones = [&quot;+12345678901&quot;, &quot;+10987654321&quot;]<\/p>\n<p>    sms_message = &quot;Your message goes here.&quot;<\/p>\n<p>    try:<\/p>\n<p>        result = send_mass_sms(phones, sms_message)<\/p>\n<p>        print(&quot;SMS sent successfully:&quot;, result)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528requests\u5e93\u5411\u77ed\u4fe1\u5e73\u53f0\u7684API\u53d1\u9001POST\u8bf7\u6c42\u4ee5\u7fa4\u53d1\u77ed\u4fe1\u3002\u4e3a\u4e86\u4f7f\u4ee3\u7801\u8fd0\u884c\uff0c\u4f60\u9700\u8981\u5c06<code>API_KEY<\/code>\u548c<code>API_URL<\/code>\u66ff\u6362\u4e3a\u771f\u5b9e\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0API\u5b89\u5168\u6027\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528API\u65f6\uff0c\u4fdd\u8bc1\u901a\u4fe1\u8fc7\u7a0b\u7684\u5b89\u5168\u6027\u975e\u5e38\u91cd\u8981\u3002\u56e0\u6b64\uff0c<strong>\u9009\u62e9\u652f\u6301HTTPS\u534f\u8bae\u7684\u77ed\u4fe1\u5e73\u53f0<\/strong>\u4ee5\u907f\u514d\u654f\u611f\u4fe1\u606f\u5728\u7f51\u7edc\u4e2d\u660e\u6587\u4f20\u8f93\u5c24\u4e3a\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p><strong>\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5b89\u5168\u7684API\u5bc6\u94a5\u7ba1\u7406<\/strong>\uff1a\u907f\u514d\u5c06API\u5bc6\u94a5\u786c\u7f16\u7801\u5728\u4ee3\u7801\u4e2d\uff0c\u91c7\u7528\u73af\u5883\u53d8\u91cf\u6216\u914d\u7f6e\u6587\u4ef6\u7b49\u5b89\u5168\u65b9\u5f0f\u5b58\u50a8\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5bf9API\u8bf7\u6c42\u7684\u53ef\u80fd\u9519\u8bef\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u8bbe\u7f6e\u5408\u7406\u7684\u91cd\u8bd5\u673a\u5236\u3002<\/li>\n<li><strong>\u5408\u89c4\u6027\u9075\u5faa<\/strong>\uff1a\u9075\u5faa\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u6bd4\u5982\u5728\u53d1\u9001\u8425\u9500\u77ed\u4fe1\u65f6\u9700\u8003\u8651\u63a5\u6536\u8005\u7684\u540c\u610f\u548c\u9690\u79c1\u6743\u3002<\/li>\n<\/ul>\n<p><p>\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\u548cPython\u8bed\u8a00\u7684\u7ed3\u5408\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u96c6\uff0c\u4ee5\u652f\u6301\u4e0d\u540c\u89c4\u6a21\u548c\u9700\u6c42\u7684\u77ed\u4fe1\u53d1\u9001\u4efb\u52a1\u3002\u638c\u63e1\u5982\u4f55\u4f7f\u7528Python\u811a\u672c\u8c03\u7528\u8fd9\u4e9bAPI\u63a5\u53e3\u5c06\u4f7f\u4f60\u80fd\u591f\u6709\u6548\u5730\u96c6\u6210\u77ed\u4fe1\u670d\u52a1\u5230\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u81ea\u52a8\u5316\u5de5\u4f5c\u6d41\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u73b0\u6709\u7684\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u63a5\u53e3\u662f\u5426\u652f\u6301Python\u8bed\u8a00\uff1f<\/strong><\/p>\n<p>\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\u662f\u5426\u80fd\u591f\u4e0ePython\u8bed\u8a00\u517c\u5bb9\uff1f<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\uff1f<\/strong><\/p>\n<p>\u6211\u600e\u6837\u624d\u80fd\u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\u6765\u8c03\u7528\u548c\u96c6\u6210\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\uff1f<\/p>\n<p><strong>\u6709\u6ca1\u6709\u9488\u5bf9Python\u5f00\u53d1\u7684\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u63a5\u53e3\u6587\u6863\uff1f<\/strong><\/p>\n<p>\u662f\u5426\u5b58\u5728\u9002\u7528\u4e8ePython\u5f00\u53d1\u7684\u8be6\u7ec6\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u63a5\u53e3\u6587\u6863\uff0c\u4ee5\u4fbf\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u65b9\u4fbf\u5730\u4f7f\u7528\u548c\u96c6\u6210\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"\u7fa4\u53d1\u77ed\u4fe1\u5e73\u53f0\u7684\u63a5\u53e3\u901a\u5e38\u652f\u6301Python\u8bed\u8a00\uff0c\u56e0\u4e3aPython\u5177\u5907\u7b80\u6d01\u6613\u8bfb\u7684\u4ee3\u7801\u3001\u5f3a\u5927\u7684\u5e93\u652f\u6301\u548c\u5e7f\u6cdb\u7684\u793e\u533a\u8d44\u6e90\uff0c [&hellip;]","protected":false},"author":3,"featured_media":173094,"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\/173089"}],"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=173089"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173089\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173094"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}