openresty 学习笔记番外篇:python的一些扩展库
openresty 学习笔记番外篇:python的一些扩展库
要写一个可以使用的python程序还需要比如日志输出,读取配置文件,作为守护进程运行等
读取配置文件
使用自带的ConfigParser模块
import os
import ConfigParser
获取配置文件路径并进行读取
path = os.path.realpath(__file__)
path = os.path.dirname(path)
configPath = path + '/config.cfg' config = ConfigParser.ConfigParser()
config.read(configPath)
使用方法
agentConfig = {}
agentConfig['MYSQLport'] = config.get('MySQL', 'port')
日志输出
使用自带的logging库
import logging
import logging.handlers
定义日志级别
loggingLevelMapping = {
'debug': logging.DEBUG,
'info': logging.INFO,
'error': logging.ERROR,
'warn': logging.WARN,
'warning': logging.WARNING,
'critical': logging.CRITICAL,
'fatal': logging.FATAL,
}
customLogging = config.get('Logging', 'logging_level')
agentConfig['LOGGINGlevel'] = loggingLevelMapping[customLogging.lower()]
定义日志文件路径和格式
logFile = agentConfig['LOGGINGfile']
handler = logging.handlers.RotatingFileHandler(logFile, maxBytes=10485760, backupCount=10) # 10MB files
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) mainLogger = logging.getLogger('main')
mainLogger.setLevel(agentConfig['LOGGINGlevel'])
mainLogger.addHandler(handler)
使用方法 mainLogger.info(" Messages Done")
mainLogger.debug(" ACK Messages")
守护进程
使用第三方库Python daemonizer class
from daemon import Daemon
定义一个类继承守护进程并且有run()方法
class pantalaimon(Daemon):
def run(self):
# Do stuff pidFile = agentConfig['DAEMONpid']
daemon = pantalaimon(pidFile)
对守护进程的各种操作
if 'start' == sys.argv[1]:
daemon.start() elif 'stop' == sys.argv[1]:
daemon.stop() elif 'restart' == sys.argv[1]:
daemon.restart() elif 'foreground' == sys.argv[1]:
daemon.run() elif 'status' == sys.argv[1]:
try:
pf = file(pidFile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
except SystemExit:
pid = None if pid:
print 'is running as pid %s.' % pid
else:
print 'is not running.' else:
print 'Unknown command'
sys.exit(1)
openresty 学习笔记番外篇:python的一些扩展库的更多相关文章
- openresty 学习笔记番外篇:python访问RabbitMQ消息队列
openresty 学习笔记番外篇:python访问RabbitMQ消息队列 python使用pika扩展库操作RabbitMQ的流程梳理. 客户端连接到消息队列服务器,打开一个channel. 客户 ...
- 《30天自制操作系统》学习笔记--番外篇之Mac环境下的工具介绍
这几天又有点不务正业了,书也没看,一直在搞这个破环境,尝试各种做法,网上各种垃圾信息,浪费了很多时间,说的基本都是废话,不过还是找到了一些,赶紧写下来,不然这个过几天又忘了 首先是环境,我用的是Max ...
- Python学习-day10(番外篇) 阻塞IO 非阻塞IO 同步IO 异步IO
这个章节的内容是关于IO的概念,谈一谈什么是 阻塞IO 非阻塞IO 同步IO 异步IO.以下摘要是我对这四种IO的一个形象理解. 场景是去去银行办理业务.节点有三个,1)到银行提交申请:2)取号:3) ...
- H5学习_番外篇_PHP数据库操作
1. 文件操作 1.1 打开关闭文件 fopen() resource fopen ( string filename, string mode [, bool use_include_path [, ...
- vue学习【番外篇】vue-cli脚手架的安装
大家好,我是一叶,今天和大家分享的是vue-cli脚手架的安装,关于vue-cli的优点,我就不赘述了. 一.检查安装node 安装vue-cli之前,先检查node是否安装.win+R,输入cmd打 ...
- OpenCV 学习笔记(1-1)opecv3.41及其扩展库在VS2015下配置
其他正常 opencv_aruco341.lib opencv_bgsegm341.lib opencv_bioinspired341.lib opencv_calib3d341.lib opencv ...
- 痞子衡嵌入式:超级下载算法(RT-UFL)开发笔记番外(1) - JLinkScript妙用
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是超级下载算法开发笔记番外篇之JLinkScript妙用. JLinkScript 文件是配套 J-Link 调试器使用的脚本,这个脚本适 ...
- 给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV
这次博客园的排版彻底残了..高清版请移步: https://zhuanlan.zhihu.com/p/24425116 本篇是前面两篇教程: 给深度学习入门者的Python快速教程 - 基础篇 给深度 ...
- Netty学习笔记(番外篇) - ChannelHandler、ChannelPipeline和ChannelHandlerContext的联系
这一篇是 ChannelHandler 和 ChannelPipeline 的番外篇,主要从源码的角度来学习 ChannelHandler.ChannelHandler 和 ChannelPipeli ...
随机推荐
- 脱壳——UPX脱壳原理(脱壳helloworld)
脱壳--UPX脱壳原理 脱壳步骤 1 找到OEP 2 dump(导出)内存文件 3 修复 1 找到OEP 1 程序运行先从壳代码运行,壳代码执行完之后会跳转到真正的OEP,也就是是说第一步,首先要找到 ...
- 1090 Highest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 805. Split Array With Same Average
In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...
- CVPR2021 | 华为诺亚实验室提出Transformer in Transformer
前言: transformer用于图像方面的应用逐渐多了起来,其主要做法是将图像进行分块,形成块序列,简单地将块直接丢进transformer中.然而这样的做法忽略了块之间的内在结构信息,为此,这篇论 ...
- 千位分隔符,音频音量加强,transform 垂直居中
1.最近做阴阳师日本官网,其中有个功能是获取预约人数,设计稿上的人数是这样151,567,000,想了想自己写还有点麻烦,于是网上copy了代码,再修改了下. 其中,有一点需要注意的是:函数中的str ...
- 【Springboot】Springboot监听器Demo
/** * @author: yq * @date: 2020/8/31 0:01 * @description 自定义事件 */ @Data public class MyEvent extends ...
- Think5之ajax批量删除数据功能
//批量删除学员信息 public function deleteMany() { $id = input('post.'); //判断id是数组还是一个数值 if(is_array($id)){ f ...
- think php 5.x
# Exploit Title: thinkphp 5.X RCE # Date: 2019-1-14 # Exploit Author: vr_system # Vendor Homepage: h ...
- 路由器逆向分析------QEMU的下载和安装(Linux平台)
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/68953160 一.QEMU源码的下载和编译 QEMU源码的github下载地址:h ...
- 解决Metasploit中shell乱码的问题
我们在kali中用 Metasploit 获取到windows主机的shell后,会出现乱码问题,归根到底,就是windows和linux系统的中文编码不同,所以导致windows系统上的中文在lin ...