为了检测多个网站及检测网站ssl证书有效性,写了一个在线测试检测工具。
实现功能 自由添加和编辑站点信息 单选及多选检测 支持http和https检测 当有ssl证书时,检测证书的有效状态以及过期时间 查看检测历史
环境 python3 node-8.9.1
框架及数据库 后端:tornado 前端:vue 数据库:mongo
http检测状态实现原理 通过 python requests 库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import requeststry : web_url = 'http://...' r = requests.get(web_url, timeout=10 ) print ('请求成功' ) ... return True ; except Exception as e: print (e) return False ;
https 检测状态及ssl证书状态测试实现原理 运用 socket 库建立连接,ssl库获取ssl证书的公钥,在通过openssl的crypto通过公钥解密获取ssl证书里的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import sslimport socketfrom OpenSSL import cryptoweb_url = 'https://...' context = ssl.SSLContext(ssl.PROTOCOL_TLS) sock = socket.socket(socket.AF_INET) sock.settimeout(10 ) wrappedSocket = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=web_url try : wrappedSocket.connect(web_url, 443 )) pem_cert = ssl.DER_cert_to_PEM_cert(wrappedSocket.getpeercert(True )) wrappedSocket.close() io_cert = crypto.load_certificate(crypto.FILETYPE_PEM, pem_cert) ssl_time = io_cert.get_notAfter().decode()[:-1 ] ssl_not_after = ssl_time[0 :4 ] + '年' + ssl_time[4 :6 ] + '月' + ssl_time[6 :8 ] + '日' + ssl_time[8 :10 ] + '时' + ssl_time[10 :12 ] + '分' + ssl_time[12 :14 ] + '秒' ssl_expired = io_cert.has_expired() return True ; except Exception as e: print (e) return False ;
遇到问题 1.webpack打包时遇到
1 You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions , or use the compiler-included build.
解决办法,在webpack配置中增加
1 2 3 4 5 resolve : { alias : { vue : 'vue/dist/vue.js' , } }
资料
ssl — TLS/SSL wrapper for socket objects - https://docs.python.org/3.2/library/ssl.html#ssl.SSLSocket.getpeercert