python 物件導向&常用模組(二)

來源:互聯網
上載者:User

標籤:物件導向   常用模組   

1.物件導向1.1 異常處理(異常、斷言、異常類型、拋出異常、Python異常捕捉)

異常處理

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  name == "aa","name not wf" 

raise 異常類型("錯誤提示") ;和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 物件導向編程(物件導向基礎、封裝和調用、間接調用、定義類、類成員)
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()

什麼時候能用到類呢? 我個人理解:

例如我寫一個zabbix 增刪改查指令碼,每次操作都需要使用zabbix建立好的這個串連。我當然可以寫成面向過程或面向函數的這種指令碼。但是它的可讀性和代碼的簡潔度不好。我寫成一個類,無論是使用還是可讀性都會變的很好。
1.3 繼承與多態(類的繼承、繼承順序、方法重寫)
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)

啥叫多態呢? 可以往類裡傳入任何資料類型的參數。

多繼承注意事項。

1. 當只有一條道的時候,就是一條路走到底。子類繼承父類,父類繼承父父類。2. 當有2條道的時候,左邊的的優先於右邊的。當左邊的道走到第三步還沒有走到時,就要返回同級走第二條道到底了。3. 這裡需要注意的是,self是代表是第一層的obj 就是最小的那代類。
1.4 類方法與類屬性(類方法定義、方法調用、類內建屬性、運算子多載)

類的方法

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() # 通過對象來訪問

類的屬性

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.實戰應用2.1 模組應用Requests(RESTAPI標準操作、HTTP請求與返回操作、API介面調用實戰)

requests模組 參考連結

擷取訪問url的狀態代碼

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

案列1:讀取url列表,進行測試,如果狀態代碼是40x 50x 的就列印出來。生產環境就是調用介面進行警示。

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))

案例2:上方案例從檔案中讀取url列表,進行判斷。

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

如果url是包含https,怎麼處理呢 ?

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

請求api介面,擷取添加的主機資訊。由於請求的時候,需要token資訊,url需要拼接下。

# 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 模組應用Redis、MySQL、MongoDB

redis參考連結

redis用戶端使用協助

mysql參考連結

mongodb參考連結

向redis裡面寫入資料,讀取資料。

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的是一個列表

例子:擷取主機名稱和ip地址 ,然後寫到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 模組應用logging(日誌記錄,為自己的程式添加日誌)

logging模組官方參考連結

Level Numeric value
CRITICAL 50
ERROR 40
WARNING 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 模組應用psutil(作業系統資訊採集實戰)

psutil官方參考連結

psutil 模組擷取系統資訊

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 模組應用configparser(設定檔解析,為自己的程式增加設定檔管理)

configparser 模組讀取設定檔。

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

例子: 從設定檔裡面讀取redis 地址和連接埠資訊,然後寫一個資訊到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 模組應用schedule(Python計劃任務實戰)
# 定時任務模組# 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 (實現對excel檔案的讀寫)
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.實戰案例3.1 調用Zabbix API ,增刪改主機。
from  pyzabbix import ZabbixAPIimport xlrdimport configparserimport loggingimport sysimport osclass zabbix():    def __init__(self,host_file):        self.host_file = host_file        # 日誌模組        logging.basicConfig(level=logging.INFO,                            format=‘%(asctime)s %(levelname)s %(message)s‘,                            datefmt=‘%Y%m%d %H:%M:%S‘,                            filename="./zabbix.log",                            filemode=‘a‘)        # 讀取zabbix 設定檔        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(msg)            raise msg        # 登入zabbix        try:            self.zapi = ZabbixAPI("http://192.168.4.135/zabbix")            self.zapi.login(self.zabbix_user,self.zabbix_passwd)            logging.info("connecting zabbix successful.")        except Exception as msg:            logging.error("connecting zabbix failed" + "detail:{}".format(msg))            raise msg    def load_zabbix(self):        # 讀取excel檔案        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 = ‘‘        # 讀取zabbix 資訊        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 = row_values[-1]            ip = row_values[-2]            group_id = "2"            create_host = {                "host": hostname,                "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):        for 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):        ‘‘‘        迴圈zabbix 上擷取到的主機,然後和本地excel檔案裡要刪除的主機進行對比,如果是的,那麼就把他刪掉。        刪除用hostid        :return:        ‘‘‘        for host in obj.zapi.host.get():            for del_host in self.load_zabbix():                if host["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"]))                    except Exception as msg:                        print(msg)                        logging.error("del host:{} failed,".format(host["host"]))if __name__ == "__main__":    action,file = sys.argv[1:]    assert os.path.exists(file),"file:‘{}‘ doesn‘t exists".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 err.")        raise Exception
3.2 開發一個Agent,採集系統資訊。

要求:

  1. 擷取cpu 記憶體 磁碟(磁碟使用資訊) 主機名稱 主機ip地址 等資訊
  2. 組裝成json格式上傳到redis裡。
  3. 要求記錄日誌
  4. 定時執行每10分鐘更新一次。
  5. 用多線程執行

Agent指令碼內容

import jsonimport platformimport scheduleimport psutilimport configparserimport redisimport loggingimport threadingimport socketimport timeimport mathdef log():    logging.basicConfig(level=logging.INFO,                        format=‘%(asctime)s %(levelname)s %(message)s‘,  # 日誌格式                        datefmt=‘%Y%m%d %H:%M:%S‘,  # 時間格式                        filename="./agent.log",  # 記錄檔的路徑                        filemode=‘a‘)  # 檔案的開啟模式    return logging.basicConfiglog()def get_sys():    try:        hostname = socket.gethostname()        ip = socket.gethostbyname(hostname)    except Exception as msg:        print(msg)        hostname = ""        ip = ""    data = {"hostname":hostname,"ip":ip}    return  datadef get_cpu():    cpu_count_logical = psutil.cpu_count(logical=False)    cpu_count_phycial = psutil.cpu_count(logical=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(小數) 取整數    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 get_disk():    disk_list = []    all_disk_part = psutil.disk_partitions()    for partition in all_disk_part:        mount_point = partition[1]        mount_total = math.ceil(psutil.disk_usage(mount_point).total/1024/1024/1024)        mount_percent = 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_data["host"] = get_sys()    sys_data["disk"] = get_disk()    sys_data["mem"] = get_mem()    sys_data["cpu"] = get_cpu()    return  json.dumps(sys_data) # 進行還原序列化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")            redis_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 successful....")def thread_job(func):    target =threading.Thread(target=func)    target.start()if __name__ == "__main__":    schedule.every(2).seconds.do(thread_job,put_redis)    while True:        schedule.run_pending()        time.sleep(1)    

python 物件導向&常用模組(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.