1. get

import requests

# 最简单的get请求
r = requests.get(url)
print(r.status_code)
print(r.json()) # url 中?key=value&key=value
r = requests.get(url, params=params) # form 表单
params = {"username":"name", "password":"passw0rd"}
headers = {'Content-Type':'application/x-www-form-urlencoded'}
r = requests.get(url, params=params, headers=headers) # 下载
r = requests.get(url)
r.raise_for_status()
with open(target, 'wb') as f:
for ch in r.iter_content(10000):
result_file_size += f.write(ch)

2. post请求

data = {'name':'train', 'device':'CN0989'}
r = requests.post(url, json=data) #上传
files = {
"file": (os.path.basename(filepath), open(filepath, "rb"), "application/zip")
}
print('POST %s'%url)
with open(filepath, 'rb') as f:
r = requests.post(url, files=files)
r = requests.post(url, json=json_dict, headers=headers)

# 保持response流,保证接收完整
with requests.get('https://httpbin.org/get', stream=True) as r:
# Do things with the response here. # 上传文件
with open('massive-body', 'rb') as f:
requests.post('http://some.url/streamed', data=f) # 上传文件 2 指定文件格式
files = {'file': (filename, open(filename, 'rb), 'application/zip')}
r = requests.post(url, files=files)
print(r.status_code)
print(r.json()) # 上传多个文件
url = 'https://httpbin.org/post'
multiple_files = [
('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
r = requests.post(url, files=multiple_files)
r.text

3. 登录

_session = requests.Session()

# login
url = '%s/login'%_basic_url
params = {"username":"admin", "password":"admin"}
headers = {'Content-Type':'application/x-www-form-urlencoded'}
r = _session.post(url, params=params, headers=headers) #做其他请求
r = _session.get(url) _session.close()

4. 使用basic登录

from requests.auth import HTTPBasicAuth

r = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
r.encoding = 'utf-8'
print r.status_code
print r.text
print r.json()

python 使用 requests 做 http 请求的更多相关文章

  1. 【Python】 requests 各种参数请求的方式

    Python使用requests发送post请求 1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form ...

  2. 12.Python使用requests发送post请求

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  3. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  4. 使用Python的requests模块编写请求脚本

    requests模块可用来编写请求脚本. 比如,使用requests的post函数可以模拟post请求: resp = requests.post(url, data = content) url即为 ...

  5. Python 使用 requests 模块发送请求的使用及封装

    一.requests 模块基本使用 1.准备接口的URL.请求参数.请求头 # 1. 构造注册.登录.充值请求的url register_url = "注册url" login_u ...

  6. 利用python的requests发送http请求

    >>> from requests import put, get >>> put('http://localhost:5000/todo1', data={'da ...

  7. 分别用postman和python做post请求接口功能测试

    前几天,在做一个post请求的接口功能测试的时候,发现数据始终无法入库, 认真加仔细检查了请求的url.方式.参数,均没有问题 找到技术确认,原来是需要传json格式数据 在头信息中加上类型,body ...

  8. 想在java接口自动化里用上Python的requests?这样做就可以了

    相信现在很多的公司自动化测试重点都在接口层,因为接口测试更加接近代码底层,相对于UI自动化,接口自动化有着开发更快.覆盖更全.回报率高等优点. 接口自动化代码实现不难,本质上就是代码模拟发送请求,然后 ...

  9. python利用requests库模拟post请求时json的使用

    我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...

随机推荐

  1. 实现简单的AOP前置后置增强

    AOP操作是我们日常开发经常使用到的操作,例如都会用到的spring事务管理.今天我们通过一个demo实现对一个类的某一个方法进行前置和后置的增强. //被增强类 public class PetSt ...

  2. aspxgridview 实现单选

    <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server"             AutoGenerat ...

  3. linux下安装谷歌拼音输入法

    linux下安装谷歌拼音输入法 输入以下命令,等待安装完成. sudo apt-get install fcitx 接着输入,完成安装谷歌中文输入法 sudo apt-get install fcit ...

  4. [EXP]CVE-2019-1821 Cisco Prime Infrastructure思科未授权远程代码执行漏洞

    CVE-2019-1821 Cisco Prime Infrastructure Remote Code Execution https://srcincite.io/blog/2019/05/17/ ...

  5. JavaSE 面试题: 方法的参数传递机制

    JavaSE 面试题 方法的参数传递机制 import java.util.Arrays; public class Test { public static void main(String[] a ...

  6. Svn CleanUp failed解决方案

    在项目目录下找到wc.db文件,使用sqlite工具打开,清空main下的WC_LOCK和 WORK_QUEUE表即可.

  7. 大一C语言课程设计——班级档案管理系统

    记录我在大一第二学期期末做的C语言课程毕业设计 1. 班级档案管理系统运用到的主要结构体 typedef struct birthday //出生日期{int year;int month;int d ...

  8. myeclipse导入项目中文乱码怎么解决教程

    大家在Myeclipse导入项目的时候,应该都遇见过一些乱码的问题,不单单只是Myeclipse有这个问题,那么怎么解决Myeclipse导入项目乱码的问题呢,问题出现的原因是什么呢,下面来看看答案. ...

  9. RabbitMQ 的消息持久化与 Spring AMQP 的实现剖析

    文章目录 1. 原生的实现方式 2. Spring AMQP 的实现方式   要从奔溃的 RabbitMQ 中恢复的消息,我们需要做消息持久化.如果消息要从 RabbitMQ 奔溃中恢复,那么必须满足 ...

  10. 多态性   类(class)的四则运算

       我们知道c语言中可以整型数据或浮点型等做四则运算,而自己写的类也可做四则运算,是不是感觉奇怪,可以看以下代码是如何完成类之间的四则运算: #include "stdafx.h" ...