requests库,os库,unittest库、time库、logging模块
# coding:utf-8
#requests库简单操作
import requests
r=requests.get('https://www.baidu.com')
print(r.url)#打印url
print(r.text)#打印网页标题
print(r.status_code)#打印请求网页返回状态
#os库简单操作
import os
print(os.getcwd())#获取当前python所在的目录
print(os.stat(os.getcwd()))#获取当前文件信息
os.system("whereis python")#执行shell操作
print(os.listdir("/Users/ph/PycharmProjects/penghui"))#获取当前目录下的文件
print(os.path.split(os.getcwd()))#文件夹和文件分开 元组形式
print(os.path.getatime(os.getcwd()))#文件修改时间
print(os.path.getsize('/Users/ph/Documents/个人文件'))#文件大小
print(os.path.exists('/Users/ph/Documents/个人文件'))#文件是否存在 返回true和false
print(os.path.abspath('.')#获取当前python文件路径
创建一个目录 os.path.join('目录路径',‘目录名’) os.mkdir('路径/目录名')
删除目录:os.rmdir('路径/目录名')
file=open('/Users/ph/Desktop/测试.txt','w')#创建一个txt文件,可写入
file.write('hello,world')#在文件中写入内容"hello,world"
#单元测试简单案例
import unittest
from test2 import unittest_1
''' unittest_1内容:
def add(a,b):
return a+b
def miuns(a,b):
return a-b '''
class TestMathFunc(unittest.TestCase):
def test_add(self):
self.assertEqual(3,unittest_1.add(1,2))
def test_minus(self):
self.assertEqual(0,unittest_1.miuns(4,4))
unittest.main()
class ceshi(unittest.TestCase):
def test_api(self):
url = "https://www.v2ex.com/api/nodes/show.json"
search={'name':'python'}
response=requests.request('GET',url=url,params=search).json()
self.assertEqual(response['name'],'python')
self.assertEqual(response['id'],90)
unittest.main()
a=ceshi()
a.test_api()
def setUp(self):#测试前的提前准备操作
self.dr=webdriver.Firefox(executable_path='/Users/ph/Documents/geckodriver')
self.dr.get('https:/www.baidu.com')
time.sleep(3)
def tearDown(self):#测试结束后的操作
self.dr.quit()
json 库:
json.dumps(data) #将python数据结构编码为json格式,data为具体要转化的数据
json.loads(data)#将json格式解码为python格式,data为具体要转化的数据
time 库 :
time.time() 获取的是1970年到现在的时间,单位为秒
time.localtime() 获取如下格式的时间 time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=11, tm_min=0, tm_sec=57, tm_wday=0, tm_yday=337, tm_isdst=0)
time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) 获取如下格式的时间:2018-6-21 11:00:57
random库:
random.randint(1,10) # 产生 1 到 10 的一个整数型随机数
random.random() # 产生 0 到 1 之间的随机浮点数
random.uniform(1.1,5.4) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
random.choice('tomorrow' # 从序列中随机选取一个元素
random.randrange(1,100,2) # 生成从1到100的间隔为2的随机整数
a=[1,3,5,6,7] # 将序列a中的元素顺序打乱
random.shuffle(a)
用qq服务器发送邮件
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
my_sender = 'xx@qq.com' #发件人邮箱
my_pass = 'xxx' # 发件人邮箱授权码
my_user = 'xxx@qq.com' # 收件人邮箱账号
message="邮件发送的内容"
def mail():
ret = True
try:
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = formataddr(["FromRunoob", my_sender]) # 发件人邮箱昵称、发件人邮箱账号
msg['To'] = formataddr(["FK", my_user]) # 收件人邮箱昵称、收件人邮箱账号
msg['Subject'] = "数据上报接口测试" # 邮件的标题
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器
server.login(my_sender, my_pass) # 发件人邮箱账号、邮箱密码
server.sendmail(my_sender, [my_user, ], msg.as_string()) # 发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception:
ret = False
return ret