python模块
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python模块,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3220字,纯文字阅读大概需要5分钟。
内容图文
![python模块](/upload/InfoBanner/zyjiaocheng/831/755643d423e0404d881e4ba9c89b06d2.jpg)
psutil资源监控模块
py文件名字不要跟导入的模块重名
py文件是可以当作模块导入的
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)
os命令调用模块
os.system 利用python 帮我们调用系统命令
res返回 0 就等于命令执行成功,如果不为 0 执行结果失败
import os # os.system利用python帮我们调用系统命令 # res返回0就等于命令执行成功,如果不为0执行结果失败 cmds = ['service httpd restart','uname -r','update','ifconfig -a'] for cmd in cmds: res = os.system(cmd) if res == 0: print('执行成功') else: print('执行失败') # path.exists判断是否存在这个文件或者目录 res = os.path.exists(r'a.txt') if res: print('文件已存在') else: os.system('dir') # remove移除文件或目录 os.remove('a.txt') # rename重命名 os.rename('lock.txt', '250.txt') # path.join,拼接路径 HOME = '/etc/yum.repo.d/' res = os.path.join(HOME,'a.txt') print(res)
yagmail 电子邮件发送模块
python 标准库中发送电子邮件的模块比较复杂,因此,有许多开源的库提供了更加易用的接口来发送电子邮件,其中yagmail 是一个使用比较广泛的开源项目,yagmail 底层依然使用了smtplib 和 email 模块,但是 yagmail 提供了更好的接口,并具有更好的易读性
yagmail 是开源项目,因此,在使用前需要安装
pip install yagmail
#连接邮箱服务器 yag = yagmail.SMTP(user='xxx@163.com', password='xxxx', host='smtp.163.com') #发送邮件 yag.send(to='xxx@126.com', cc='xxx@163.com',subject='这是测试邮件', contents='这是测试邮件的内容') #断开连接 yag.close()
ConfigParser模块
一、ConfigParser 简介
ConffigParser 使用来读取配置文件的包。配置文件的格式如下:
中括号"[]" 内包含为section 。 section 下面为类似key—value 的配置内容
![python模块 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506125924342.jpg)
![python模块 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506125924494.jpg)
import configparser config = configparser.ConfigParser() config.read("mariadb.repo",encoding= 'gbk') # 添加section config.add_section('mysql') config.set('mysql','baseurl','http://www.mysql.com') config.remove_option('mysql','baseurl')#删除option ''' 查看是否有baseurl这个option res = config.has_option('mysql','baseurl') if res: print('已存在') else: config.set('mysql','baseurl','http://www.mysql.com') ''' config.remove_section('mysql')#删除section,如果有option一并删除 res = config.values() for i in res: print(i) ''' res = config.get('server','baseurl')#get通过section和option取value print(res) ''' config.write(open("mariadb.repo", "w"))#保存(必加)View Code
paramiko 模块
模拟ssh 连接 linux 主机
import paramiko #模拟ssh链接linux主机 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='192.168.1.144',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)
上传文件到 linux
![python模块 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506125924342.jpg)
![python模块 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506125924494.jpg)
import paramiko # 通过paramiko 模块连接主机上传 hostname = '192.168.1.144' 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'D:\11\untitled1\requestment.txt','/root/requestment.txt') sftp.close()View Code
从linux 下载文件
![python模块 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506125924342.jpg)
![python模块 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506125924494.jpg)
import paramiko # 通过paramiko 模块连接主机上传 hostname = '192.168.1.144' 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/txt22',r'D:\11\untitled1\txt22.txt') sftp.close()View Code
内容总结
以上是互联网集市为您收集整理的python模块全部内容,希望文章能够帮你解决python模块所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。