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 ...
随机推荐
- SpringBoot系列教程web篇Listener四种注册姿势
java web三要素Filter, Servlet前面分别进行了介绍,接下来我们看一下Listener的相关知识点,本篇博文主要内容为SpringBoot环境下,如何自定义Listener并注册到s ...
- Storm里面fieldsGrouping和Field参数和 declareOutputFields
Fields,个人理解,类似于一张表,你取那些字段以及这些字段所对应的数据给后面的bolt用 这个Field通常和fieldsGrouping分组机制一起使用,这个Field特别难理解,我自己也是在网 ...
- .net中加密与解密
.Net中的加密解密 引言 在一些比较重要的应用场景中,通过网络传递数据需要进行加密以保证安全.本文将简单地介绍了加密解密的一些概念,以及相关的数字签名.证书,最后介绍了如何在.NET中对数据进行对称 ...
- 第四节:配置的读取、StartUp类、内置依赖注入和扩展改造
一. 配置的读取 在Asp.Net Core中,有一个 appsettings.json 文件,用于存储相应的配置信息,读取的时,要通过构造函数注入:IConfiguration Configurat ...
- 简易版php文件上传_超详细详解
1.php简化版的图片上传(没有各种验证) 1234 <form action="" enctype="multipart/form-data" meth ...
- python selenium爬虫工具
今天seo的同事需要一个简单的爬虫工具, 根据一个url地址,抓取改页面的a连接,然后进入a连接里面的页面再次抓取a连接 1.需要一个全局的set([])集合来保存抓取的url地址 2.由于现在单页面 ...
- 更新Linux内核
说明:为了安装Docker,当前虚拟机不满足要求,版本如下: [root@localhost116 ~]# uname -r -.el6.x86_64 [root@localhost116 ~]# c ...
- jwt的简单使用
目录 简介 java版本 golang版本 简介 使用jwt对数据进行存储加密,分为java和golang版本. java版本 maven配置 <dependency> <group ...
- Unable to connect to HBase using Phoenix JDBC Driver
Feb 01, 2017; 5:21pm Unable to connect to HBase using Phoenix JDBC Driver 9 posts Hi All, I am try ...
- c#读取数据库bool值
数据库里bit这个布尔类型的字段,非常实用,但是在c#里读取时,许多人喜欢犯一些错误,导致运行报错. 实际中,有效的正确读取方法只有以下两种: int xxx= Convet.ToInt16(read ...