python自动化测试-D7-学习笔记之三(网络编程)

'''网络编程:python操作网络,也就是打开一个网站,或者请求一个http接口,使用urllib模块。
urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib模块,在python2里面有urllib模块和urllib2模块。

使用python自带的urllib模块去请求一个网站,或者接口,但是urllib模块太麻烦了,传参数的话,都得是bytes类型,
返回数据也是bytes类型,还得解码,想直接把返回结果拿出来使用的话,还得用json,发get请求和post请求,也不通,
使用比较麻烦,还有一个比较方便的模块,比urllib模块方便很多,就是requests模块,它使用比较方便,需要安装,pip install requests即可
'''
# import requests
#
# req = requests.get('http://www.nnzhp.cn', data={'username': 'xxx'}, cookies={'k': 'v'},
# headers={'User-Agent': 'Chrome'}, verify=False, timeout=3) # 发送get请求,data是请求数据,
# # cookies是要发送的cookies,headers是请求头信息,verify=False是https请求的时候要加上,要不然会报错。
# # timeout参数是超时时间,超过几秒钟的话,就不再去请求它了,会返回timeout异常
# # 这些都可以不写,如果有的话,可以加上
# req2 = requests.post('http://www.nnzhp.cn', data={'username': 'xxx'}, cookies={'k': 'v'},
# headers={'User-Agent': 'Chrome'}, files={'file': open('a.txt')}, timeout=3) # 发送post请求,data是请求数据,
# # cookies是要发送的cookies,headers是请求头信息,files是发送的文件,verify=False是https请求的时候要加上,
# # 要不然会报错,timeout参数是超时时间,超过几秒钟的话,就不再去请求它了,会返回timeout异常
# # 这些都可以不写,如果有的话,可以加上
#
# req3 = requests.put('http://www.nnzhp.cn') # put方式请求
# req4 = requests.patch('http://www.nnzhp.cn') # patch方式请求
# req5 = requests.delete('http://www.nnzhp.cn') # delete方式请求
# req6 = requests.options('http://www.nnzhp.cn') # options方式请求,用法和上面的get、post都一样
#
# print(req.status_code) # 获取返回状态码
# print(req.content) # 获取返回的内容,二进制格式,一般下载图片、视频用这个
# print(req.text) # 获取返回的内容,字符串格式
# print(req.json()) # 获取返回的内容,json格式,这个必须是返回的是json才可以使用,否则会报错
# print(req.headers) # 获取响应头
# print(req.cookies) # 获取返回的cookie
# print(req.encoding) # 获取返回的字符集

# 实例:::
# import urllib.request # 这个模块是发送请求用的,但不好用
# import json
# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=test'
# res = urllib.request.urlopen(url) # 发送请求
# msg = res.read().decode() # 获取结果,返回的结果是bytes,用decode转换
# print(json.loads(msg))

# 发送get请求的
# import requests,json #requests 模块是发送请求用的
# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马'
# req = requests.get(url) #发送get请求
# print(req.text) # 获取结果 , 是str类型的 不方便取值
# # print(json.loads(req.text)) # 转换成字典,方便取值,这是传统写法
# print(req.json())# 获取结果直接转成字典

# 发送post请求的
# import requests,json
# url = 'http://api.nnzhp.cn/api/user/login'
# data = {
# 'username':'niuhanyang',
# 'passwd':'aA123456'
# }
# req = requests.post(url,data) # 请求参数第一个是url,第二个必须是字典格式
# print(req.json())

# 课堂练习:
# 1、牛刀小试 -- 注册接口
# import requests,json
# url = 'http://api.nnzhp.cn/api/user/user_reg'
# data = {
# 'username':'sherry',
# 'pwd':'Sherry000',
# 'cpwd':'Sherry000',
# }
# req = requests.post(url,data)
# print(req.json())

#2、入参是json类型的接口 牛刀小试 -- 添加学生信息 http://doc.nnzhp.cn/index.php?s=/6&page_id=10
# import requests,json
# url = 'http://api.nnzhp.cn/api/user/add_stu'
# data = {
# 'name':'张三丰',
# 'grade':'天蝎座',
# 'phone':'19910010012',
# 'sex':'男',
# 'age':18,
# 'addr':'元末武当山'
# }
# req = requests.post(url,json=data) # 请求参数第一个是url,第二个写成 json= data
# print(req.json())

# 3、添加cookie的接口开发 牛刀小试 --金币充值 http://doc.nnzhp.cn/index.php?s=/6&page_id=11
# 先登录,获取 sign = 4c7b4f7415a2ab5e7710097a2544204f
# cookie中key为登录的用户名,value从登录接口中获取,登陆成功之后会返回sign
# import requests,json
# url = 'http://api.nnzhp.cn/api/user/gold_add'
# data = {
# 'stu_id':214,
# 'gold':10000,
# }
# cookie = { # 定义的变量名称叫cookie
# 'sherry':'4c7b4f7415a2ab5e7710097a2544204f'
# }
# req = requests.post(url,data,cookies=cookie) # 请求参数第一个是url,第二个 data 第三个写 cookies = cookie
# print(req.json())

# 4、添加header的接口开发 牛刀小试 -- 获取所有学生信息 http://doc.nnzhp.cn/index.php?s=/6&page_id=14
# import requests,json
# url = 'http://api.nnzhp.cn/api/user/all_stu'
# head = {
# 'Referer':'http://api.nnzhp.cn/'
# }
# res = requests.get(url,headers=head)
# print(res.json())

# 5、上传文件的接口开发 牛刀小试 --文件上传,http://doc.nnzhp.cn/index.php?s=/6&page_id=13
# import requests,json
# url = 'http://api.nnzhp.cn/api/file/file_upload'
# f = open(r'C:UsersAdministratorPictureshaimianbaobao.jpg','rb') # 图片是二进制的,需要指定下二进制
# file = {
# 'file':f
# }
# req = requests.post(url,files=file)
# print(req.json())

# 6、下载
# import requests,json
# url='https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3772631548,3511332781&fm=200&gp=0.jpg'
# req = requests.get(url)
# # print(req.status_code) # 获取请求状态码
# # print(req.content) # 获取返回结果二进制格式的
# fw= open(r'C:UsersAdministratorPictures iger.jpg','wb') #指定目录,tiger.jpg是自己取的文件名字,图片是二进制的,所以文件格式要加b
# fw.write(req.content)
# fw.close()

# 6、下载一篇博客 保存网页
# import requests,json
# url = 'http://www.nnzhp.cn/archives/140'
# req =requests.get(url)
# fw = open('boke.html','wb')
# fw.write(req.content)
# fw.close()
原文地址:https://www.cnblogs.com/blackbird0423/p/8394762.html