python 使用 requests 做 http 请求
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 请求的更多相关文章
- 【Python】 requests 各种参数请求的方式
Python使用requests发送post请求 1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form ...
- 12.Python使用requests发送post请求
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- Python使用requests发送post请求的三种方式
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- 使用Python的requests模块编写请求脚本
requests模块可用来编写请求脚本. 比如,使用requests的post函数可以模拟post请求: resp = requests.post(url, data = content) url即为 ...
- Python 使用 requests 模块发送请求的使用及封装
一.requests 模块基本使用 1.准备接口的URL.请求参数.请求头 # 1. 构造注册.登录.充值请求的url register_url = "注册url" login_u ...
- 利用python的requests发送http请求
>>> from requests import put, get >>> put('http://localhost:5000/todo1', data={'da ...
- 分别用postman和python做post请求接口功能测试
前几天,在做一个post请求的接口功能测试的时候,发现数据始终无法入库, 认真加仔细检查了请求的url.方式.参数,均没有问题 找到技术确认,原来是需要传json格式数据 在头信息中加上类型,body ...
- 想在java接口自动化里用上Python的requests?这样做就可以了
相信现在很多的公司自动化测试重点都在接口层,因为接口测试更加接近代码底层,相对于UI自动化,接口自动化有着开发更快.覆盖更全.回报率高等优点. 接口自动化代码实现不难,本质上就是代码模拟发送请求,然后 ...
- python利用requests库模拟post请求时json的使用
我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...
随机推荐
- kali 更改图标村大小
1.“设置” --> "通用辅助功能" --> "大号字体" 2.在终端中输入 “gnome-tweaks” 打开 优化 --> 扩展 -- ...
- 在myecplice中关联svn
1:下载插件 site-1.8.22 2:找到myecplic的安装目录 下的dropins 文件夹(例如:C:\Users\han\AppData\Local\MyEclipse Professio ...
- C运算符优先级和结合性
C中运算符优先级和结合性一览表: 在上表中能总结出一下规律: (1)结合方向只有三个是从右往左,其余都是从左往右: (2)逗号运算符的优先级最低: (3)对于优先级,有一个普遍规律:算术运算符 > ...
- C++动态规划实现查找最长公共子序列
问题描述: 给定两个序列X={x1,x2,…,xm}和Y={y1,y2,…,yn},找出X和Y的最长公共子序列.(给定两个序列X和Y,当另一序列Z既是X的子序列又是Y的子序列时,称Z是序列X和Y的公共 ...
- MODBUS 数据格式相关记录
串口通讯格式: 串口通讯可以分为同步通讯(Synchronous)和异步通讯(Asynchronous).同步通讯时有一根时钟信号,数据格式中没有起始位和停止位:异步通讯中没有时钟信号,数据格式中包含 ...
- Scala 数组操作之Array、ArrayBuffer以及遍历数组
ArrayBuffer 在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,则可以使用ArrayBuffer. // 如果不想每次都使用全限定名,则可以预先导入Array ...
- Matlab的solve()函数的使用方法
Matlab的solve()函数的使用方法 1.首先是对方程的求解 不废话直接上例子 syms x: eq=x^2+2*x+1; s=solve(eq,x); 结果如下 完美的算出了方程的解 现在对上 ...
- k8s Helm安装Prometheus Operator
Ubuntu 18 Kubernetes集群的安装和部署 以及Helm的安装完成了k8s的集群和helm的安装,今天我们来看看Prometheus的监控怎么搞.Prometheus Operator ...
- PTA 甲级 1139
https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...
- 总结:WPF中模板需要绑定父级别的ViewModel该如何处理
原文:总结:WPF中模板需要绑定父级别的ViewModel该如何处理 <ListBox ItemsSource="{Binding ClassCollection}"> ...