requests类库之get
requests是python的接口测试的类库。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、道外网站维护、网站推广。
一、安装
直接使用pip install requests即可安装。
二、发送一个get请求
response的返回包含了body、cookies、status、headers等信息,在拿到response之后,可以通过text,content等方法,得到body等的信息,可以通过cookies、encoding、status_code等属性获取关键信息。从而使用这些返回值进行断言判断。
# coding:utf-8 import requests import json import unittest.test.test_assertions class TestV3(unittest.TestCase): def test_get(self): host = "https://ruby-china.org" path = "/api/v3/topics?limit=1" url = host + path headers = {"Content-Type":"Application/json","User-Agent":"Chrome"} response = requests.get(url, headers = headers) #p text = json.loads(response.text) content = json.loads(response.content) apparent_encoding = response.apparent_encoding links = response.links redirect = response.is_permanent_redirect is_redirect = response.is_redirect next = response.next ok = response.ok #f header = response.headers url = response.url status_code = response.status_code reason = response.reason request = response.request raw = response.raw encoding = response.encoding cookies = response.cookies # print('text:',text) # print('content:',content) # print('header:',header) # print('url:',url) # print('redirect:',redirect) # print('status_code:',status_code) # print('reason:',reason) # print('request:',request) # print('raw:',raw) # print('encoding:',encoding) # print('apparent_encoding:',apparent_encoding) # print('links:',links) # print('cookies:',cookies) # print('is_redirect:',is_redirect) # print('next:',next) # print('ok:',ok) self.assertEqual(status_code, 200) self.assertEqual(redirect, False) self.assertEqual(encoding,'utf-8') if __name__ == '__main__': unittest.main()
网站名称:requests类库之get
标题来源:http://pwwzsj.com/article/ipohho.html