在大的互联网公司干技术的基本都会碰到测试、预发布、线上这种多套环境的,来实现测试和线上正式环境的隔离,这种情况下,就难免会碰到秀逗了把测试的链接发布到线上的情况,一般这种都是通过一些测试的检查工具来检查链接来规避风险的。前两天跟了一个问题也是这个情况,开发疏忽把日常url发布到线上了。但是测试那边没有自动化的监控工具,导致没有及时发现,由于最近正好在看python,后来处理完回家就想用python做个简单的监控。
大略思路是:用python写一个脚本来分析网页里的所有url,看是否包含日常的链接,然后把脚本放到crontab里跑定时任务,10分钟跑一次检查。如果发现非法链接,就发告警邮件给相关人员。脚本代码100行左右,比较好理解,贴上代码。
本来想用beautifulsoup,不过考虑到安装三方库麻烦,所以还是用自带的sgmllib来,不需要关心库。发邮件函数没有实现,根据各自的smtp服务器实现以下即可。
代码如下:
#!/usr/bin/env python
#coding:UTF-8
import urllib2
from sgmllib import SGMLParser
import smtplib
import time
#from email.mime.text import MIMEText
#from bs4 import BeautifulSoup
#import re
class UrlParser(SGMLParser):
urls = []
def do_a(self,attrs):
'''''parse tag a'''
for name,value in attrs:
if name=='href':
self.urls.append(value)
else:
continue
def do_link(self,attrs):
'''''parse tag link'''
for name,value in attrs:
if name=='href':
self.urls.append(value);
else:
continue
def checkUrl(checkurl, isDetail):
'''''检查checkurl对应的网页源码是否有非法url'''
parser = UrlParser()
page = urllib2.urlopen(checkurl)
content = page.read()
#content = unicode(content, "gb2312").encode("utf8")
parser.feed(content)
urls = parser.urls
dailyUrls = []
detailUrl = ""
for url in urls:
if 'daily' in url:
dailyUrls.append(url);
if not detailUrl and not isDetail and 'www.bc5u.com' in url:
detailUrl = url
page.close()
parser.close()
if isDetail:
return dailyUrls
else:
return dailyUrls,detailUrl
def sendMail():
'''''发送提醒邮件'''
pass
def log(content):
'''''记录执行日志'''
logFile = 'checkdailyurl.log'
f = open(logFile,'a')
f.write(str(time.strftime("%Y-%m-%d %X",time.localtime()))+content+'\n')
f.flush()
f.close()
def main():
'''''入口方法'''
#检查ju
url = "www.bc5u.com"
dailyUrls,detailUrl=checkUrl(url, False)
if dailyUrls:
#检查到daily链接,发送告警邮件
sendMail()
log('check: find daily url')
else:
#没检查到daily链接,不处理
log('check: not find daily url')
#检查judetail
dailyUrls=checkUrl(detailUrl, True)
if dailyUrls:
#检查到daily链接,发送告警邮件
log('check: find daily url')
sendMail()
else:
#没检查到daily链接,不处理
log('check: not find daily url')
if __name__ == '__main__':
main()

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment