我是新手,所以请原谅我的基本知识。我有一个Python来接收SMTP消息‘’并将其发送到SMS服务提供商的API上,该脚本正在运行于带有nohup和&的UbuntuServerOS18.04上。虽然我对功能没有问题,但每天早上05:00至05:30之间,服务器的响应都会异常延迟(我从PRTG和日志中都可以看出这一点)。从下面的代码日志中,您可以看到该功能仍然有效,但是它使请求等待的时间比正常时间长,这样就使得发送的带有SMS的代码变得不可用了。
您的帮助将不胜感激。
OS信息:
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-101-generic x86_64)
System load: 0.08 Processes: 193
Usage of /: 9.8% of 97.93GB Users logged in: 0
Memory usage: 9% IP address for ens160: XXXX
Swap usage: 0%Python代码:
import asyncio
from aiosmtpd.controller import Controller
import asyncore
import time
import requests
import logging
from smtplib import SMTP as Client
logging.basicConfig(filename='smtpServer.log',level=logging.INFO, format='%(asctime)s %(message)s')
class DataHandler: #this is the server taking the data and processing it.
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
envelope.rcpt_tos.append(address)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
msg_content = envelope.content.decode('utf8', errors='replace')
sent_to = envelope.rcpt_tos[0].split('@')[0][1:]
api_key = "API_KEY"
word_list = msg_content.split()
code = " "
if 'AuthCode:' in word_list:
code_index = word_list.index('AuthCode:')
code = word_list[code_index+1]
msg= 'Your Token Code to access XXX VPN is ' + code +'. Do not share the password with anyone'
headers = {'Content-Type': 'application/json'}
--- code omitted ---
elif sent_to[:2] == '90': #if it's a local number 'starting with 90'
try:
url = "SMS_ServiceProvider_URL"
payload = '{"api_key":"'+api_key+'","title":"8507013986","text":"'+msg+'","sentto":"'+sent_to+'"}'
print('{} Code has been sent to +{}'.format(code, sent_to))
requests.request("POST", url, headers=headers, data = payload)
logging.info('{} Code has been sent to +{} by local server'.format(code, sent_to))
except Exception as e:
raise e ('Failed sending to local number ERROR')
--- code omitted ---
return '250 Message accepted for delivery'
controller = Controller(DataHandler(), hostname='XXXX', port=25)
controller.start()
while True:
time.sleep(60)
print(end='')日志消息:
-正常情况:
2020-06-04 10:35:25,327 Peer: ('<IP>', 16750)
2020-06-04 10:35:25,327 ('<IP>', 16750) handling connection
2020-06-04 10:35:25,327 ('<IP>', 16750) Data: b'EHLO example.com'
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 10:35:25,328 ('<IP>', 16750) sender: example@example.com
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'RCPT TO:<+9*******9794@10.90.214.135>'
2020-06-04 10:35:25,329 ('<IP>', 16750) recip: +9*******9794@10.90.214.135
2020-06-04 10:35:25,329 ('<IP>', 16750) Data: b'DATA'
2020-06-04 10:35:26,568 043396 Code has been sent to +9*******9794 by local server
2020-06-04 10:35:26,569 ('<IP>', 16750) Data: b'QUIT'
2020-06-04 10:35:26,570 ('<IP>', 16750) connection lost-停工期间:
2020-06-04 05:12:12,797 Peer: ('<IP>', 59450)
2020-06-04 05:12:12,797 Peer: ('<IP>', 6845)
2020-06-04 05:12:12,797 Peer: ('<IP>', 59798)
2020-06-04 05:12:12,797 Peer: ('<IP>', 10667)
2020-06-04 05:12:12,797 Peer: ('<IP>', 24380)
2020-06-04 05:12:12,797 Peer: ('<IP>', 60299)
2020-06-04 05:12:12,797 Peer: ('<IP>', 23987)
2020-06-04 05:12:12,797 ('<IP>', 7834) connection lost
2020-06-04 05:12:12,798 Connection lost during _handle_client()
2020-06-04 05:12:12,798 ('<IP>', 18527) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 9209) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 59450) handling connection
2020-06-04 05:12:12,798 ('<IP>', 6845) handling connection
2020-06-04 05:12:12,798 ('<IP>', 59798) handling connection
2020-06-04 05:12:12,797 ('<IP>', 7834) connection lost
2020-06-04 05:12:12,798 Connection lost during _handle_client()
2020-06-04 05:12:12,798 ('<IP>', 18527) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 9209) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 59450) handling connection
2020-06-04 05:12:12,798 ('<IP>', 6845) handling connection
2020-06-04 05:12:12,798 ('<IP>', 59798) handling connection
2020-06-04 05:12:12,798 ('<IP>', 10667) handling connection
2020-06-04 05:12:12,799 ('<IP>', 24380) handling connection
2020-06-04 05:12:12,799 ('<IP>', 60299) handling connection
2020-06-04 05:12:12,799 ('<IP>', 23987) handling connection
2020-06-04 05:12:12,799 ('<IP>', 18527) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,799 ('<IP>', 9209) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,799 ('<IP>', 59798) EOF received
2020-06-04 05:12:12,799 ('<IP>', 59450) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,800 ('<IP>', 59798) connection lost
2020-06-04 05:12:12,800 ('<IP>', 6845) Data: b'EHLO example.com'
2020-06-04 05:12:12,800 ('<IP>', 10667) Data: b'EHLO example.com'
2020-06-04 05:12:12,800 ('<IP>', 24380) Data: b'EHLO example.com'
2020-06-04 05:12:12,801 ('<IP>', 60299) Data: b'EHLO TWPRTGIST01'
2020-06-04 05:12:12,801 ('<IP>', 6845) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,801 ('<IP>', 6845) sender: example@example.com
2020-06-04 05:12:12,801 ('<IP>', 10667) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 10667) sender: example@example.com
2020-06-04 05:12:12,802 ('<IP>', 24380) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 24380) sender: example@example.com
2020-06-04 05:12:12,802 ('<IP>', 23987) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 23987) sender: example@example.com
2020-06-04 05:12:12,803 ('<IP>', 60299) Data: b'QUIT'
2020-06-04 05:12:12,803 ('<IP>', 6845) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 6845) recip: +9**********@<IP>
2020-06-04 05:12:12,803 ('<IP>', 10667) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 10667) recip: +9**********@<IP>
2020-06-04 05:12:12,803 ('<IP>', 24380) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 24380) recip: +9**********@<IP>
2020-06-04 05:12:12,804 ('<IP>', 60299) connection lost
2020-06-04 05:12:12,804 Connection lost during _handle_client()
2020-06-04 05:12:12,804 ('<IP>', 23987) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,804 ('<IP>', 23987) recip: +9**********@<IP>
2020-06-04 05:12:12,804 ('<IP>', 6845) Data: b'DATA'
2020-06-04 05:12:12,804 ('<IP>', 10667) Data: b'DATA'
2020-06-04 05:12:12,804 ('<IP>', 24380) Data: b'DATA'
2020-06-04 05:12:12,805 ('<IP>', 23987) Data: b'DATA'
2020-06-04 05:13:52,957 708496 Code has been sent to +9********** by local server
2020-06-04 05:15:33,051 619421 Code has been sent to +9********** by local server
2020-06-04 05:17:13,215 035670 Code has been sent to +9********** by local server
2020-06-04 05:18:53,341 861227 Code has been sent to +9********** by local server发布于 2020-06-08 14:01:38
简短回答:
从盒子里想出来,我可以看到,每条记录下来的信息之间的距离是1分40秒--这绝不是巧合。
检查什么是您的短信网关的限制-您可以立即发送多少条消息,如果有任何成形算法正在应用,一旦一定数量的发送。
更长的答案:
从日志中我可以看到python处理并发连接没有任何延迟,但是最后的POST请求被延迟了。这意味着您的服务器应该有足够的CPU/内存来工作,但最后一次操作是瓶颈。因为您使用异步运行它,所以它不应该是Python端的问题,而应该是在接收端- SMS网关。这个服务很可能是接收所有的请求并将它们添加到某个队列中,所以如果在您的端异步处理它们并不重要。
据我所知,这个问题与PRTG/监视无关,但您仍然可以证明我是错的--在服务器上添加几个传感器来监视磁盘I/O、CPU负载、内存使用情况,并查看服务器在白天或夜间是否有任何性能问题。
https://stackoverflow.com/questions/62189427
复制相似问题