发表于: 2018-01-05 23:21:08
2 924
今天完成的事情:
简单的配置nginx和uwsgi的配置文件,使用Nginx,uwsgi,flask建立一个简单的网站,并编写脚本统计网站每天前10个请求,并通过邮件发送。
统计网站前10个请求脚本如下:
import datetime
import time
import shutil
import re
from email.mime.text import MIMEText
import smtplib
temp=re.compile(r'\d+.\d+.\d+.\d+ - - \[\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2} \+\d{4}\] "(\w+ /.*) HTTP.*" .*')
baktime=datetime.datetime.now()+datetime.timedelta(days=1)
baktime=str(baktime)[:10]+' 00:00:00'
temptime=str(datetime.datetime.now())[:10]+' 23:55:00'
#备份
def backup():
shutil.copy('/home/bob/nginx/logs/access.log','./logs/{}.log'.format(temptime[:10]))
with open('/home/bob/nginx/logs/access.log','w') as f:
f.write('')
#统计,并通过列表形式返回前10个请求
def tongji(date):
with open('/home/bob/python/venv/logs/'+date+'.log') as f:
txt=f.readlines()
llist=[]
for i in txt:
te=temp.findall(i)[0]
if te:
llist.append(te)
l=list(set(llist))
numb=[]
for i in l:
numb.append(llist.count(i))
dic={}
for i in range(len(l)):
dic[l[i]]=numb[i]
dic=sorted(dic.items(),key=lambda key:key[1],reverse=True)
ll=[]
for i,j in dic:
if len(ll)<10:
ll.append(i)
return ll
#发送邮件
password=input('password:')
def sendmail(password,llist):
ms=''
for i in llist:
ms=ms+i+' \n '
msg=MIMEText(ms,'plain','utf-8')
server=smtplib.SMTP('smtp.163.com',25)
#server.starttls()
server.set_debuglevel(1)
server.login('15637589942@163.com',password)
server.sendmail('15637589942@163.com',['15637589942@163.com'],msg.as_string())
server.quit()
while True:
now=str(datetime.datetime.now())[:19]
if now < temptime:
time.sleep(180)
else:
if now < baktime:
time.sleep(1)
else:
backup()
print('{}backup'.format(baktime))
#tongji
date=temptime[:10]
llist=tongji(date)
#sendmail
sendmail(password,llist)
print('sendmail ok')
temptime=str(datetime.datetime.now())[:10]+' 23:55:00'
baktime=datetime.datetime.now()+datetime.timedelta(days=1)
baktime=str(baktime)[:10]+' 00:00:00'
邮件截图如下:
明天计划的事情:
学习mangodb数据库,用数据库存储数据
遇到的问题:
不能让局域网设备访问网站,更不能让公网设备访问网站,发送邮件的时候,命令终端弹出很多信息,不知道怎么消除。
收获:学习了基本的nginx+uwsgi+flask的配置,列表转集合,字典按值排序,但结果不是字典了,而是里面是元组的列表,发送文本邮件。
评论