Python request操作步骤及代码实例

作者:天天向上327 时间:2022-03-16 10:42:47 

操作步骤

A.cmd输入:pip install requests,安装requests

B.py文件导入:import requests

C.get

  • 调用get: r = requests.get(url)

  • 断言:self.assertEqual(r.status_code, 200)

  • 说明:status_code为状态返回值,如200表示访问成功

D.post

入参为json格式需要把字典转成json格式: json_data =json.dumps({"usename": "test","password" : "123456"}) ,

请求报头为json格式:self.json_headers= {'content-type': 'application/json'}

调用post(请求与返回都是josn格式):

r = requests.post(url,data=json_data,headers=self.json_headers)

如果返回内容为:[{'name': 'zhangshan', 'age': '18'}, {'name': 'lisi', 'age': '29'}]

断言1:self.assertEqual(r.json()[0]['name'], 'zhangshan') ,说明r.json()转成list,list里面是字典,取list[0]第一个值,取字典key值['name']

如果返回内容:{'success': 'true', 'msg': 'chengong'}

断言2:self.assertEqual(r.json()['success'], 'true'),说明r.json()转成字典,取字典key值['success']

config.json 配置文件内容:


[
{
 "request" :
  {
   "method": "post",
   "uri" : "/login",
   "file": {
    "json": "user.json"
   }
  },
 "response" :
 {
  "json": {"success": "true","msg": "chengong"}
 }
},
{
 "request" :
  {
   "method": "post",
   "uri" : "/data"
  },
 "response" :
 {
  "file" : "data.json"
 }
},
{
 "request" :
  {
   "method": "get",
   "uri" : "/home"
  },
 "response" :
 {
   "text" : {
    "template": "true"
   }
 }
}
]

data.json 返回值文件内容:


[
{
 "name": "zhangshan",
 "age": "18"
},
{
 "name": "lisi",
 "age": "29"
}
]

user.json 入参文件内容:


{
"usename": "test",
"password" : "123456"
}

python文件“login.py”内容:


#!/usr/bin/python3
# encoding:utf-8
import unittest
import requests
import json

class login(unittest.TestCase):
 def setUp(self):
   self.d ='http://127.0.0.1:9999'
   self.json_headers= {'content-type': 'application/json'}

def tearDown(self):
   pass
 def test_gethome(self):
   url = self.url('/home')#http://127.0.0.1:9999/home
   r = requests.get(url)
   self.assertEqual(r.text, 'true')
   self.assertEqual(r.status_code, 200)

def test_postlogin(self):
   url= self.url('/login')
   json_data =json.dumps({"usename": "test","password" : "123456"})
   r = requests.post(url,data=json_data,headers=self.json_headers)
   #{'success': 'true', 'msg': 'chengong'}
   self.assertEqual(r.json()['success'], 'true')

def test_postdata(self):
   url= self.url('/data')
   r = requests.post(url,data={},headers=self.json_headers)
   print(len(r.json()))#r.josn返回list,长度2
   #[{'name': 'zhangshan', 'age': '18'}, {'name': 'lisi', 'age': '29'}]
   self.assertEqual(r.json()[0]['name'], 'zhangshan')
   self.assertEqual(r.json()[-1]['age'],'29')

def url(self,path):
   return self.d + path
if __name__=='__main__':
 unittest.main()

运行结果

.2
...
.----------------------------------------------------------------------
.Ran 3 tests in 0.036s
.
.OK

来源:https://www.cnblogs.com/yiwenrong/p/12658855.html

标签:Python,request,操作
0
投稿

猜你喜欢

  • 数据库之SQL技巧整理案例

    2024-01-26 00:46:27
  • 使用postman进行接口测试的方法(测试用户管理模块)

    2023-03-13 15:59:11
  • Vue组件公用方法提取mixin实现

    2024-06-07 16:02:47
  • python实现人脸识别经典算法(一) 特征脸法

    2021-07-15 11:35:03
  • 3种适用于Python的疯狂秘密武器及原因解析

    2023-02-08 09:10:30
  • 实现框架页面iframe的背景透明方法

    2008-06-18 12:21:00
  • Python 炫技操作之合并字典的七种方法

    2022-07-16 05:56:12
  • 利用Python实现学生信息管理系统的完整实例

    2022-03-12 10:35:03
  • python之生产者消费者模型实现详解

    2021-12-03 14:37:15
  • python中waitKey实例用法讲解

    2022-10-27 22:33:33
  • Python 获取numpy.array索引值的实例

    2021-01-19 21:36:57
  • python列表中常见的一些排序方法

    2023-08-17 08:48:45
  • Python Matplotlib绘制箱线图boxplot()函数详解

    2021-03-09 11:16:07
  • 在IntelliJ IDEA中使用Java连接MySQL数据库的方法详解

    2024-01-27 06:56:35
  • Python算法输出1-9数组形成的结果为100的所有运算式

    2022-05-02 22:45:48
  • python模型集成知识点总结

    2023-08-03 00:16:16
  • Python自定义函数计算给定日期是该年第几天的方法示例

    2021-07-25 02:38:10
  • 用Python制作一个可以聊天的皮卡丘版桌面宠物

    2021-12-05 10:44:41
  • 解决Python一行输出不显示的问题

    2021-05-19 19:21:46
  • 用Python给文本创立向量空间模型的教程

    2021-07-13 17:06:41
  • asp之家 网络编程 m.aspxhome.com