运维初学PythonDay09

多类继承


class B:
    def fun02(self):
        print("this is fun02")
    def fun04(self):
        print("this is fun04B")
class C(B,A):#就近原则
    def fun03(self):
        print("this is fun03")
if __name__ == '__main__':
    c=C()
    c.fun03()
    c.fun02()
    c.fun01()
    c.fun04()

__init__ 初始化构造方法,对象初始化

__str__ 反回值为str类型 直接打印对象显示的字符串数据

__call__ 将一个对象做为函数去调用执行的逻辑


class Book:
    def __init__(self,title,author):
        self.title = title
        self.author = author
    def __str__(self):
        return f"title:{self.title}"
    def __call__(self):
        print(f"title:{self.title}autho:{self.author}")
if __name__ == '__main__':
    b1=Book("123","456") #初始化init
    print(b1)#字符串 str
    b1()#函数名 call

正则表达式 线程 模块的安装

RE模块

match 函数

尝试用正则表达式模式从字符串的 开头匹配如果匹配成功,则返回一个匹配对象;否则返回 None

search 函数

在字符串中查找正则表达式模式的第一次出现,如果匹配成功,则返回一个匹配对象;否则返回 None。匹配即停止

findall 函数

在字符串中查找正则表达式模式的所有出现;返回一个匹配对象的列表

finditer函数

查找字符串中所有匹配字符【返回迭代器】

.group()返回匹配到东西


#正则表达式里面\d等级于[0-9] +至少匹配一次 {3}匹配次数  
import re
#re.match(正则字符串,目标字符串)
data1=re.match("\d{3}","123asd123123gh123ghf")
print(data1,data1.group())
data2=re.search("\d{3}","asd123gggt123gh123ghf")
print(data2,data2.group())
data3=re.findall("\d{3}","asd123gggt123gh123ghf")
print(data3)
data4=re.finditer("\d{3}","asd123gggt123gh123ghf")
print(data4)fdata5=re.split("3|g","123asd123123gh123ghf")
print(data5)
data5=re.sub("\d{3}","YYYY","asd123gggt123gh123ghf")
print(data5)
obj=re.compile("\d{3}")
data6=obj.search("asd123gggt123gh123ghf")
print(data6,data6.group())

案列

第二步:编写函数count_log()

# fname:日志文件路径 re_str:正则字符串

# 1.按行读取文件(while True+readline/readlines)

# 2.使用search方法对行数据进行正则匹配

# 3.如果数据匹配成功,使用group获取匹配的字符串内容

# 4.如果新一行的ip存在,则更新字典数据,访问次数+1

# 5.如果新一行的ip不存在, ip:1 添加到字典中


import re
def count_log(fname, re_str):
    mydict = {}  # 用于统计
    re_obj = re.compile(re_str)  # 正则字符串编译成正则对象
    with open(fname, mode="r") as fr:
        for item in fr.readlines():  # item: 每一行数据
            data = re_obj.search(item)
            if data != None:  # 数据匹配成功
                tmp = data.group()  # tmp:匹配字符串
                if tmp in mydict.keys():  # key存在
                    mydict[tmp] = mydict[tmp] + 1
                else:  # key不存在
                    mydict[tmp] = 1
    return mydict
if __name__ == '__main__':
    d1 = count_log("/opt/web.log", "(\d+\.){3}\d+")#ip
    print("ip:", d1)
    d2 = count_log("/opt/web.log", "Firefox|MSIE")
    print("browser:", d2)

进程是争夺CPU的最小单元线程是执行程序的最小单元

线程的运行离不开进程 进程的正常流转离不开线程

多线程编程

  • **thread 和 threading **模块允许程序员创建和管理线程

  • thread 模块提供了基本的线程和锁的支持,而 threading 提供了更高级别、功能更强的线程管理功能

  • 推荐使用更高级别的 threading 模块

使用

  • 多线程编程有多种方法,传递函数给threading模块的Thread类是介绍的第一种方法

  • Thread 对象使用 start() 方法开始线程的执行,直到线程结束


import time,threading
def banzhuan(t):
    print("start....")
    time.sleep(t)
    print("end....")
for i in range(2,6):
    #target:函数,当前线程执行的任务
    #args:元组 给多线程执行的任务传递实际参数
    t = threading.Thread(target=banzhuan,args=(i,))#多线程
    t.start()
    #banzhuan()#单线程

扫描网断存活的主机


import subprocess,threading
def ping(host):
    result=subprocess.run(
        f"ping -c2 -w0.1 {host} &> /dev/null",shell=True
    )
    if result.returncode:
        print(f"host:{host} is no")
    else:
        print(f"host:{host} is ok")
if __name__ == '__main__':
    ips = [f"172.40.63.{i}" for i in range(255)]
    for ip in ips:
        t=threading.Thread(target=ping,args=(ip,))
        t.start()

Python 模块安装

通过pip本地安装python模块

  • pipPython 包管理工具

  • 提供了对 Python 包的查找、下载、安装、卸载的功能

  • 下载模块后解压缩并安装

离线安装


上传 wget-3.2.zip 到 pycharm 主机

[root@localhost ~]# unzip wget-3.2.zip  # 解压
[root@localhost ~]# cd wget-3.2/  # 进入到wget解压目录下
[root@localhost wget-3.2]# python3 setup.py install  # 安装wget
[root@localhost wget-3.2]# pip3 list |grep -i wget  # 查看wget的版本

在线安装模块

使用国内镜像站点
[root@localhost xxx]# mkdir ~/.pip            #在root家目录下,创建隐藏目录.pip
[root@localhost xxx]# vim ~/.pip/pip.conf        #创建,配置国内镜像源,index-url 指定国内镜像源路径,trusted-host 信任该镜像网站,否则无法使用
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

pip常见指令以及模块安装方式

pip3 list  # 查看所有
pip3 list | grep -i 包名  # -i 忽略大小写,查看指定的包
pip3 list | grep -i requests
pip3 install 包名  # 安装指定模块
pip3 uninstall 包名  # 卸载

pip3 install pymysql==查看包版本要安装哪一个

这些你需要知道

1 如何用正则表达式匹配全部是数字的字符串?

'^\d+$' '^[0-9]+$'

2 re.match和re.search的区别是什么?

re.match只能从字符串的开头匹配

re.search可以在字符串的任意位置匹配 匹配即停止

3 通过什么方法可以将以下正则表达式匹配到的内容提取出来?

  1. m = re.search('f..','seafood')

m.group() .group()方法

4 如何通过正则表达式匹配字符串中所有符合条件的子串。

re.findall方法

re.finditer方法 字符串

5 re.compile作用是什么?

对正则表达式模式进行编译,返回一个正则表达式对象

在大量匹配的情况下,可以提升效率

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

small white poplar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值