官网:
https://pypi.org/
https://pypi.org/project/requests/
wind模块下载与导入:
如果我们在下载好模块后,打开Pycharm,import导入相应的模块后。在执行的时候报没有模块的报错:我们可以在这里面双击pip模块,然后刷新内部模块,然后再导入所有已下载的模块即可。
邮件模块(smtplib)
设置邮件发送的前提:在QQ邮箱或者163邮箱开启SMTP,否则没用。
案例:编写发送邮件件程序,发件人为root,收件人是本机的bob和alice帐户
- [root@ localhost day12]# vim mail_client.py
- #!/usr/bin/env python3
- import smtplib
- from getpass import getpass
- from email.mime.text import MIMEText
- from email.header import Header
- mail_host = 'ahao.126.com' #发件人邮箱账号
- mail_user = 'open_ahaocloud@163.com' #收件人邮箱账号
- mail_pwd = getpass() #获取密码
- #邮件正文有三个参数:第一个为文本内容,第二个设置文本格式plain,第三个utf-8设置编码保证多语言兼容性
- message = MIMEText('Python邮件发送测试 ', 'plain', 'utf8')
- #发送者信息
- message['From'] = Header('zhangzhigang79@126.com', 'utf8')
- #接收者信息
- message['To'] = Header('open_ahaocloud@163.com', 'utf8')
- #主题信息
- message['Subject'] = Header('python 1802 mail test', 'utf8')
- sender = 'open_ahaocloud@163.com' #发送方
- receivers = ['open_ahaocloud@163.com'] #接收方
- smtp_obj = smtplib.SMTP() #创建SMTP对象
- smtp_obj.connect(mail_host) #将SMTP对象与发送人邮件简历连接建立连接
- smtp_obj.login(mail_user, mail_pwd) #登录用户名密码
- # SMTP 对象使用 sendmail 方法发送邮件
- smtp_obj.sendmail(sender, receivers, message.as_string())
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。
Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
Python SMTP 对象使用 sendmail 方法发送邮件:
smtp_obj.sendmail(sender, receivers, message.as_string())
requests模块
案例:利用json和requests与sys模块向钉钉机器人发起请求。
[root@ localhost day12]# vim dingtalk.py import json import requests import sys def send_msg(url, reminders, msg): headers = {'Content-Type': 'application/json;charset=utf-8'} data = { "msgtype": "text", # 发送消息类型为文本 "at": { "atMobiles": reminders, "isAtAll": False, # 不@所有人 }, "text": { "content": msg, # 消息正文 } } r = requests.post(url, data=json.dumps(data), headers=headers) return r.text if __name__ == '__main__': msg = sys.argv[1] reminders = ['15055667788'] # 特殊提醒要查看的人,就是@某人一下 url = 此处填写上面webhook的内容 print(send_msg(url, reminders, msg))
案例三:
>>> url = 'http://jisutqybmf.market.alicloudapi.com/weather/query' #得到url >>> headers = {'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'APPCODE 你管理后台中查询到的appcode'} //修改编码类型 >>> params = {'citycode': '101010100'} //取出想要地区的地方编码 >>> r = requests.get(url, headers=headers, params=params) >>> data = r.json() //取出数据转化为json格式 >>> import pprint //导入pprint模块 >>> pprint.pprint(data) # 取出后天最高、最低温度 >>> pprint.pprint(data['result']['daily'][2]['day']['temphigh']) //打印时间 '8' >>> pprint.pprint(data['result']['daily'][2]['night']['templow'])
最后案例:
作为参考得出思路即可:
1.定义一个字典,该字典中键‘0’和‘1’对应的值为天气网址中城市对应的id,城市不同id则不同
2.运行程序时,屏幕将出现你所在城市名字
3.当用户指定查询某城市(即输入0或1时)
4.调用get_weather函数,函数的实际参数为city_codes字典对应值(即对应的城市id)
5. 打开天气网页,使用urllib模块的urlopen函数打开url,赋值给html
6.读html获取的数据,用json.loads()获取天气信息,获取到的信息为字典形式
7.从获取到的字典数据中提取气温、湿度、风向、风速等信息,保存在output变量中
8.将output变量作为get_weather函数的返回值,打印在屏幕上
#!/usr/bin/python from urllib.request import urlopen import json def get_weather(city_code): #定义一个输入城市id的函数 5.打开天气网页,使用urllib模块的urlopen函数打开url,赋值给html url = 'http://www.weather.com.cn/data/sk/%s.html' % city_code html = urlopen(url) 6.读html获取的数据,用json.loads()获取我们想要的信息 #json.loads()是将json格式数据转换为字典 #(可以理解为json.loads()函数是将字符串转化为字典) data = json.loads(html.read()) 7.output为返回值,即最终屏幕显示的信息 output = '风向:%s, 风力: %s, 温度:%s, 湿度:%s' % ( #data获取到的天气信息为字典,该字典中weatherinfo键对应的值还是一个字典,这个字典中‘WD’键对应的值是风向,‘WS’键对应的值是风力,'temp'键对应的值是温度,'SD'键对应的值是湿度,利用键值对关系将相应数据显示出来即可 data['weatherinfo']['WD'], data['weatherinfo']['WS'], data['weatherinfo']['temp'], data['weatherinfo']['SD'] ) return output if __name__ == '__main__': 1.定义字典:键对应的值为天气网站网址接口中城市ID city_codes = { '0': '101010100', '1': '101121404'} 2.代码执行后,屏幕给出的提示信息 prompt = """(0) 北京 (1) 台儿庄 请选择(0/1): """ 3.根据提示信息,输入0或1 choice = input(prompt) 4.调用get_weather函数,其实际参数为city_codes字典对应值 8.打印调用get_weather函数返回值 print(get_weather(city_codes[choice]))
结论:我们可以通过邮件(smtplib)模块群体发送内容相同的信息,节省时间。
我最喜欢的还是最为报警机制,通过shell脚本检测linux系统中的各项参数,达到或者超过预设定的值后触发相应的邮箱脚本。(跟zabbix一样)
requests模块可以用来测试接口API,可以利用多线程(thread和threading)结合requests模块对测压的服务器进行压力测试,和各种检测
API接口调用:可以利用现有的模块向需要的钉钉机器人,或者QQ内部的小冰进行相应的控制,可以正对某个人或者群体发送相应的消息。