首页 > 编程学习 > 16.1json模块 16.2文件上传 16.3session的高级用法

16.1 json模块

# json主要是干嘛的?json非常严格的数据类型,只能用“”,不然会报错,只支持""
# 把一个东西变成序列
# [1,2,3,4,5] #有序的叫序列
# {"a",'b'} #散列

# import json
#
# dic={
# "operate":"login",
# "username":"admin",
# "password":"123456"
# }
# print(dic,type(dic))
# json_dict=json.dumps(dic)
# print(json_dict,type(json_dict))
# 结果:
# {'operate': 'login', 'username': 'admin', 'password': '123456'} <class 'dict'>
# {"operate": "login", "username": "admin", "password": "123456"} <class 'str'>

# bytes_json=json_dict.encode('utf-8')#转为字节
# print(bytes_json)
# 结果:
# b'{"operate": "login", "username": "admin", "password": "123456"}结果带b,表明是字节

# 网络传输不认识字典
# 文件操作也不认识字典
#
# json 不认识集合
# json 不认识元祖,但是不会报错,会把元祖转为列表



# dic2={"南京":{"江宁","玄武","六合"}}
# #写到文件中去
# with open('city.txt',mode='w',encoding='utf-8')as f:
# f.write(dic2)
# 结果:字典写不进去

# 网络传输不认识字典,文件操作也不认识字典,
# 如何改变:做序列化,转为json的字符串 value不让套字典转为字符串,因为json不认识集合,集合其他语言中没有
# json不认识元祖,但没报错,因为会把元祖自动转为列表

dict2={"南京":{"江宁":"九龙湖","玄武":"玄武湖"}}
dict2_json=json.dumps(dic2,ensure_ascii=False)#ensure_ascii=False 解决乱码的问题
print(dict2_json)
with open('city.txt',mode='w',encoding='utf-8')as f:
f.write(dict2_json)#写进来字节


# 读没有问题

# with open('city.txt',encoding='utf-8')as f1:
# str_d=f1.read()
# print(str_d)
# dic_s=json.loads(str_d)
# # 能序列化也可以反序列化,反序列化原本的类型,反序列化没问题
# print(dic_s)



# 总结:
# json的key必须是字符串,value只能是列表,字符串,数字,布尔值,字典,不能是元祖或结合,
# 因为是python的数据类型,是python和Java做交互,两个互发作交换,通过json做交互,
# json是一个格式要求的字符串,只能是列表,字符串,数字,布尔值,
# 作用:序列化和反序列化
#

16.2 文件上传
import requests
data={"file":open('a.png','wb')} # open('a.png')(绝对路径) 二进制读,请求体,读完之后,data拿到二进制字典
print(data)
response=requests.post('http://www.httpbin.org/post',files=data)#files传参
print(response.status_code)
print(response.json())


复杂请求

16.3 session的高级用法
# session如何用:
# 作用:用来帮我们保持回话
# session携带cookies

import requests
session=requests.Session()

session.get('http://www.httpbin.org/cookies/set/username/root')
response=session.get('http://www.httpbin.org/cookies'
print(response.json()) # {'cookies':{'username':'root'}}
requests.Session() 可以用来帮我们保持会话


import unittest
import requests
class Cms(unittest.TestCase):
_session=requests.Session()


@classmethod
def setUpClass(cls):

data = {
'userAccount': 'admin',
'loginPwd': '123456'
}
url1 = 'http://124.220.179.221:8081/cms/manage/loginJump.do'
res = cls._session.request(method='post', url=url1, data=data)
# print(res.status_code)
# print(res.json())
# cls.cookie = res.cookies

def test_add(self):
data1 = {
'id': '',
'userName': '崔瑞雪222233',
'userSex': '0',
'userMobile': '15695368956',
'userEmail': '1112 @ qq.com',
'userAccount': '崔瑞雪2222331',
'loginPwd': '123456',
'confirmPwd': '123456'
}
url2 = 'http://124.220.179.221:8081/cms/manage/saveSysUser.do'
res1 = self._session.request('post', url2, data=data1, cookies=self.cookie)
print(res1.status_code)
print(res1.json())
def test_delete(self):
url3 = 'http://124.220.179.221:8081/cms/manage/deleteByIds.do'
data3 = {
'ids': '1073'
}
res2 = self._session.request('post', url3, data=data3, cookies=self.cookie)
print(res2.status_code)
print(res2.json())
def test_update(self):
data4 = {
'id': '931',
'userName': '68',
'userSex': '0',
'userMobile': '17366275050',
'userEmail': '602212084@qq.com',
'userAccount': 'admin45269',
'loginPwd': '123456',
'confirmPwd': '123456'
}
url4 = ' http://124.220.179.221:8081/cms/manage/saveSysUser.do'
res3 = self._session.request('post', url4, data=data4, cookies=self.cookie)
print(res3.status_code)
print(res3.json())
def test_updateUserStatus(self):
url5 = 'http://124.220.179.221:8081/cms/manage/updateUserStatus.do'
data5 = {
'userId': '1102',
'userStatus': '1'
}
res4 = self._session.request('post', url5, data=data5, cookies=self.cookie)
print(res4.status_code)
print(res4.json())
def test_updateLoginPwd(self):
url6 = 'http://124.220.179.221:8081/cms/manage/updateLoginPwd.do'
data6 = {
'id': '1106',
'loginPwd': '123456',
'loginPwd2': '123456'
}
res5 = self._session.requests.request('post', url6, data=data6, cookies=self.cookie)
print(res5.status_code)
print(res5.json())
if __name__=="__main__":
unittest.main()

















































Copyright © 2010-2022 mfbz.cn 版权所有 |关于我们| 联系方式|豫ICP备15888888号