一、sys模块(内置模块)

用于提供对解释器相关的操作

import sys
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]

sys模块更多用法:https://docs.python.org/2/library/sys.html?highlight=sys#module-sys

二、Greenlet模块

IO操作,即对硬盘上的数据进行读写操作。

greenlet只是提供了一种比generator更加便捷的切换方式,当切到一个任务执行时如果遇到IO,那就原地阻塞,仍然是没有解决遇到IO自动切换来提升效率的问题。

单线程里的这20个任务的代码通常会既有计算操作又有阻塞操作,我们完全可以在执行任务1时遇到阻塞,就利用阻塞的时间去执行任务2......如此,才能提高效率,这就用到了Gevent模块

from greenlet import greenlet  #greenlet协程模块
# 定义吃
def eat():
print('eat-start')
g2.switch() #
print('eat-end')
# 定义玩
def play():
print('play happy')
g1.switch() # 切回对应的方法接着执行
g1 = greenlet(eat)
g2 = greenlet(play)
g1.switch()#可以在第一次switch时传入参数,以后都不需要;switch是切换的意思
输出:
eat-start
play happy
eat-end

三、paramiko模块

#通过paramiko模块连接主机运行bash命令

import paramiko
hostname = '192.168.254.24'
port = 22
username = 'root'
password = 'root'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname,port=port,username=username,password=password)
stdin, stdout, stderr = ssh.exec_command("ls -ltr")
print(stdout.read().decode('utf-8')) #通过paramiko模块连接主机上传
import paramiko
hostname = '192.168.254.24'
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\fengzi\Desktop\Linux.xmind', '/root/aaa.xmind')
sftp.close() #通过paramiko模块连接主机下载
import paramiko
hostname = '192.168.254.24'
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/test3.yml', r'C:\Users\fengzi\Desktop\test3.yml')
sftp.close()

四、pexpect模块

import pexpect
host = '192.168.254.24'
password = 'root'
username = 'root'
child = pexpect.spawn('ssh root@192.168.254.24')
child.expect('password:')
child.sendline(password)
child.expect('#')
child.sendline('mysql -uroot -proot')
child.expect('none')
child.sendline('show variables like "%log%";')
child.sendline('exit')
child.sendline('exit')
child.interact()
child.close() #利用pexpect查看其他机器的数据库
import pexpect
ssh = pexpect.spawn('ssh 192.168.254.12',timeout=10)
i = ssh.expect(['password:','continue connecting'],timeout=10)
print(i)
if i == 0:
ssh.sendline('root')
elif i == 1:
ssh.sendline('yes\n')
ssh.expect('password:')
ssh.sendline('root\n')
index = ssh.expect(['#',pexpect.EOF,pexpect.TIMEOUT],timeout=15)
if index == 0:
ssh.sendline('ip a')
ssh.sendline('exit')
ssh.interact()
ssh.close()
elif index == 1:
print('logging process exit')
elif index == 2:
print('logging in time out') #ssh登录主机

五、pymysql模块

#pymysql操作数据库
import pymysql
# 打开数据库连接
db = pymysql.connect(host="192.168.254.24", user="root",
password="root", db="mysql", port=3306) # 使用cursor()方法获取操作游标
cur = db.cursor() # 1.查询操作
# 编写sql 查询语句 user 对应我的表名
sql = "select host,user,password from user"
try:
cur.execute(sql) # 执行sql语句
results = cur.fetchall() # 获取查询的所有记录
for i in results:#遍历结果
print(i)
except Exception as e:
raise e
finally:
db.close() # 关闭连接

六、configparser模块

1、CconfigParser简介

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

[db]
db_host = 127.0.0.1
db_port = 69
db_user = root
db_pass = root
host_port = 69 [concurrent]
thread = 10
processor = 20

括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。

2、ConfigParser 初始化对象
使用ConfigParser 首先需要初始化实例,并读取配置文件:

import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")

3、ConfigParser 常用方法

1、获取所用的section节点
# 获取所用的section节点
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
print(config.sections())
#运行结果
# ['db', 'concurrent'] 2、获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
r = config.options("db")
print(r)
#运行结果
# ['db_host', 'db_port', 'db_user', 'db_pass', 'host_port'] 3、获取指点section下指点option的值
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
r = config.get("db", "db_host")
# r1 = config.getint("db", "k1") #将获取到值转换为int型
# r2 = config.getboolean("db", "k2" ) #将获取到值转换为bool型
# r3 = config.getfloat("db", "k3" ) #将获取到值转换为浮点型
print(r)
#运行结果
# 127.0.0.1 4、获取指点section的所用配置信息
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
r = config.items("db")
print(r)
#运行结果
#[('db_host', '127.0.0.1'), ('db_port', '69'), ('db_user', 'root'), ('db_pass', 'root'), ('host_port', '69')] 5、修改某个option的值,如果不存在则会出创建
# 修改某个option的值,如果不存在该option 则会创建
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
config.set("db", "db_port", "69") #修改db_port的值为69
config.write(open("info.txt", "w"))
#运行结果

6、检查section或option是否存在,bool值
import configparser
config = configparser.ConfigParser()
config.has_section("section") #是否存在该section
config.has_option("section", "option") #是否存在该option

7、添加section 和 option
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
if not config.has_section("default"): # 检查是否存在section
config.add_section("default")
if not config.has_option("default", "db_host"): # 检查是否存在该option
config.set("default", "db_host", "1.1.1.1")
config.write(open("info.txt", "w"))
#运行结果

8、删除section 和 option
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
config.remove_section("default") #整个section下的所有内容都将删除
config.write(open("info.txt", "w"))
#运行结果

9、写入文件
以下的几行代码只是将文件内容读取到内存中,进过一系列操作之后必须写回文件,才能生效。
import configparser
config = configparser.ConfigParser()
config.read("info.txt", encoding="utf-8")
写回文件的方式如下:(使用configparser的write方法)
config.write(open("info.txt", "w"))

python基础之常用模块一(sys、greenlet、pymysql、paramiko、pexpect、configparser)的更多相关文章

  1. 十八. Python基础(18)常用模块

    十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...

  2. python基础31[常用模块介绍]

    python基础31[常用模块介绍]   python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...

  3. Python全栈开发之路 【第六篇】:Python基础之常用模块

    本节内容 模块分类: 好处: 标准库: help("modules") 查看所有python自带模块列表 第三方开源模块: 自定义模块: 模块调用: import module f ...

  4. Python基础之--常用模块

    Python 模块 为了实现对程序特定功能的调用和存储,人们将代码封装起来,可以供其他程序调用,可以称之为模块. 如:os 是系统相关的模块:file是文件操作相关的模块:sys是访问python解释 ...

  5. python基础之常用模块以及格式化输出

    模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  6. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  7. Python基础5 常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  8. Python基础之常用模块(二)

    一.sys模块 1.sys.exit() 退出程序,这是正常退出程序,与之前用的break不同的是,break只是退出循环,循环之后的代码还会正常运行 2.sys.argv  会返回一个列表,列表中的 ...

  9. Python基础之常用模块

    一.time模块 1.时间表达形式: 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: 1.1.时间戳(timestamp) :通常来说,时间 ...

随机推荐

  1. 000 - 准备工作ADB wifi连接多台鸿蒙设备进行调试

    首先将两台鸿蒙设备插入电脑的usb上 查看两台鸿蒙设备的deviceid C:\Users\Administrator>adb devices * daemon not running; sta ...

  2. Java网络编程快速上手(SE基础)

    参考资料:百度百科TCP协议 本文涉及Java IO流.异常的知识,可参考我的另外的博客 一文简述Java IO 一文简述JAVA内部类和异常 1.概述 计算机网络相关知识: OSI七层模型 一个报文 ...

  3. 数据库MySQL二

    注意拼接的时候如果为null则都为null 用if null 1.条件查询 2.按逻辑表达式筛选 3.模糊查询 还有not like 用转义字符\ #2.in 数值型的常量值都不用单引号,非数值型的都 ...

  4. 页面元素定位 - XPath

    1. XPath 简介 2. 选取节点 2.1 选取节点表达式 2.2 XPath 运算符 2.3 XPath 常用函数 2.4 亲属关系匹配 2.5 *综合示例 1. XPath 简介 什么是 XP ...

  5. 1091 Acute Stroke

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  6. ECMAScript 2019(ES10)新特性简介

    简介 ES10是ECMA协会在2019年6月发行的一个版本,因为是ECMAScript的第十个版本,所以也称为ES10. 今天我们讲解一下ES10的新特性. ES10引入了2大特性和4个小的特性,我们 ...

  7. 06- web兼容性测试与web兼容性测试工具

    web兼容性概述 定义:软件兼容性测试是指检查软件之间能否正确地进行交互和共享信息.随着用户对来自各种类型软件之间共享数据能力和充分利用空间同时执行多个程序能力的要求,测试软件之间能否协作变得越来越重 ...

  8. 十步解决php utf-8编码

    以前说过如果JS文件不是UTF8会在IE有bug,所以JS代码也要用UTF-8.还有数据库也都要用UTF-8.php用UTF-8总结: php文件本身必须是UTF-8编码.不像Java会生成class ...

  9. PowerDesigner16安装和使用

    安装 安装参考链接:PowerDesigner安装教程 因为这个博主已经操作的很详细了,这边就不做过多的赘述. 使用 新建模型 选择物理模型 调出面板Palette 建表 最终的效果(一般不在数据库层 ...

  10. Linux执行命令报错:Permission denied

    原因:权限被拒 结局办法 chmod -R 777 目录名 更改目录内文件的权限即可