{"id":1180472,"date":"2025-01-15T18:37:08","date_gmt":"2025-01-15T10:37:08","guid":{"rendered":""},"modified":"2025-01-15T18:37:10","modified_gmt":"2025-01-15T10:37:10","slug":"python%e5%a6%82%e4%bd%95%e5%8f%91%e9%80%81%e7%bb%99%e5%88%ab%e4%ba%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180472.html","title":{"rendered":"python\u5982\u4f55\u53d1\u9001\u7ed9\u522b\u4eba"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114605\/76a458b1-ae96-40a4-b975-b7ce512886c7.webp\" alt=\"python\u5982\u4f55\u53d1\u9001\u7ed9\u522b\u4eba\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53d1\u9001\u5185\u5bb9\u7ed9\u522b\u4eba\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u7535\u5b50\u90ae\u4ef6\u3001\u5373\u65f6\u6d88\u606f\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u3002<strong>\u7535\u5b50\u90ae\u4ef6\u3001\u5373\u65f6\u6d88\u606f\u3001\u6587\u4ef6\u4f20\u8f93<\/strong>\u662f\u5e38\u89c1\u7684\u51e0\u79cd\u65b9\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u5185\u5bb9\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u5185\u5bb9\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5e93<code>smtplib<\/code>\u6765\u5904\u7406SMTP\uff08Simple M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l Transfer Protocol\uff09\u4efb\u52a1\uff0c<code>email<\/code>\u5e93\u5219\u5e2e\u52a9\u6211\u4eec\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u901a\u8fc7Gmail SMTP\u670d\u52a1\u5668\u53d1\u9001\u4e00\u5c01\u7535\u5b50\u90ae\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>from email.mime.multipart import MIMEMultipart<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<p>def send_email(subject, body, to_email):<\/p>\n<p>    from_email = &quot;your-email@gmail.com&quot;<\/p>\n<p>    from_password = &quot;your-email-password&quot;<\/p>\n<p>    # Create the container (outer) email message.<\/p>\n<p>    msg = MIMEMultipart()<\/p>\n<p>    msg[&#39;From&#39;] = from_email<\/p>\n<p>    msg[&#39;To&#39;] = to_email<\/p>\n<p>    msg[&#39;Subject&#39;] = subject<\/p>\n<p>    # Attach the body with the msg instance<\/p>\n<p>    msg.attach(MIMEText(body, &#39;plain&#39;))<\/p>\n<p>    # Create server object with SSL option<\/p>\n<p>    server = smtplib.SMTP_SSL(&#39;smtp.gmail.com&#39;, 465)<\/p>\n<p>    server.login(from_email, from_password)<\/p>\n<p>    server.sendmail(from_email, to_email, msg.as_string())<\/p>\n<p>    server.quit()<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_email(&quot;Test Subject&quot;, &quot;This is a test email body&quot;, &quot;recipient-email@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>MIMEMultipart<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u591a\u90e8\u5206\u7684\u90ae\u4ef6\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>MIMEText<\/code>\u6765\u9644\u52a0\u90ae\u4ef6\u6b63\u6587\u3002\u7136\u540e\u6211\u4eec\u901a\u8fc7Gmail\u7684SMTP\u670d\u52a1\u5668\u53d1\u9001\u90ae\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u7535\u5b50\u90ae\u4ef6<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528smtplib\u548cemail\u5e93\u53d1\u9001\u7535\u5b50\u90ae\u4ef6<\/h3>\n<\/p>\n<p><p>\u7535\u5b50\u90ae\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u901a\u4fe1\u624b\u6bb5\uff0cPython\u5185\u7f6e\u7684<code>smtplib<\/code>\u5e93\u548c<code>email<\/code>\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u90ae\u4ef6\u53d1\u9001\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>from email.mime.multipart import MIMEMultipart<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u53d1\u9001\u8005\u548c\u63a5\u6536\u8005\u7684\u4fe1\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from_email = &quot;your-email@gmail.com&quot;<\/p>\n<p>from_password = &quot;your-email-password&quot;<\/p>\n<p>to_email = &quot;recipient-email@example.com&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u90ae\u4ef6\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">msg = MIMEMultipart()<\/p>\n<p>msg[&#39;From&#39;] = from_email<\/p>\n<p>msg[&#39;To&#39;] = to_email<\/p>\n<p>msg[&#39;Subject&#39;] = &quot;Test Subject&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u90ae\u4ef6\u6b63\u6587<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">body = &quot;This is a test email body&quot;<\/p>\n<p>msg.attach(MIMEText(body, &#39;plain&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u9001\u90ae\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">server = smtplib.SMTP_SSL(&#39;smtp.gmail.com&#39;, 465)<\/p>\n<p>server.login(from_email, from_password)<\/p>\n<p>server.sendmail(from_email, to_email, msg.as_string())<\/p>\n<p>server.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u53d1\u9001\u90ae\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\uff0cPython\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5982<code>yagmail<\/code>\u548c<code>emails<\/code>\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u7b80\u5316\u548c\u5f3a\u5927\u7684\u90ae\u4ef6\u53d1\u9001\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528yagmail<\/h4>\n<\/p>\n<p><p><code>yagmail<\/code>\u662f\u4e00\u4e2a\u7b80\u5316\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u7684\u5e93\uff0c\u5b83\u5c01\u88c5\u4e86\u5e38\u7528\u7684\u64cd\u4f5c\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yagmail<\/p>\n<p>def send_email_via_yagmail(subject, body, to_email):<\/p>\n<p>    yag = yagmail.SMTP(&quot;your-email@gmail.com&quot;, &quot;your-email-password&quot;)<\/p>\n<p>    yag.send(to=to_email, subject=subject, contents=body)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_email_via_yagmail(&quot;Test Subject&quot;, &quot;This is a test email body&quot;, &quot;recipient-email@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>yagmail<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u90ae\u4ef6\u53d1\u9001\u7684\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5373\u65f6\u6d88\u606f<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528API\u53d1\u9001\u5373\u65f6\u6d88\u606f<\/h3>\n<\/p>\n<p><p>\u5373\u65f6\u6d88\u606f\uff08IM\uff09\u662f\u4e00\u79cd\u5feb\u901f\u3001\u5b9e\u65f6\u7684\u901a\u4fe1\u65b9\u5f0f\u3002\u8bb8\u591a\u5373\u65f6\u6d88\u606f\u5e73\u53f0\u63d0\u4f9bAPI\u63a5\u53e3\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u7a0b\u5e8f\u53d1\u9001\u6d88\u606f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Slack API\u53d1\u9001\u6d88\u606f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5slack_sdk\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install slack_sdk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Slack API\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from slack_sdk import WebClient<\/p>\n<p>from slack_sdk.errors import SlackApiError<\/p>\n<p>def send_slack_message(token, channel, text):<\/p>\n<p>    client = WebClient(token=token)<\/p>\n<p>    try:<\/p>\n<p>        response = client.chat_postMessage(channel=channel, text=text)<\/p>\n<p>        assert response[&quot;message&quot;][&quot;text&quot;] == text<\/p>\n<p>    except SlackApiError as e:<\/p>\n<p>        print(f&quot;Error sending message: {e.response[&#39;error&#39;]}&quot;)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_slack_message(&quot;your-slack-bot-token&quot;, &quot;#general&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u53d1\u9001\u5373\u65f6\u6d88\u606f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5b98\u65b9API\uff0c\u7b2c\u4e09\u65b9\u5e93\u5982<code>python-telegram-bot<\/code>\u4e5f\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u53d1\u9001\u5373\u65f6\u6d88\u606f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Telegram Bot API\u53d1\u9001\u6d88\u606f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5python-telegram-bot\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-telegram-bot<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Telegram Bot API\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from telegram import Bot<\/p>\n<p>def send_telegram_message(token, chat_id, text):<\/p>\n<p>    bot = Bot(token=token)<\/p>\n<p>    bot.send_message(chat_id=chat_id, text=text)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_telegram_message(&quot;your-telegram-bot-token&quot;, &quot;your-chat-id&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u6587\u4ef6\u4f20\u8f93<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528FTP\u534f\u8bae\u4f20\u8f93\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>FTP\uff08File Transfer Protocol\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6587\u4ef6\u4f20\u8f93\u534f\u8bae\uff0cPython\u7684<code>ftplib<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0FTP\u6587\u4ef6\u4f20\u8f93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165ftplib\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ftplib import FTP<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5FTP\u670d\u52a1\u5668\u5e76\u4e0a\u4f20\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def upload_file(ftp_server, ftp_user, ftp_password, file_path, remote_path):<\/p>\n<p>    ftp = FTP(ftp_server)<\/p>\n<p>    ftp.login(user=ftp_user, passwd=ftp_password)<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        ftp.storbinary(f&#39;STOR {remote_path}&#39;, file)<\/p>\n<p>    ftp.quit()<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>upload_file(&quot;ftp.example.com&quot;, &quot;ftp_user&quot;, &quot;ftp_password&quot;, &quot;local_file.txt&quot;, &quot;remote_file.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528SFTP\u534f\u8bae\u4f20\u8f93\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>SFTP\uff08SSH File Transfer Protocol\uff09\u662f\u4e00\u79cd\u5b89\u5168\u7684\u6587\u4ef6\u4f20\u8f93\u534f\u8bae\uff0c<code>paramiko<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0SFTP\u6587\u4ef6\u4f20\u8f93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5paramiko\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install paramiko<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528SFTP\u4e0a\u4f20\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import paramiko<\/p>\n<p>def upload_file_sftp(host, port, username, password, local_path, remote_path):<\/p>\n<p>    transport = paramiko.Transport((host, port))<\/p>\n<p>    transport.connect(username=username, password=password)<\/p>\n<p>    sftp = paramiko.SFTPClient.from_transport(transport)<\/p>\n<p>    sftp.put(local_path, remote_path)<\/p>\n<p>    sftp.close()<\/p>\n<p>    transport.close()<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>upload_file_sftp(&quot;sftp.example.com&quot;, 22, &quot;sftp_user&quot;, &quot;sftp_password&quot;, &quot;local_file.txt&quot;, &quot;remote_file.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u6d88\u606f\u961f\u5217<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528RabbitMQ\u53d1\u9001\u6d88\u606f<\/h3>\n<\/p>\n<p><p>\u6d88\u606f\u961f\u5217\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6d88\u606f\u4f20\u9012\u65b9\u5f0f\uff0cRabbitMQ\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\u3002Python\u7684<code>pika<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e0eRabbitMQ\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pika\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pika<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528RabbitMQ\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>def send_rabbitmq_message(host, queue, message):<\/p>\n<p>    connection = pika.BlockingConnection(pika.ConnectionParameters(host=host))<\/p>\n<p>    channel = connection.channel()<\/p>\n<p>    channel.queue_declare(queue=queue)<\/p>\n<p>    channel.basic_publish(exchange=&#39;&#39;, routing_key=queue, body=message)<\/p>\n<p>    connection.close()<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_rabbitmq_message(&quot;localhost&quot;, &quot;test_queue&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528Redis\u53d1\u9001\u6d88\u606f<\/h3>\n<\/p>\n<p><p>Redis\u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f5c\u6d88\u606f\u961f\u5217\u3002Python\u7684<code>redis<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e0eRedis\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5redis\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install redis<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Redis\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import redis<\/p>\n<p>def send_redis_message(host, port, channel, message):<\/p>\n<p>    r = redis.StrictRedis(host=host, port=port, db=0)<\/p>\n<p>    r.publish(channel, message)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_redis_message(&quot;localhost&quot;, 6379, &quot;test_channel&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001WebSocket<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528WebSocket\u53d1\u9001\u6d88\u606f<\/h3>\n<\/p>\n<p><p>WebSocket\u662f\u4e00\u79cd\u5728\u5355\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\u3002Python\u7684<code>websockets<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0WebSocket\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5websockets\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install websockets<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528WebSocket\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import websockets<\/p>\n<p>async def send_websocket_message(uri, message):<\/p>\n<p>    async with websockets.connect(uri) as websocket:<\/p>\n<p>        await websocket.send(message)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>asyncio.get_event_loop().run_until_complete(send_websocket_message(&#39;ws:\/\/localhost:8765&#39;, &#39;This is a test message&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528Socket.IO\u53d1\u9001\u6d88\u606f<\/h3>\n<\/p>\n<p><p>Socket.IO\u662f\u4e00\u4e2a\u7528\u4e8e\u5b9e\u65f6\u53cc\u5411\u4e8b\u4ef6\u901a\u4fe1\u7684\u5e93\uff0cPython\u7684<code>sockio<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0Socket.IO\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5sockio\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-socketio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Socket.IO\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socketio<\/p>\n<p>def send_socketio_message(url, message):<\/p>\n<p>    sio = socketio.Client()<\/p>\n<p>    sio.connect(url)<\/p>\n<p>    sio.emit(&#39;message&#39;, message)<\/p>\n<p>    sio.disconnect()<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_socketio_message(&#39;http:\/\/localhost:5000&#39;, &#39;This is a test message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u516d\u3001\u77ed\u4fe1<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528Twilio API\u53d1\u9001\u77ed\u4fe1<\/h3>\n<\/p>\n<p><p>Twilio\u662f\u4e00\u4e2a\u8457\u540d\u7684\u901a\u4fe1\u670d\u52a1\u63d0\u4f9b\u5546\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176API\u53d1\u9001\u77ed\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5twilio\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twilio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Twilio API\u53d1\u9001\u77ed\u4fe1<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from twilio.rest import Client<\/p>\n<p>def send_sms_twilio(account_sid, auth_token, from_number, to_number, body):<\/p>\n<p>    client = Client(account_sid, auth_token)<\/p>\n<p>    message = client.messages.create(<\/p>\n<p>        body=body,<\/p>\n<p>        from_=from_number,<\/p>\n<p>        to=to_number<\/p>\n<p>    )<\/p>\n<p>    return message.sid<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_sms_twilio(&quot;your_account_sid&quot;, &quot;your_auth_token&quot;, &quot;+1234567890&quot;, &quot;+0987654321&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528Nexmo API\u53d1\u9001\u77ed\u4fe1<\/h3>\n<\/p>\n<p><p>Nexmo\u4e5f\u662f\u4e00\u4e2a\u8457\u540d\u7684\u901a\u4fe1\u670d\u52a1\u63d0\u4f9b\u5546\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176API\u53d1\u9001\u77ed\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5nexmo\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nexmo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Nexmo API\u53d1\u9001\u77ed\u4fe1<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nexmo<\/p>\n<p>def send_sms_nexmo(api_key, api_secret, from_number, to_number, body):<\/p>\n<p>    client = nexmo.Client(key=api_key, secret=api_secret)<\/p>\n<p>    response = client.send_message({<\/p>\n<p>        &#39;from&#39;: from_number,<\/p>\n<p>        &#39;to&#39;: to_number,<\/p>\n<p>        &#39;text&#39;: body,<\/p>\n<p>    })<\/p>\n<p>    return response<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_sms_nexmo(&quot;your_api_key&quot;, &quot;your_api_secret&quot;, &quot;Nexmo&quot;, &quot;+0987654321&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e03\u3001\u793e\u4ea4\u5a92\u4f53<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528Twitter API\u53d1\u9001\u63a8\u6587<\/h3>\n<\/p>\n<p><p>Twitter API\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u7a0b\u5e8f\u53d1\u9001\u63a8\u6587\u3002Python\u7684<code>tweepy<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e0eTwitter API\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5tweepy\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tweepy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Twitter API\u53d1\u9001\u63a8\u6587<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tweepy<\/p>\n<p>def send_tweet(api_key, api_secret_key, access_token, access_token_secret, status):<\/p>\n<p>    auth = tweepy.OAuth1UserHandler(api_key, api_secret_key, access_token, access_token_secret)<\/p>\n<p>    api = tweepy.API(auth)<\/p>\n<p>    api.update_status(status=status)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_tweet(&quot;your_api_key&quot;, &quot;your_api_secret_key&quot;, &quot;your_access_token&quot;, &quot;your_access_token_secret&quot;, &quot;This is a test tweet&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528Facebook API\u53d1\u9001\u6d88\u606f<\/h3>\n<\/p>\n<p><p>Facebook API\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u7a0b\u5e8f\u53d1\u9001\u6d88\u606f\u3002Python\u7684<code>facebook-sdk<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e0eFacebook API\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5facebook-sdk\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install facebook-sdk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Facebook API\u53d1\u9001\u6d88\u606f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import facebook<\/p>\n<p>def send_facebook_message(access_token, recipient_id, message):<\/p>\n<p>    graph = facebook.GraphAPI(access_token)<\/p>\n<p>    graph.put_object(parent_object=recipient_id, connection_name=&#39;feed&#39;, message=message)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_facebook_message(&quot;your_access_token&quot;, &quot;recipient_id&quot;, &quot;This is a test message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u516b\u3001\u901a\u77e5\u670d\u52a1<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528Pushbullet API\u53d1\u9001\u901a\u77e5<\/h3>\n<\/p>\n<p><p>Pushbullet\u662f\u4e00\u4e2a\u5141\u8bb8\u8bbe\u5907\u95f4\u53d1\u9001\u901a\u77e5\u7684\u670d\u52a1\u3002Python\u7684<code>pushbullet<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e0ePushbullet API\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pushbullet\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pushbullet.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Pushbullet API\u53d1\u9001\u901a\u77e5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pushbullet import Pushbullet<\/p>\n<p>def send_pushbullet_notification(api_key, title, body):<\/p>\n<p>    pb = Pushbullet(api_key)<\/p>\n<p>    pb.push_note(title, body)<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_pushbullet_notification(&quot;your_api_key&quot;, &quot;Test Notification&quot;, &quot;This is a test notification&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528Pushover API\u53d1\u9001\u901a\u77e5<\/h3>\n<\/p>\n<p><p>Pushover\u662f\u4e00\u4e2a\u5141\u8bb8\u8bbe\u5907\u95f4\u53d1\u9001\u901a\u77e5\u7684\u670d\u52a1\u3002Python\u7684<code>requests<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e0ePushover API\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5requests\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Pushover API\u53d1\u9001\u901a\u77e5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def send_pushover_notification(token, user, message):<\/p>\n<p>    url = &quot;https:\/\/api.pushover.net\/1\/messages.json&quot;<\/p>\n<p>    data = {<\/p>\n<p>        &quot;token&quot;: token,<\/p>\n<p>        &quot;user&quot;: user,<\/p>\n<p>        &quot;message&quot;: message<\/p>\n<p>    }<\/p>\n<p>    response = requests.post(url, data=data)<\/p>\n<p>    return response<\/p>\n<h2><strong>Usage<\/strong><\/h2>\n<p>send_pushover_notification(&quot;your_app_token&quot;, &quot;your_user_key&quot;, &quot;This is a test notification&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5411\u522b\u4eba\u53d1\u9001\u4fe1\u606f\u7684\u529f\u80fd\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u3001\u5373\u65f6\u6d88\u606f\u3001\u6587\u4ef6\u4f20\u8f93\u8fd8\u662f\u6d88\u606f\u961f\u5217\u7b49\u65b9\u5f0f\uff0c\u90fd\u53ef\u4ee5\u5229\u7528Python\u7684\u4e30\u5bcc\u5e93\u8d44\u6e90\u6765\u5b9e\u73b0\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f<\/strong>\u3002\u65e0\u8bba\u662f\u54ea\u79cd\u65b9\u5f0f\uff0c\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\u548c\u5b89\u5168\u6027\u8003\u8651\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u4ee5\u786e\u4fdd\u4fe1\u606f\u4f20\u9012\u7684\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4ee3\u7801\u5206\u4eab\u7ed9\u4ed6\u4eba\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5206\u4eabPython\u4ee3\u7801\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5c06\u4ee3\u7801\u4fdd\u5b58\u4e3a.py\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7\u7535\u5b50\u90ae\u4ef6\u3001\u4e91\u5b58\u50a8\uff08\u5982Google Drive\u3001Dropbox\uff09\u6216\u7248\u672c\u63a7\u5236\u5e73\u53f0\uff08\u5982GitHub\uff09\u8fdb\u884c\u5206\u4eab\u3002\u60a8\u4e5f\u53ef\u4ee5\u5c06\u4ee3\u7801\u7c98\u8d34\u5230\u4ee3\u7801\u5206\u4eab\u7f51\u7ad9\u4e0a\uff0c\u4f8b\u5982Pastebin\u6216Gist\uff0c\u4ee5\u4fbf\u4e8e\u4ed6\u4eba\u67e5\u770b\u548c\u4e0b\u8f7d\u3002<\/p>\n<p><strong>\u5728\u53d1\u9001Python\u4ee3\u7801\u65f6\uff0c\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u53d1\u9001\u4ee3\u7801\u65f6\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u6dfb\u52a0\u6ce8\u91ca\u4ee5\u89e3\u91ca\u590d\u6742\u7684\u90e8\u5206\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u9075\u5faa\u826f\u597d\u7684\u7f16\u7801\u89c4\u8303\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u4ee3\u7801\u4e2d\u662f\u5426\u5305\u542b\u654f\u611f\u4fe1\u606f\uff08\u5982\u5bc6\u7801\u6216API\u5bc6\u94a5\uff09\uff0c\u5e76\u5728\u5206\u4eab\u4e4b\u524d\u5c06\u5176\u79fb\u9664\u6216\u66ff\u6362\u4e3a\u5360\u4f4d\u7b26\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u5728\u53d1\u9001\u4ee3\u7801\u65f6\u8fdb\u884c\u683c\u5f0f\u5316\uff1f<\/strong><br \/>\u6709\u5f88\u591a\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u60a8\u683c\u5f0f\u5316Python\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u6613\u8bfb\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u5982Black\u6216YAPF\uff0c\u8fd9\u4e9b\u5de5\u5177\u4f1a\u81ea\u52a8\u8c03\u6574\u4ee3\u7801\u7684\u683c\u5f0f\u3002\u6b64\u5916\uff0c\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e5f\u63d0\u4f9b\u4e86\u4ee3\u7801\u683c\u5f0f\u5316\u7684\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u5728\u4fdd\u5b58\u6587\u4ef6\u524d\u8fdb\u884c\u683c\u5f0f\u5316\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53d1\u9001\u5185\u5bb9\u7ed9\u522b\u4eba\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u7535\u5b50\u90ae\u4ef6\u3001\u5373\u65f6\u6d88\u606f\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u3002\u7535\u5b50\u90ae\u4ef6\u3001\u5373\u65f6\u6d88\u606f [&hellip;]","protected":false},"author":3,"featured_media":1180475,"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\/1180472"}],"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=1180472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180472\/revisions"}],"predecessor-version":[{"id":1180477,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180472\/revisions\/1180477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180475"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}