python模块学习

1.psutil模块

psutil模块是一个资源监控模块
用法示例:
psutil模块需要先下载pip install psutil

import psutil
#内存
mem = psutil.virtual_memory()
print(mem)

#cpu
cpu = psutil.cpu_percent(1)
print(cpu)

#硬盘
disk = psutil.disk_usage(r'c:')
print(disk)

2.yagmail邮件模块

yagmail是一个发送邮件的模块
示例用法:
yagmail模块需要先下载pip install yagmail

import yagmail
def sendmail(subject,contents):  #subject形参是邮件的主题,contents是邮件的 内容
    #连接邮箱服务器,此处的密码是允许第三方登录的授权码
    yag = yagmail.SMTP(user='xxxxx@163.com',password='xxxxxx',host='smtp.163.com')
    #发送邮件
    yag.send(to='xxxxx@163.com',cc='xxxxx@163.com',subject=subject, contents=contents)
    #断开连接
    yag.close()

3.OS模块

os模块是python自带模块不需要下载就可用
os模块常用语句:

1.os.system利用python帮我们调用系统命令
import os
cmds = ['systemctl restart httpd','uname -r ','update','ip a ']
for cmd in cmds:
    res = os.system(cmd)
    if res == 0:
        print('执行成功')
    else:
        print('执行失败')

res返回0就等于命令执行成功,如果不为0执行结果失败

2.path.exists判断是否存在这个文件或者目录
res = os.path.exists(r'a.txt')
if res :
    print('文件已存在')
else:
    os.system('dir')
3.remove移除文件或目录
os.remove('a.txt')
4.rename重命名
os.rename(‘原文件名’,‘新文件名’)
5.path.join 拼接路径
CPATH = '/etc/yum.repo.d/'
res = os.path.join(CPATH,'a.txt')
print(res)

4.time模块

time模块不用下载直接可用
用法示例:

import time
可用在需要时间间隔来输出或执行某些动作的时候
time.sleep(0.1)  ——>括号里可填以秒为单位的数,表示间隔0.1

5.configparser模块

configparser模块是用来读取配置文件的包。
配置文件的格式如下:
中括号“[]”内包含的为section
section下面为类似于key-value的配置内容

用法示例:
configparser 是python内置模块可以直接调用

import configparser
config = configparser.ConfigParser()
config.read("F:\demo\my_yum.repo", encoding="utf-8")
1.添加section
config.add_section('mysql')#[mysql]叫section 
config.write(open("F:\demo\my_yum.repo", "w"))#保存
2.section里添加(set)option和value
config.set('mysql','baseurl','http://www.mysql.com')#往mysql这个section里添加(set)option和value
config.write(open("F:\demo\my_yum.repo", "w"))#保存
3.删除option
config.remove_option('mysql','baseurl')
config.write(open("F:\demo\my_yum.repo", "w"))#保存
4.查看是否有baseurl这个option
res = config.has_option('mysql','baseurl')
if res:
    print('已存在')
else:
    config.set('mysql','baseurl','http://www.mysql.com')
config.write(open("F:\demo\my_yum.repo", "w"))#保存  
5.删除section,如果有option一并删除
config.remove_section('mysql')
config.write(open("F:\demo\my_yum.repo", "w"))#保存
6.get通过section和option取value
res = config.get('mysql','baseurl')
print(res)
输出:
http://www.mysql.com

6.paramiko模块

paramiko模块可以模拟ssh连接linux主机
用法示例:
paramiko模块是第三方软件需要用pip安装
pip install paramiko

1.模拟ssh连接linux主机,执行命令
import paramiko
#模拟ssh连接linux主机
ssh = paramiko.SSHClient()   #ssh为自定义变量名
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.30.147',port=22,username='root',password='root')
while True:
    stdin,stdout,stderr = ssh.exec_command(input('==>:').strip())
    res = stdout.read().decode('utf-8')+stderr.read().decode('utf-8')
    print(res)

我们知道pip安装是可以解决软件依赖关系,而安装paramiko模块会安装其他一些依赖的软件,其中有个叫cryptography的软件依赖下载默认会安装最新版本2.6.1,这时你执行上述代码会报一些警告,但代码可以正常运行。

F:\demo\venv\lib\site-packages\paramiko\kex_ecdh_nist.py:39: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
  m.add_string(self.Q_C.public_numbers().encode_point())
F:\demo\venv\lib\site-packages\paramiko\kex_ecdh_nist.py:96: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
  self.curve, Q_S_bytes
F:\demo\venv\lib\site-packages\paramiko\kex_ecdh_nist.py:111: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
  hm.add_string(self.Q_C.public_numbers().encode_point())

产生这些警告是因为最新版本的包其中的一些旧版本的功能使用方法有改变或直接被移除了,所以要解决这些问题就要先将最新版本的包卸载然后下载指定版的包:
解决方法:

1.可以在命令提示符界面先进入你用到的虚拟环境里卸载,没有虚拟环境的直接卸载
pip uninstall Cryptography

然后再指定版本包下载

pip install Cryptaphyogr==2.4.2

如果下载出现超时可以用国内的pipy源下载

pip install Cryptography==2.4.2  -i  https://mirrors.aliyun.com/pypi/simple

国内pypi源:
阿里:https://mirrors.aliyun.com/pypi/simple
豆瓣:http://pypi.douban.com/simple
中国科学技术大学:http://pypi.mirrors.ustc.edu.cn/simple/

2.在pycharm里也可以解决

在顶头菜单里找到File——>Settings——>找到右栏中Cryptography软件
在这里插入图片描述
按照上图第4步进行卸载
在这里插入图片描述
在这里插入图片描述
如果下载出现超时可以添加国内的pipy源地址
在这里插入图片描述
在这里插入图片描述
然后重新搜索进行下载安装,再执行代码警告消除。

执行结果:
==>:ls -ltr
总用量 143380
-rw-r--r--   1 root  root  33203321 5月   6 2015 mysql-5.6.25.tar.gz
-rw-r--r--   1 root  root  49025014 6月   8 2018 mysql-boost-5.7.23.tar.gz
-rw-r--r--   1 root  root  19480712 3月   7 17:10 php-7.1.27.tar.gz
-rw-------.  1 root  root      1257 3月  27 06:34 anaconda-ks.cfg
-rw-r--r--   1 root  root    874044 3月  28 10:26 apr-util-1.5.4.tar.gz
-rw-r--r--   1 root  root   1031613 3月  28 10:26 apr-1.5.2.tar.gz
-rw-r--r--   1 root  root   4657514 3月  28 11:29 wordpress-3.3.1-zh_CN.zip
-rw-r--r--   1 root  root   8299011 3月  28 11:30 httpd-2.4.20.tar.gz
-rw-r--r--   1 root  root  19421313 3月  29 15:37 php-7.3.3.tar.gz
-rw-r--r--   1 root  root  10794370 3月  30 21:01 phpMyAdmin-4.8.5-all-languages.zip
drwxr-xr-x  12   501 games     4096 3月  31 13:34 httpd-2.4.20
drwxr-xr-x  20 mysql mysql     4096 3月  31 19:43 php-7.1.27

==>:pwd
/root

==>:cd /tmp

==>:pwd
/root

执行命令有一个缺陷,就是不能切换目录,那么想要在其他目录执行命令就得写绝对路径

2.通过paramiko模块连接主机上传
hostname = '192.168.30.147'
port = 22
username = 'root'
password = 'root'
t = paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(r'C:\Users\zhilo\Desktop\a.txt', '/root/aa.txt')
sftp.close()
3.通过paramiko模块连接主机下载
hostname = '192.168.30.147'
port = 22
username = 'root'
password = 'root'
t=paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get('/root/aa.txt', r'C:\Users\zhilo\Desktop\aaa.txt')
sftp.close()

7.socket模块

用于监控端口是否正常
示例用法

import socket,re   
#.*?叫做非贪婪匹配,尽可能的少匹配
#.*叫做贪婪匹配,尽可能的多匹配
# a = 'fenif1212nfi129f21f'
# res = re.compile('(\d+)').findall(a)
# print(res)
hosts = ['1.1.1.1:90','2.2.2.2:8080','127.0.0.1:443','3.3.3.3:50','192.168.1.1:9090']
for host in hosts:
    ip = re.compile('(.*?):(.*)').search(host).group(1)
    port = re.compile('(.*?):(.*)').search(host).group(2)
    server = socket.socket()#tcp协议
    server.settimeout(1)#设置超时时间
    res = server.connect_ex((ip,int(port)))
    # print(res)#res == 0代表端口号启用|res != 0代表端口号没启用
    if res == 0:
        print('%s---%s:ok' % (ip,port))
    else:
        print('%s---%s:不通' % (ip, port))

8.re模块

re模块的用法非常强大
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值