释放双眼,带上耳机,听听看~!
1.获取外网ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1#!/usr/bin/env python
2-*- coding:utf-8 -*-
3Time: 2019/12/20 10:05
4import socket
5import requests,re
6#方法一
7text=requests.get("http://txt.go.sohu.com/ip/soip").text
8ip=re.findall(r'\d+.\d+.\d+.\d+',text)
9#方法二
10ipqwb = socket.getaddrinfo('www.baidu.com', 'http') #获取指定域名的A记录
11nowIp = (ipqwb[0][4][0]) # 赋值
12print("本机外网IP: " + ip[0])
13print("qwb IP: " + nowIp)
14
2.生成随机密码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3# Time: 2019/11/21 11:43
4import random,string
5def passwd():
6 src = string.ascii_letters + string.digits
7 count = input('请确认要生成几条密码: ')
8 list_passwds = []
9 for i in range(int(count)):
10 #密码位数为N+3,例如下面就是5+3=8位密码
11 list_passwd_all = random.sample(src, 5) #从字母和数字中随机取5位
12 list_passwd_all.extend(random.sample(string.digits, 1)) #让密码中一定包含数字
13 list_passwd_all.extend(random.sample(string.ascii_lowercase, 1)) #让密码中一定包含小写字母
14 list_passwd_all.extend(random.sample(string.ascii_uppercase, 1)) #让密码中一定包含大写字母
15 random.shuffle(list_passwd_all) #打乱列表顺序
16 str_passwd = ''.join(list_passwd_all) #将列表转化为字符串
17 if str_passwd not in list_passwds: #判断是否生成重复密码
18 list_passwds.append(str_passwd)
19 print(list_passwds[i])
20 #print(list_passwds)
21passwd()
22
3.发送邮件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3# Time: 2019/11/15 17:18
4import smtplib
5from email.mime.text import MIMEText
6from time import sleep
7from email.header import Header
8host = 'smtp.163.com'
9port = 25
10sender = 'xxxx@163.com'
11pwd = 'xxxxx'
12receiver = ['22222222@qq.com', 'xxxxxxxx@163.com'] # 可以不用添加自己的邮箱,添加为了防止系统认为是垃圾邮箱发送失败会报错
13body = '邮件内容'
14title = "邮件标题"
15def sentemail():
16 msg = MIMEText(body, 'plain', 'utf-8')
17 msg['subject'] = Header(title, 'utf-8').encode()
18 msg['from'] = sender
19 msg['to'] = ','.join(receiver)
20 try:
21 s = smtplib.SMTP(host, port)
22 s.login(sender, pwd)
23 s.sendmail(sender, receiver, msg.as_string())
24 print ('Done.sent email success')
25 except smtplib.SMTPException as e:
26 print ('Error.sent email fail')
27 print (e)
28if __name__ == '__main__':
29 sentemail()
30
4.基础log日志配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3# Time: 2019/11/27 13:04
4import logging
5def logger():
6 logger=logging.getLogger()
7
8 fh=logging.FileHandler("test.log") #向文件中发送内容,有自己默认的日志格式
9 ch=logging.StreamHandler() #向屏幕发送文件,有自己默认的日志格式
10
11 fm=logging.Formatter("%(asctime)s %(message)s") #定义自己的日志格式
12 fh.setFormatter(fm) #添加自定义的日志格式,如果不添加会用自己默认的日志格式
13 ch.setFormatter(fm)
14
15 logger.addHandler(fh) #显示出fh,ch的日志
16 logger.addHandler(ch)
17 logger.setLevel("DEBUG") #定义日志级别
18 return logger # 返回函数对象
19
20logger=logger() #调用函数
21
22logger.debug("hello 1") #打印日志
23logger.info("hello 2")
24logger.warning("hello 3")
25logger.error("hello 4")
26logger.critical("hello 5")
27
5.查看本地端口是否开放:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3# Time: 2019/11/21 11:05
4import socket
5
6port_number = [135,443,80,3306,22]
7
8for index in port_number:
9 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10 result = sock.connect_ex(('127.0.0.1', index))
11 if result == 0:
12 print("Port %d is open" % index)
13 else:
14 print("Port %d is not open" % index)
15 sock.close()
16