我是个新手,所以请原谅我的基础知识。我有一个Python脚本,用于 接收SMTP信息'服务器'并将其发送到SMS服务提供商的API。脚本运行在Ubuntu服务器操作系统18.04上,并使用了以下功能 nohup和&。 ... 虽然我的功能没有问题,但是每天早上05:00到05:30之间,我的服务器都会有异常的延迟响应(我从PRTG和日志中都可以看到)。从下面的代码日志可以看到,功能仍然可以工作,但它的请求比正常的等待时间更长'这样做使得发送的代码与短信无法使用'。
非常感谢您的帮助。
操作系统信息。
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:<[email protected]>'
2020-06-04 10:35:25,328 ('<IP>', 16750) sender: [email protected]
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'RCPT TO:<+9*******[email protected]>'
2020-06-04 10:35:25,329 ('<IP>', 16750) recip: +9*******[email protected]
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:<[email protected]>'
2020-06-04 05:12:12,801 ('<IP>', 6845) sender: [email protected]
2020-06-04 05:12:12,801 ('<IP>', 10667) Data: b'MAIL FROM:<[email protected]>'
2020-06-04 05:12:12,802 ('<IP>', 10667) sender: [email protected]
2020-06-04 05:12:12,802 ('<IP>', 24380) Data: b'MAIL FROM:<[email protected]>'
2020-06-04 05:12:12,802 ('<IP>', 24380) sender: [email protected]
2020-06-04 05:12:12,802 ('<IP>', 23987) Data: b'MAIL FROM:<[email protected]>'
2020-06-04 05:12:12,802 ('<IP>', 23987) sender: [email protected]
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
简短的回答:我有一个Python脚本接收SMTP消息'服务器',并将其发送到SMS服务提供商的API上,该脚本运行在Ubuntu服务器操作系统18.04上,并带有..:
我可以看到每条日志信息之间的间隔时间是1分40秒,这不可能是巧合。
检查一下你的SMS网关的限制是什么--你可以在一分钟内发送多少条信息,一旦发送了一定数量的信息,是否有任何整形算法被应用。
更长的答案。
从日志中可以看出,python处理并发连接没有任何延迟,但最后的POST请求却有延迟。这说明你的服务器应该有足够的CPUMemory来工作,但最后的操作是瓶颈。因为你是用async运行的,这应该不是Python方面的问题,而是接收方面的问题--SMS网关。这个服务很可能会接收你所有的请求并将它们添加到某个队列中,所以在你的终端上是否异步处理这些请求并不重要。
这个问题与PRTGmonitoring无关,但你可以证明我是错的--在服务器上添加一些传感器,观察磁盘IO、CPU负载、内存使用情况,看看服务器在白天或晚上是否有任何性能问题。