Python Object-oriented & Common modules (ii)

Source: Internet
Author: User
Tags assert disk usage redis server

1. Object-oriented 1.1 exception handling (exceptions, assertions, exception types, throw exceptions, Python exception snaps)

Exception handling

name = "aa"try:   # 这一段是要执行的主体    print(name)# except Exception as msg: # 如果没有正常执行,就执行下面的这一段。Exception 是错误类型。except : # 如果没有正常执行,就执行下面的这一段。Exception 是错误类型(有好多类型,可以百度查下,错误类型 ,尽量写的准确些)。    print("name is err...")else: # 只有当try的那段 正常执行了后,才会执行下面的这一段。    print("welcome: {}.".format(name))finally: # 无论有没有正常执行 都会执行这一段的。    print("this is test.")

Assert assert

格式: assert 表达式, ‘异常错误信息‘# 当表达式不成立时,就会抛后面的错误信息。assert  

Raise Exception type ("Error hint"), and use with try

作用:1.捕获异常为了让程序不至于中断,在逻辑控制之内。2.抛出异常是为了,保证程序在遇到这种问题的时候必须中止。v1 = "wf"li = [1,23]try:    # print(v1)    # print(li[4])    raise  IndexError("索引不存在") # 一般不用# 为啥写这么多except呢?方便记录日志.except IndexError as msg: # 错误类型是索引    print(msg)except Exception as msg: # 所有的错误类型,你可以这么些    print(msg)else:    print("good")finally:    print("test")
1.2 Object-Oriented programming (object-oriented, encapsulation and invocation, indirect invocation, definition class, class member)
def send_mail(msg,mail):    print("msg:{} to mail:{}".format(msg,mail))send_mail("hellow","[email protected]")class mail():    def __init__(self,msg,mail):        #构造方法,无须调用,自动执行        self.msg = msg        self.mail = mail    def send_mail(self):        print("send msg:{} to mail:{}".format(self.msg, self.mail))# 创建一个对象,obj 就是对象。obj是类对象的指针。obj = mail("hello","[email protected]") 他指向mail这个类。obj.send_mail()

When can I use a class? I personally understand:

例如我写一个zabbix 增删改查脚本,每次操作都需要使用zabbix创建好的这个连接。我当然可以写成面向过程或面向函数的这种脚本。但是它的可读性和代码的简洁度不好。我写成一个类,无论是使用还是可读性都会变的很好。
1.3 Inheritance and polymorphism (class inheritance, inheritance order, method overrides)
class c1(): # 父类;基类    def f1(self):        v1 = 1        print(v1)    def f2(self):        v2 = 2        print(v2)class c2(c1): # 子类;派生类,如何继承多个父类呢? c2(c1,c4)    name = "wangfei"   # 静态字段    def __init__(self,age):        self.age = age    def f1(self):        v1 = "c2_2"    # 普通字段        print(v1)    def f3(self):        v3 = 3        print(v3)# obj = c2("18") ## # obj.f1() # 子类的方法是优先父类的方法# # obj.f2() # 子类没有这个方法就到父类去找# re = c2.name # 静态字段通过类来访问的# print(re)# re2 = obj # 普通字段通过对象来访问# print(re2)

What do you mean polymorphic? Parameters of any data type can be passed into the class.

Multiple inheritance considerations.

1. 当只有一条道的时候,就是一条路走到底。子类继承父类,父类继承父父类。2. 当有2条道的时候,左边的的优先于右边的。当左边的道走到第三步还没有走到时,就要返回同级走第二条道到底了。3. 这里需要注意的是,self是代表是第一层的obj 就是最小的那代类。
1.4 Class methods and class properties (class method definition, method invocation, class built-in properties, operator overloading)

Methods of the class

class c1():    v2 = 20    def __init__(self,v1):  # 构造方法        self.v1 = v1    def f1(self):  # 普通方法        print(self.v1)        print(c1.v2)    @staticmethod    def f2():   # 静态方法,通过类来调用;为了节省内存        print(c1.v2)obj1 = c1(120)c1.f2() # 通过类来访问静态方法obj1.f2() # 通过对象来访问

Properties of the class

class c1():    v2 = 20    def __init__(self,v1):        self.v1 = v1    def f1(self):        print(self.v1)        print(c1.v2)    @property # 这个就叫做属性.    def f2(self):        print(c1.v2)    def f3(self):        re = obj1.f2        # print(re)obj1 = c1(120)obj1.f2 # 奇怪吧? 通过访问字段的方式进行使用的
2. Practical application 2.1 Module application requests (RESTAPI standard operation, HTTP request and return operation, API interface call combat)

Requests Module Reference link

Get the status code of the access URL

import  requestsres = requests.get("http://www.baidu.com/")print(res.status_code)

Case 1: Read the URL list, test, if the status code is 40x 50x print out. The production environment is called an interface for alerting.

import requestsurl_list = [‘http://www.baidu.com‘, ‘http://www.sina.com.cn‘, ‘http://adminset.cn‘]err_http_code = [401,402,403,404,500,501,502,503,504,505]for url in url_list:    res = requests.get(url)    if res.status_code in err_htttp_code:        print("access {} http code failed".format(url))

Case 2: The above case reads the URL list from the file and makes a judgment.

import requestserror_code_list = [401,402,403,404,500,501,502,503,504,505]with open("url_list.txt","rb") as url_file:     while True:        url_line = url_file.readline()        if url_line:            str_url_line = str(url_line,encoding="utf-8")            str_url = str_url_line.split("\n")[0] # 从文件里面读取的行,后面是有换行符的。\n的 不能直接使用,需要去掉哦。            res = requests.get(str_url)            if res.status_code in error_code_list:                print("url: {}, status code: {}".format(str_url,res.status_code))        else:            break

If the URL contains https, how do you deal with it?

res = requests.get("https://www.12306.cn",verify = False)print(res)

Requests the API interface for the added host information. Due to the request, token information is required, the URL needs to be stitched down.

# url后面的”“ 表示后面要接参数 ,因为在字典里面定义了(para变量),requests模块加了params后会自动进行 拼接得。# http://115.28.147.154/cmdb/get/host/?token="token信息"&name=“user”para = {‘token‘: ‘PnSlpwJDQE3L‘, ‘name‘: ‘adminset‘}res = requests.get("http://115.28.147.154/cmdb/get/host/", params = para)# 查看拼接后的urlprint(res.url)# 获取结果data = r.textimport json# 进行反序列化d1 = json.loads(data)print(d1[‘data‘][‘ip‘])
2.2 Module Application Redis, MySQL, MongoDB

Redis Reference Links

Redis Client Use Help

MySQL reference link

MongoDB Reference Link

Writes data to the Redis and reads the data.

import  redisredis_conn = redis.StrictRedis(host="127.0.0.1",port=6379,db=0)redis_conn.set("name","wangfei") # 写入数据re = redis_conn.get("name") # 读取数据数据print(re)redis_conn.lpush("list2","python class") # push的是一个列表

Example: Get the hostname and IP address and write to Redis.

import  redisimport  socketdef get_sys_info():    try:        hostname = socket.gethostname()        ip = socket.gethostbyname(hostname)    except Exception as msg:        print(msg)        hostname = ""        ip = ""    finally:        data = {"hostname": hostname,"ip":ip}    return  data# 链接redisredis_conn = redis.StrictRedis("127.0.0.1",6379)try:    # 获取系统信息    re = get_sys_info()    # 将系统信息写到redis,作为列表里的一个元素。    redis_conn.lpush("host",re)except Exception as msg:    print(msg)# 上面这一段或者是这么写。# re = get_sys_info()# assert redis_conn.lpush("host",re),"lpush error"
2.3 Module Application Logging (logging, adding logs for your own program)

Logging Module Official Reference link

level numeric value
critical 50
error 40
30
info 20
debug 10
import logginglogging.basicConfig(level=logging.INFO,                    format=‘%(asctime)s %(levelname)s %(message)s‘,                    datefmt=‘%Y%m%d %H:%M:%S‘,                    filename="./agent.log",                    filemode=‘a‘)log = "redis connecting+++++++++++++"logging.critical(log)
2.4 Module Application Psutil (operating system Information acquisition combat)

Psutil Official Reference link

Psutil Module for System Information

import  psutil# 获取cpu的数量信息print(psutil.cpu_count()) # 逻辑核心数print(psutil.cpu_count(logical=False)) # 物理核心数# 获取所有磁盘分区信息# print(psutil.disk_partitions())res = psutil.disk_partitions()for mount in res:    print(mount[1])# 获取磁盘分区的使用信息# print(psutil.disk_usage("/"))res = psutil.disk_partitions()for mount in res:    print(psutil.disk_usage(mount[1]))# 获取内存信息print(sutil.virtual_memory())print(sutil.virtual_memory().total)
2.5 Module Application Configparser (profile resolution, add profile management for your own programs)

The Configparser module reads the configuration file.

import  configparser# 如何从一个文件里面读取参数?# 读取文件流程# 第1步 初始化模块(我自己想的,帮助记忆理解)config = configparser.RawConfigParser()# 第2步 打开配置参数文件redis_conf_file = open("redis.conf","r")# 第3步 读取所有参数内容config.read_file(redis_conf_file)# 第4步 获取对应key下面的参数。print(config.get("redis","host"))print(config.get("redis","port"))-----------------------备注:redis.conf内容    [redis]    host = 127.0.0.1    port = 6379

Example: Read Redis address and port information from the configuration file and write a message to Redis.

import  redis# python2 python3 导入configparser模块名不一样。 try:    import configparserexcept Exception as msg:    print(msg)    import  ConfigParserconfig = configparser.RawConfigParser()# 打开配置文件,读取内容try:    with open("redis.conf","r") as redis_conf_file:        config.read_file(redis_conf_file)        host = config.get("redis","host")        port = config.get("redis","port")except Exception as msg:    print(msg)# 写信息到redis里面去try:    redis_conn = redis.StrictRedis(host,port,0)    redis_conn.set("test_key","test_values")except Exception as msg:    print(msg)
2.6 Module Application Schedule (Python scheduled task combat)
# 定时任务模块# time.sleep 是整个脚本程序都中断了,任务是串行的。使用schedule 是异步执行的。import  scheduleimport  timeimport  threadingdef hello(): 60s    print("2 hello")def morning(): 3600s    print("1 morning")def job_thread(func): # 多线程    t = threading.Thread(target=func)    t.start()schedule.every(2).seconds.do(job_thread, hello) # 每隔2秒执行一次任务。schedule.every(1).seconds.do(job_thread, morning) # 每隔1秒 执行一次任务。while True: # 这个是固定的写法,每隔1s 检查查定时任务    schedule.run_pending()    time.sleep(1)
2.7 XLRD/XLWT (for reading and writing to Excel files)
import  xlrd # 读取excel文件内容#### 从Excel文件读取内容zabbix_file = xlrd.open_workbook("./zabbix-host.xls")zabbix_file_table = zabbix_file.sheet_by_name("sheet01")print(zabbix_file_table.nrows) # 获取行数print(zabbix_file_table.row_values(0)) # 获取指定行的内容print( zabbix_file_table.ncols) # 获取列数print(zabbix_file_table.col_values(2)) # 获取指定列的内容print(zabbix_file_table.cell(1,2)) # 获取1行2列内容#### 向Excel文件写入内容import xlwt # 向Excel文件里写入内容zabbix_file = xlwt.Workbook(encoding="ascii") # 指定编码file_sheet = zabbix_file.add_sheet("sheet02") # 新建工作簿file_sheet.write(0,1,label = "hehe") # 写入内容file_sheet.write(0,2,label = "hehe")file_sheet.write(0,4,label = "hehe")file_sheet.write(0,3,label = "hehe")zabbix_file.save("test.xls") # 保存
3. Actual Case 3.1 Call Zabbix API, delete and change the host.
From Pyzabbix import zabbixapiimport xlrdimport configparserimport loggingimport sysimport osclass Zabbix (): Def __ini                            T__ (self,host_file): self.host_file = host_file # log module logging.basicconfig (Level=logging.info,                             format= '% (asctime) s% (levelname) s% (message) s ', datefmt= '%y%m%d%h:%m:%s ',        Filename= "./zabbix.log", filemode= ' a ') # Read Zabbix configuration file Try:config = Configparser. Rawconfigparser () with open ("/users/feiwang/pycharmprojects/python-study2/work/zabbix.conf", "R") as Zabbix_conf                _file:config.read_file (zabbix_conf_file) Self.zabbix_user = Config.get ("Zabbix", "user") SELF.ZABBIX_PASSWD = Config.get ("Zabbix", "passwd") except Exception as Msg:logging.error (ms g) Raise msg # login Zabbix Try:self.zapi = Zabbixapi ("http://192.168.4.135/zabbix ") Self.zapi.login (SELF.ZABBIX_USER,SELF.ZABBIX_PASSWD) logging.info (" Connecting Zabbix suc        Cessful. ") Except Exception as msg:logging.error ("Connecting Zabbix failed" + "detail:{}". Format (msg)) Raise M            SG def Load_zabbix (self): # read Excel file Try:zabbix_file = Xlrd.open_workbook (self.host_file)        zabbix_file_table = Zabbix_file.sheet_by_name ("sheet01") logging.info ("Open Host.xls OK.")        Except Exception as Msg:logging.error (msg) zabbix_file = ' zabbix_file_table = ' # read Zabbix information host_list = [] for row in range (1, zabbix_file_table.nrows): Row_values = Zabbix_ File_table.row_values (row) Visible_name = row_values[1] hostname = row_values[0] Port = r OW_VALUES[-1] IP = row_values[-2] group_id = "2" Create_host = {"Host": H  Ostname,              "description": Visible_name, "name": Visible_name, "Interfaces": [                        {"Type": 1, "main": 1, "Useip": 1,                "IP": IP, "DNS": "", "Port": Port},], "Groups": [{"GroupID": group_id},],} host_list.append (Create_                Host) return Host_list def add_host (self): to host in Self.load_zabbix (): Try:            Self.zapi.host.create (host) logging.info ("Create Status:ok, create host info:{}". Format (host)) Except Exception as Msg:print (msg) logging.error ("Zabbix Create hosts failed. {} ". Format (msg)) # # del host def Del_host (self):" Loop Zabbix gets to the host and then compares it to the host in the local Excel file to be deleted, if yes, then        Just erase him. Delete with Hostid:retuRN: ' For Host in Obj.zapi.host.get (): For Del_host in Self.load_zabbix (): If Ho                        st["host"] = = del_host["Host"]: Try:self.zapi.host.delete (host["HostID"]) Print ("Delete host{} is ok.". Format (host["host"])) Logging.info ("Del host:{} OK". Format (host["HostID"]) exce PT Exception as Msg:print (msg) logging.error ("Del host:{} failed,". Format (H ost["host"])) if __name__ = = "__main__": Action,file = sys.argv[1:] Assert os.path.exists (file), "file: ' {} ' doesn ' t ex Ists ". Format (file) obj = Zabbix (file) if action = =" Add ": Obj.add_host () elif action = =" del ": obj. Del_host () elif action = = "Update": pass elif action = = "get": Pass Else:print ("action is E        RR. ") Raise Exception
3.2 Develop an agent to collect system information.

Requirements:

    1. Get CPU Memory disk (disk usage information) host name host IP address and other information
    2. Assembled into the JSON format to upload to Redis.
    3. Require logging
    4. Timed execution is updated every 10 minutes.
    5. Execute with Multithreading

Agent script Content

Import jsonimport platformimport scheduleimport psutilimport configparserimport redisimport loggingimport Threadingimport socketimport timeimport mathdef log (): Logging.basicconfig (Level=logging.info, F                        ormat= '% (asctime) s% (levelname) s% (message) s ', # log format datefmt= '%y%m%d%h:%m:%s ', # time format Filename= "./agent.log", # log file path filemode= ' a ') # File open mode return Logging.basicco Nfiglog () def get_sys (): Try:hostname = socket.gethostname () IP = socket.gethostbyname (hostname) EXCEP T Exception as Msg:print (MSG) hostname = "" "IP =" "data = {" hostname ": hostname," IP ": IP} retur n datadef get_cpu (): cpu_count_logical = Psutil.cpu_count (logical=false) cpu_count_phycial = Psutil.cpu_count (logic al=true) cpu_percent = psutil.cpu_percent (interval=2) data = {"Phycial_count": cpu_count_phycial, "logical": Cpu_count_ Logical, "percent": cpu_percent} return Datadef Get_mem (): # Math.ceil (decimal) takes an integer mem_total = Math.ceil (Psutil.virtual_memory (). total/1024/1024/1024) Mem_ Percent = Psutil.virtual_memory (). Percent data = {"Mem_total": Mem_total, "mem_percent": mem_percent} return datadef ge T_disk (): Disk_list = [] All_disk_part = Psutil.disk_partitions () for partition in All_disk_part:mount_po int = partition[1] Mount_total = Math.ceil (Psutil.disk_usage (mount_point). total/1024/1024/1024) Mount_percen t = Psutil.disk_usage (mount_point). Percent data = {"Mount": Mount_point, "Total": Mount_total, "percent": mount_percent } disk_list.append (data) return Disk_listdef Thread_run (): Passdef agent_info (): Sys_data = {} Sys_dat a["host"] = Get_sys () sys_data["disk"] = Get_disk () sys_data["mem"] = Get_mem () sys_data["CPU"] = GET_CPU () re Turn Json.dumps (sys_data) # to Deserialize Def Put_redis (): config = configparser. Rawconfigparser () Try:with open ("redis.conf", "R") as Redis_conf_file:config.read_file (redis_conf_file) redis_host = Config.get ("Redis", "host") Red        Is_port = Config.get ("Redis", "Port") except Exception as Msg:redis_host = "127.0.0.1" Redis_port = 6379        Print (msg) try:logging.info ("Connect Redis server.") Redis_conn = Redis.        Strictredis (redis_host,redis_port,0) logging.info ("Put SystemInfo to Redis server.") Redis_conn.lpush ("Host", Xagent_info ()) Logging.info ("data:{}". Format (Agent_info ())) Logging.info ("Put succes Sful ... ") def thread_job (func): Target =threading. Thread (Target=func) Target.start () if __name__ = = "__main__": Schedule.every (2). Seconds.do (Thread_job,put_redis) W     Hile True:schedule.run_pending () time.sleep (1)

Python Object-oriented & Common modules (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.