requests安装
先看下怎么安装requests, 执行以下命令:

pip install requests

安装好后如何导入requests模块呢? 如下所示:

import requests

基本示例
下面我们看一个基本的示例, 体验下requests的强大, 直接上代码演示利用requests访问github的api, 具体api说明请参见:

https://developer.github.com/v3

代码示例

# 导入模块
import requests
if __name__ == "__main__":
print("fighter007 - requests示例")
# 发送HTTP GET请求, 获取github API列表
r = requests.get("https://api.github.com")
# 请求返回码
status_code = r.status_code
# 完整的返回头
headers = r.headers
# 请求返回头 content-type的值
content_type = r.headers["content-type"]
# 返回内容编码类型
code = r.encoding
# 返回内容文本
text = r.text
# 若返回结果为json格式, 我们可以获取其json格式内容
json_data = r.json()
# 打印上述所有获取到的值
print("状态码: ", status_code)
print("返回头: ", headers)
print("content-type: ", content_type)
print("编码: ", code)
print("文本内容: ", text)
print("json串内容: ", json_data)

将上述代码保存至requests_demo.py中, 执行下属命令运行:

Fighter007 - requests示例
状态码: 200
返回头: {'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload', 'X-RateLimit-Limit': '', 'Content-Encoding': 'gzip', 'Content-Security-Policy': "default-src 'none'", 'X-RateLimit-Reset': '', 'Access-Control-Allow-Origin': '*', 'X-GitHub-Media-Type': 'github.v3; format=json', 'ETag': 'W/"7dc470913f1fe9bb6c7355b50a0737bc"', 'Date': 'Mon, 19 Feb 2018 01:32:20 GMT', 'Status': '200 OK', 'Vary': 'Accept, Accept-Encoding', 'X-Frame-Options': 'deny', 'Server': 'GitHub.com', 'X-Runtime-rack': '0.012498', 'Access-Control-Expose-Headers': 'ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval', 'Transfer-Encoding': 'chunked', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'X-GitHub-Request-Id': 'CB9C:6BE0:18167B6:1F2AC7F:5A8A2923', 'Content-Type': 'application/json; charset=utf-8', 'X-RateLimit-Remaining': '', 'Cache-Control': 'public, max-age=60, s-maxage=60'}
content-type: application/json; charset=utf-8
编码: utf-8
文本内容: {"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}
json串内容: {'public_gists_url': 'https://api.github.com/gists/public', 'user_repositories_url': 'https://api.github.com/users/{user}/repos{?type,page,per_page,sort}', 'authorizations_url': 'https://api.github.com/authorizations', 'organization_repositories_url': 'https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}', 'commit_search_url': 'https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}', 'gists_url': 'https://api.github.com/gists{/gist_id}', 'organization_url': 'https://api.github.com/orgs/{org}', 'following_url': 'https://api.github.com/user/following{/target}', 'events_url': 'https://api.github.com/events', 'rate_limit_url': 'https://api.github.com/rate_limit', 'starred_gists_url': 'https://api.github.com/gists/starred', 'team_url': 'https://api.github.com/teams', 'keys_url': 'https://api.github.com/user/keys', 'repository_search_url': 'https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}', 'code_search_url': 'https://api.github.com/search/code?q={query}{&page,per_page,sort,order}', 'emails_url': 'https://api.github.com/user/emails', 'notifications_url': 'https://api.github.com/notifications', 'current_user_authorizations_html_url': 'https://github.com/settings/connections/applications{/client_id}', 'current_user_repositories_url': 'https://api.github.com/user/repos{?type,page,per_page,sort}', 'starred_url': 'https://api.github.com/user/starred{/owner}{/repo}', 'feeds_url': 'https://api.github.com/feeds', 'issue_search_url': 'https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}', 'emojis_url': 'https://api.github.com/emojis', 'user_organizations_url': 'https://api.github.com/user/orgs', 'user_url': 'https://api.github.com/users/{user}', 'hub_url': 'https://api.github.com/hub', 'current_user_url': 'https://api.github.com/user', 'followers_url': 'https://api.github.com/user/followers', 'repository_url': 'https://api.github.com/repos/{owner}/{repo}', 'user_search_url': 'https://api.github.com/search/users?q={query}{&page,per_page,sort,order}', 'issues_url': 'https://api.github.com/issues'}

本文演示了GET方法及如何获取响应状态码、 响应头、 编码、 文本内容、 json内容。

Requests接口测试(二)的更多相关文章

  1. requests接口测试-requests的安装

    requests接口测试-requests的安装 安装常见问题 提示连接不上,443问题 一般是因为浏览器设置了代理,关闭代理. 网络加载慢,设置国内镜像地址 1.pip安装 2.pycharm安装 ...

  2. Requests接口测试-对cookies的操作处理(二)

    我们继续来讨论一下cookie这方面的内容,我们都知道cookie是数据,一般的话在我接口测试中,数据都是要和代码进行分离的.本篇内容,我们队cookie内容进行处理,我们把登陆成功后的cookie写 ...

  3. 自动化测试===unittest和requests接口测试案例,测试快递查询api(二)

    在原来基础上生成测试报告: 首先需要  HTMLTestRunner.py 的unittest生成报告文件 (源码,自动化测试===unittest配套的HTMLTestRunner.py生成html ...

  4. Python+Requests接口测试教程(1):Fiddler抓包工具

    本书涵盖内容:fiddler.http协议.json.requests+unittest+报告.bs4.数据相关(mysql/oracle/logging)等内容.刚买须知:本书是针对零基础入门接口测 ...

  5. Requests接口测试(一)

    接口测试概念 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关 ...

  6. 使用robotframework做接口测试二——处理响应数据

    初使用RequestsLibrary做接口测试时,你会不会感到困惑,为什么会有${resp.content}, ${resp.status_code}这样的写法,这个status_code什么鬼,f5 ...

  7. Python+Requests接口测试教程(2):

    开讲前,告诉大家requests有他自己的官方文档:http://cn.python-requests.org/zh_CN/latest/ 2.1 发get请求 前言requests模块,也就是老污龟 ...

  8. 利用unittest+ddt进行接口测试(二):使用yaml文件管理测试数据

    知道ddt的基本使用方法之后,练习把之前用excel文件来维护的接口测试用例改用unittest+ddt来实现. 这里我选用yaml文件来管理接口参数,开始本来想用json,但是json无法添加注释, ...

  9. Python+Requests接口测试教程(2):requests

    开讲前,告诉大家requests有他自己的官方文档:http://cn.python-requests.org/zh_CN/latest/ 2.1 发get请求 前言requests模块,也就是老污龟 ...

随机推荐

  1. AppScan 8.0.3安全漏洞扫描总结

    本文记录了通过AppScan 8.0.3工具进行扫描的安全漏洞问题以及解决方案, 1.使用SQL注入的认证旁路 问题描述: 解决方案: 一般通过XSSFIlter过滤器进行过滤处理即可,通过XSSFI ...

  2. 为什么中国出不了facebook和Twitter?

    我们坐拥全球最大基数的网民,我们拥有让人骄傲的四大发明,我们有有流传全世界的孙子兵法,可是在互联网时代,我们却落后了.互联网可以说是江山人才辈辈出,各领风骚三两年. 让我们来简单地回顾一下近几年的互联 ...

  3. Fragment和FragmentActivity的使用

    可以分为下面的几部分: 使用支持库 创建一个Fragment 创建一个动态UI 多个Fragment之间的通信 1.使用支持库 如果您的应用需要运行在3.0及以上的版本,可以忽略这部分内容. 如果您的 ...

  4. 大数据竞赛平台——Kaggle 入门篇

    这篇文章适合那些刚接触Kaggle.想尽快熟悉Kaggle并且独立完成一个竞赛项目的网友,对于已经在Kaggle上参赛过的网友来说,大可不必耗费时间阅读本文.本文分为两部分介绍Kaggle,第一部分简 ...

  5. 多线程使用信号量sem_init,sem_wait,sem_post

    信号量的主要函数有: int sem_init(sem_t *sem,int pshared,unsigned int value); int sem_wait(sem_t *sem); int se ...

  6. 【转】Jmeter做web压力测试时设置持续时间注意点

    头一回使用jmeter做web的压力测试,遇到个很莫名其妙的问题,不管我的线程组怎么设置,它就是执行一次就结束了. 设置循环次数为300,不使用调度器--〉执行一次就结束了,循环次数未生效 设置循环次 ...

  7. 【转】学习使用Jmeter做压力测试(二)--压力测试的实施

    JMeter测试步骤: 1.建立测试计划 2.添加线程组 3.添加HTTP请求 4.增加监听器 5.执行测试计划 6.根据JMeter提供的报告分析结果 一.目标 测试访问目标服务器网站首页的每秒查询 ...

  8. UE4事件相关总结

    转自:http://blog.ch-wind.com/ue4-event-overview/ 事件机制是实现游戏内逻辑的重要部分,在开始进行游戏逻辑的设计和实现之前,对UE4的事件机制进行理解是非常必 ...

  9. UseSubmitBehavior="false" 防止页面重复提交bug

    OnClientClick="this.disabled=true;" UseSubmitBehavior="false" 注: 1.当设置UseSubmitB ...

  10. C Primer Plus学习笔记(十)- 字符串和字符串函数

    getchar() 和 putchar() getchar() 函数不带任何参数,它从输入队列中返回下一个字符 下面的语句读取下一个字符输入,并把该字符的值赋给变量 ch ch =getchar(); ...