python接口自动化测试(二)-requests.get()

环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括:

  1. requests常用请求方法使用,包括:get,post
  2. requests库中的Session、Cookie的使用
  3. 其它高级部分:认证、代理、证书验证、超时配置、错误异常处理等。

本节首先来了解一下requests库中如何发送get请求:

一、看下方法定义:

1、到官方文档去了下requests.get()方法的定义,如下:

2、点击右上角的【source】,看一下它的源码如下:

看到最后一行return,get方法最后是通过调用 requests.request 方法实现的,其实在其它的请求方法如post,put,head,delete等方法都是调用的request方法,然后把请求方法的类型传递给request方法第一个参数。

3、HTTP协议是一个基于请求/响应模式的、无状态的,应用层协议。既然有请求,就有响应,来看下resquest中常用的响应信息:

二、get方法简单使用: 

1、不带参数的get:

# -*- coding:utf-8 -*-
#不带参数的get

import requests
import json

host = "http://httpbin.org/"
endpoint = "get"

url = ‘‘.join([host,endpoint])
r = requests.get(url)
#response = r.json()

print type(r.text)
print (eval(r.text))

输出:

{
    ‘origin‘: ‘183.14.133.88‘,
    ‘headers‘: {
        ‘Connection‘: ‘close‘,
        ‘Host‘: ‘httpbin.org‘,
        ‘Accept-Encoding‘: ‘gzip,
        deflate‘,
        ‘Accept‘: ‘*/*‘,
        ‘User-Agent‘: ‘python-requests/2.18.1‘
    },
    ‘args‘: {

    },
    ‘url‘: ‘http: //httpbin.org/get‘
}

2、 带参数的get:

# -*- coding:utf-8 -*-
#带参数的get

import requests
import json

host = "http://httpbin.org/"
endpoint = "get"

url = ‘‘.join([host,endpoint])
params = {"show_env":"1"}
r = requests.get(url=url,params=params)

print r.url

输出:

http://httpbin.org/get?show_env=1
{
    ‘origin‘: ‘183.14.133.88‘,
    ‘headers‘: {
        ‘X-Request-Id‘: ‘ebe922b4-c463-4fe9-9faf-49748d682fd7‘,
        ‘Accept-Encoding‘: ‘gzip,
        deflate‘,
        ‘X-Forwarded-Port‘: ‘80‘,
        ‘Total-Route-Time‘: ‘0‘,
        ‘Connection‘: ‘close‘,
        ‘Connect-Time‘: ‘0‘,
        ‘Via‘: ‘1.1vegur‘,
        ‘X-Forwarded-For‘: ‘183.14.133.88‘,
        ‘Accept‘: ‘*/*‘,
        ‘User-Agent‘: ‘python-requests/2.18.1‘,
        ‘X-Request-Start‘: ‘1504755961007‘,
        ‘Host‘: ‘httpbin.org‘,
        ‘X-Forwarded-Proto‘: ‘http‘
    },
    ‘args‘: {
        ‘show_env‘: ‘1‘
    },
    ‘url‘: ‘http: //httpbin.org/get?show_env=1‘
}

3、带header的get:

# -*- coding:utf-8 -*-

import requests
import json

host = "http://httpbin.org/"
endpoint = "get"

url = ‘‘.join([host,endpoint])
headers = {"User-Agent":"test request headers"}

r = requests.get(url)
r = requests.get(url,headers=headers)
#response = r.json()
print (eval(r.text))[‘headers‘][‘User-Agent‘]

输出:

test request headers

4、同时带参数和header:

# -*- coding:utf-8 -*-
import requests
import json

host = "http://httpbin.org/"
endpoint = "get"

url = ‘‘.join([host,endpoint])
headers = {"User-Agent":"test request headers"}
params = {"show_env":"1"}

r = requests.get(url)
r = requests.get(url,headers=headers,params=params)

#response = r.json()
print (eval(r.text))[‘headers‘][‘User-Agent‘]
print r.url

输出:

test request headers
http://httpbin.org/get?show_env=1
时间: 2024-08-06 07:59:24

python接口自动化测试(二)-requests.get()的相关文章

python接口自动化测试(二)-requests.post()

上一节介绍了  requests.get()  方法的基本使用,本节介绍  requests.post()  方法的使用: 本文目录: 一.方法定义 二.post方法简单使用 1.带数据的post 2.带header的post 3.带json的post 4.带参数的post 5.普通文件上传 6.定制化文件上传 7.多文件上传 一.方法定义: 1.到官方文档去了下requests.post()方法的定义,如下: 2.源码: 3.常用返回信息: 二.post方法简单使用: 1.带数据的post:

python接口自动化测试二:python代码实现接口测试

url = '接口地址' r = requests.get(url)           #发送get请求 print(r.status_code)            #打印状态码,若有重定向,返回的是重定向之后的代码 print(r.headers)                #打印返回的报头(头部) print(r.text)                   #查看返回结果的文本形式 r.status_code                #响应状态码 r.content   

python接口自动化测试二:常用操作

url = '接口地址' r = requests.get(url)                      # 发送get请求 print(r.status_code)              # 打印状态码,若有重定向,返回的是重定向之后的代码 print(r.headers)                 # 打印返回的报头(头部) print(r.text)                    # 查看返回结果的文本形式 r.status_code               

python接口自动化测试二十三:文件上传

# 以禅道为例: 一.创建一个类,类里面写一个登录方法: import requestsclass LoginZentao(): def __init__(self, s): # 初始化 self.s = s # 定义一个全局的s def login(self): r = self.s.post() # self.s调用全局的s pass if __name__=='__main__': s = requests.session() zentao = LoginZentao(s) # 类实例化为

python接口自动化测试(三)-requests.post()

上一节介绍了  requests.get()  方法的基本使用,本节介绍  requests.post()  方法的使用: 本文目录: 一.方法定义 二.post方法简单使用 1.带数据的post 2.带header的post 3.带json的post 4.带参数的post 5.普通文件上传 6.定制化文件上传 7.多文件上传 一.方法定义: 1.到官方文档去了下requests.post()方法的定义,如下: 2.源码: 3.常用返回信息: 二.post方法简单使用: 1.带数据的post:

python接口自动化测试二十五:执行所有用例,并生成HTML测试报告

    import requestsimport unittest class TestQQ(unittest.TestCase):    '''测试QQ号接口'''      # 此注释将展示到测试报告的测试组类 def test_qq(self):        '''测试QQ号码,正确的appkey'''      # 此注释将展示到测试报告的用例标题        url = 'http://japi.juhe.cn/qqevaluate/qq'        par = {     

python 接口自动化测试二(request.get)

环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括: requests常用请求方法使用,包括:get,post requests库中的Session.Cookie的使用 其它高级部分:认证.代理.证书验证.超时配置.错误异常处理等. 本节首先来了解一下requests库中如何发送get请求: 一.看下方法定义: 1.到官方文档去了下requests.get()方法的定义,如下: 2.点击右上角的[source],看一下它的源码如下: 看到最后一行return,get方法

python接口自动化测试二十六:使用pymysql模块链接数据库

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time    : 2018/5/28 18:51# @Author  : StalloneYang# @File    : mysql_test.py# @desc: # 连接数据库 import pymysql.cursors # 连接MySQL数据库connection = pymysql.connect(host='localhost', port=3306, user='yang', pass

python接口自动化测试二十:函数写接口测试

# coding:utf-8import requestsimport refrom bs4 import BeautifulSoup # s = requests.session() # 全局的s def get_token(s): ''' fuction: 获取token args: s 参数 ->s = requests.session() :return anti_token ->{'X-Anit-Forge-Token': 'xx', 'X-Anit-Forge-Code': '38