项目作者: gateray

项目描述 :
msg-sender是一个基于tornado框架实现的异步消息发送接口,当前支持消息发送微信企业号、玄武短信接口和Email; 接口调用支持http和websocket。遵循MIT开源许可。
高级语言: Python
项目地址: git://github.com/gateray/msg-sender.git
创建时间: 2017-10-09T12:23:33Z
项目社区:https://github.com/gateray/msg-sender

开源协议:MIT License

下载


概述

msg-sender是一个基于tornado框架的异步消息发送接口,当前支持消息发送微信企业号、玄武短信接口和Email; 接口调用支持http和websocket。遵循MIT开源许可。

接口调用方式

微信企业号消息发送接口

关闭签名:

字段名 字段类型 是否必须 描述
title String 消息的标题
content String 消息的内容
  1. POST http://host:8000/qywx?content=abc&title=接口测试

开启签名:

字段名 字段类型 是否必须 描述
title String 消息的标题
content String 消息的内容
timestamp Integer unix时间戳(当前时间,10位数字)
signature String 16进制格式的签名值,支持md5、sha1、sha128、sha224、 sha256、 sha384、 sha512签名方法,默认为sha256。

签名说明:

  • 将所有字段按key自然排序后,拼接key-value得到字符串A

  • 字符串A后拼接上apiKey,得到字符串B

  • 字符串B进行签名,输出的16进制字符串为signature字段的值

示例:

  • 假设title=test&content=test消息内容×tamp=1507545674, apiKey=mysecret

  • 按key自然排序的结果为:content=test消息内容×tamp=1507545674&title=test

  • 拼接后得到字符串:contenttest消息内容timestamp1507545674titletest

  • 拼接上apiKey得到的字符串:contenttest消息内容timestamp1507545674titletestmysecret

  • 上一步得到的字符串进行sha256签名,得到签名:28924486d2aaf886565736a50e61bb9fb0d3baf613e2333b0e497934699ec15b

  1. POST http://host:8000/qywx?title=test&content=test消息内容×tamp=1507545674&signature=28924486d2aaf886565736a50e61bb9fb0d3baf613e2333b0e497934699ec15b

签名有效期为1分钟

玄武短信消息发送接口

关闭签名:

字段名 字段类型 是否必须 描述
title String 消息的标题
content String 消息的内容
  1. POST http://host:8000/sms?content=这是一条测试短信&title=标题会忽略

开启签名:

规则与上面微信企业号方式相同

Email消息发送接口

关闭签名:

字段名 字段类型 是否必须 描述
title String 消息的标题
content String 消息的内容
  1. POST http://host:8000/mail?content=这是一封测试邮件,请勿回复!&title=接口测试

开启签名:

规则与上面微信企业号方式相同

部署

环境要求

  • python 3.5以上

  • redis 2.8以上(可选,使用微信企业号发送消息时需要使用)

  • 推荐linux环境下运行

依赖安装

  1. git clone https://github.com/gateray/msg-sender.git
  2. cd msg-sender
  3. pip install -r requirements.txt

运行

测试环境

  1. cp local_settings.py.example local_settings.py
  2. python app.py --port=8000

生产环境部署建议

建议使用nginx作为反向代理,后端开启多个python进程(建议与cpu核数相等)做负载均衡。

打开最多文件数限制:

方式一:

  1. ulimit -n 1048576

方式二:

  1. vim /etc/security/limits.conf
  2. # 加上下面配置:
  3. * - nofile 1048576

方式三(systemd):

  1. 在对应服务的service文件中添加,例如:
  2. vim /lib/systemd/system/nginx.service
  3. [Service]
  4. User=gateray
  5. Group=gateray
  6. LimitNOFILE= 1048576
  7. # 实际上对于nginx,可以通过配置文件中 worker_connections 1048576;指定

开启多个python进程:

  1. for i in {8001..8008}; do
  2. nohup python app.py --port=800$i &> /tmp/msg-sender-800$i.log &
  3. done

强烈推荐使用supervisor去管理进程,以下是一个示例:

  1. msg-sender git:(master) cat /etc/supervisord.conf.d/msg-sender.conf
  2. [program:msg-sender-8001]
  3. directory=/home/gateray/workspace/python/msg-sender
  4. command=/home/gateray/.virtualenvs/msg-sender/bin/python app.py --port=8001
  5. process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  6. user=gateray ; setuid to this UNIX account to run the program
  7. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  8. stdout_logfile=/tmp/%(program_name)s.log ; stdout log path, NONE for none; default AUTO
  9. [program:msg-sender-8002]
  10. directory=/home/gateray/workspace/python/msg-sender
  11. command=/home/gateray/.virtualenvs/msg-sender/bin/python app.py --port=8002
  12. process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  13. user=gateray ; setuid to this UNIX account to run the program
  14. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  15. stdout_logfile=/tmp/%(program_name)s.log ; stdout log path, NONE for none; default AUTO
  16. [program:msg-sender-8003]
  17. directory=/home/gateray/workspace/python/msg-sender
  18. command=/home/gateray/.virtualenvs/msg-sender/bin/python app.py --port=8003
  19. process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  20. user=gateray ; setuid to this UNIX account to run the program
  21. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  22. stdout_logfile=/tmp/%(program_name)s.log ; stdout log path, NONE for none; default AUTO
  23. [program:msg-sender-8004]
  24. directory=/home/gateray/workspace/python/msg-sender
  25. command=/home/gateray/.virtualenvs/msg-sender/bin/python app.py --port=8004
  26. process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  27. user=gateray ; setuid to this UNIX account to run the program
  28. redirect_stderr=true ; redirect proc stderr to stdout (default false)
  29. stdout_logfile=/tmp/%(program_name)s.log ; stdout log path, NONE for none; default AUTO

nginx配置:

  1. user gateray;
  2. worker_processes 4;
  3. events {
  4. worker_connections 1048576;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. upstream msg-senders {
  10. server 127.0.0.1:8001;
  11. server 127.0.0.1:8002;
  12. server 127.0.0.1:8003;
  13. server 127.0.0.1:8004;
  14. }
  15. sendfile on;
  16. keepalive_timeout 65;
  17. gzip on;
  18. server {
  19. listen 80;
  20. server_name localhost;
  21. charset utf-8;
  22. location / {
  23. root html;
  24. index index.html index.htm;
  25. }
  26. location /msg-sender/ {
  27. proxy_pass http://msg-senders/;
  28. }
  29. #error_page 404 /404.html;
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root html;
  33. }
  34. }
  35. }

本地压测结果:

  1. aliyun ulimit -n 1048576
  2. aliyun ab -k -n 1000000 -c 5000 http://localhost/
  3. This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
  4. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  5. Licensed to The Apache Software Foundation, http://www.apache.org/
  6. Benchmarking localhost (be patient)
  7. Completed 100000 requests
  8. Completed 200000 requests
  9. Completed 300000 requests
  10. Completed 400000 requests
  11. Completed 500000 requests
  12. Completed 600000 requests
  13. Completed 700000 requests
  14. Completed 800000 requests
  15. Completed 900000 requests
  16. Completed 1000000 requests
  17. Finished 1000000 requests
  18. Server Software: openresty/1.11.2.5
  19. Server Hostname: localhost
  20. Server Port: 80
  21. Document Path: /
  22. Document Length: 562 bytes
  23. Concurrency Level: 5000
  24. Time taken for tests: 24.663 seconds
  25. Complete requests: 1000000
  26. Failed requests: 0
  27. Keep-Alive requests: 991975
  28. Total transferred: 805959875 bytes
  29. HTML transferred: 562000000 bytes
  30. Requests per second: 40546.28 [#/sec] (mean)
  31. Time per request: 123.316 [ms] (mean)
  32. Time per request: 0.025 [ms] (mean, across all concurrent requests)
  33. Transfer rate: 31912.76 [Kbytes/sec] received
  34. Connection Times (ms)
  35. min mean[+/-sd] median max
  36. Connect: 0 3 67.1 0 3080
  37. Processing: 0 120 71.5 119 1010
  38. Waiting: 0 119 71.5 118 1010
  39. Total: 0 123 100.1 167 3370
  40. Percentage of the requests served within a certain time (ms)
  41. 50% 167
  42. 66% 185
  43. 75% 187
  44. 80% 188
  45. 90% 191
  46. 95% 194
  47. 98% 197
  48. 99% 199
  49. 100% 3370 (longest request)

每秒可处理40k个请求

联系方式

QQ:437925289

Email:437925289@qq.com