使用 python requests 模块发送 http 请求及接收响应

2023-05-16

内容概要

  • 如何构建GET 与 POST request 请求消息
  • 对 request 的header , query string, message body 定制化
  • http header参数 content-type 的设置
  • 分析request, response 消息数据
  • 通过POST请求上传文件
  • 请求与响应使用 json 格式

为什么推荐使用 requests 模块?

用 python 编写 http request 消息代码时,建议用requests库。因为requests比urllib内置库更为简捷,requests可以直接构造get,post请求并发送,而urllib.request只能先构造get,post请求消息内容,然后再发送。并且requests 模块提供了更友好的方法与属性来解析response消息内容。

1. 准备知识

1.1 HTTP request 与 Response 通讯机制

http协议是基于1种客户机(client) – 服务器(server) 的通信模式,它的下层是TCP协议。
在这里插入图片描述

  • 所有的请求request 都是由客户机发起的
  • 服务器对客户请求做出响应response
  • 每个request 都是独立于其它request消息,服务器不需要跟踪request消息的状态

1.2 Http Request 请求与响应消息

客户端发送一个HTTP请求到服务器的请求消息由四个部分组成

  • 请求行(request line)
  • 请求头部(header)、
  • 空行(CLF)
  • 请求数据(payload,或body)

下图给出了请求报文的一般格式。

在这里插入图片描述
上图中,可以看到。Request 请求行第1个字节为请求方法, 有时也称动词(verb), 常用的主要有4种方法:GET, POST, PUT, DELETE

1.3 Http Response 响应消息

服务器的响应消息, 也是由4部分组成
状态行、消息报头、空行和响应正文
在这里插入图片描述

2. 安装 requests 模块

安装requests 模块非常简单,

pip install requests

3. GET 请求

3.1 request.get() 方法

用于准备并发送 http get 请求至指定url , 并返回response 对象

requests.get(url, params=None, **kwargs)

  • url: 拟获取页面的url链接
  • params: url中的额外参数,字典或字节流格式,可选
  • **kwargs: 可选参数,共有12个控制访问的参数

url格式:http://host_ip:port/path/add?key1=value1&key2=value2
传参数用字典类型:params={ key1: value1, key2: value2 }

response = requests.get(
    'https://api.github.com/search/repositories',
    params={'q':'requests+language:python'},
)

可能遇到的问题 : 如果GET请求参数中包含汉字,常会遇到编码错误

主要原因:http协议对URL参数的编码要求是ASCII字符集,汉字是UTF-8。在发送时要进行两次编码才能将汉字转为ASCII字节码:

  • 第1次编码, 用 UTF-8 字符集,每个汉字占3个字节。
  • 第2次编码,可以用 iso-8859-1,然后再转为ASCII,也可以用其它字符集来转ASCII。

同样,接收方也要进行两次解码,才能正确地还原汉字。

还好,python 内置库urllib 提供了1条命令,1次就可以将汉字转为ASCII编码。
编码: urllib.parse.urlencode(dic)
解码: urllib.parse.unquote(dic or str)
示例代码

keyword = "天气预报"
param_list = urllib.parse.urlencode( { 'q' : keyword } )    #包含汉字
header = {'user-Agent':’haha‘}
url = 'http://www.baidu.com/s/'
response = request.get( url, params=param_list, headers = header )

3.2 Response 对象常用属性及方法

在这里插入图片描述

收到response后,需要分析响应状态码status_code,有必要可以对404, 500等出错响应进行特殊处理。

import requests
from requests.exceptions import HTTPError

for url in ['https://api.github.com', 'https://api.github.com/invalid']:
    try:
        response = requests.get(url)

        # If the response was successful, no Exception will be raised
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')  # Python 3.6
    except Exception as err:
        print(f'Other error occurred: {err}')  # Python 3.6
    else:
        print('Success!')

当调用 .raise_for_status(), 对特定的status_code将产生1个 HTTPError 异常

Status_code
在这里插入图片描述

收到响应状态码301重定向

如果收到的status_code 为301,可以从response.url中获取新的url.

response = requests.get("http://192.168.3.100/demo/") 
new_url = response.url 

异常 response 消息

如果发出request后,收到异常的response, 可以用下面的数据检查 :

>>> response = requests.post('https://httpbin.org/post', json={'key':'value'})
>>> response.request.headers['Content-Type']
'application/json'
>>> response.request.url
'https://httpbin.org/post'
>>> response.request.body
b'{"key": "value"}'

3.3 GET 方法的请求参数 Query String

get方法的请求参数是通过 params={ } 方式来传递的。

response = requests.get(
    'https://api.github.com/search/repositories',
     params={'name': 'Jack','type':'display'},
)

4. POST 请求

4.1 POST 请求参数

与GET请求不同的是, POST 请求参数是放在 request body 里发送的, 向 request的 response对象传入的数据类型可以是 tuple, dict, json 等。

# 发送字典
post_dict = {'key1': 'value1', 'key2': 'value2'}
# 发送元组
post_tuple = (('key1', 'value1'), ('key1', 'value2'))
# 发送json
post_json = json.dumps({'some': 'data'})
r1 = requests.post("http://httpbin.org/post", data=post_dict)
r2 = requests.post("http://httpbin.org/post", data=post_tuple)
r3 = requests.post("http://httpbin.org/post", data=post_json)

输出: 以Json 格式发送 POST 请求

POST /echo/post/json HTTP/1.1
Authorization: Bearer mt0dgHmLJMVQhvjpNXDyA83vA_Pxh33Y
Accept: application/json
Content-Type: application/json
Content-Length: 85
Host: reqbin.com

{
   "Id": 12345,
   "Customer": "John Smith",
   "Quantity": 1,
   "Price": 10.00
}

收到 Response

HTTP/1.1 200 OK
Content-Length: 19
Content-Type: application/json

{"success":"true"}

4.2 POST 消息设置 cookie, header

import requests
# 请求数据
url = 'http://api.shein.com/v2/member/login'

cookie = "token=code_space;"
header = {
        "cookie": cookie,
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                      "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}

data = {
    'user_id': '123456',
    'email': '123456@163.com'
}

timeout = 0.5
resp = requests.post(url, headers=header, data=data, timeout=timeout)  
print(resp.text)
print(type(resp.json()))

4.3 用 POST请求上传文件

客户端可通过POST请求,向服务器上传文件


#形式1
url = 'http://httpbin.org/post'
#定义文件对象
files = {"files":open('test.xls', 'rb')}
response = requests.post(url,files = files)
print(response.text)

#形式2
url ='http://httpbin.org/post'
files = {'file': ('t.xls', open('t.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
r.text

#形式3, 发送多个文件
url = 'http://httpbin.org/post'
files = {'file': ('t.csv', 'bb.csv')}
response = requests.post(url, files=files)
response.text

4. 请求与响应头部的 content-type 参数说明

Content-Type 参数用于告诉客户端浏览器,http 消息所包含资源的类型。这个参数在request 与 response消息中都可能包含,是 response 消息头部非常关键1个参数,也是开发者应该掌握的1个知识点。其内容格式是 IETF’s RFC 6838 标准中的 MIME Type(Multipurpose Internet Mail Extensions).

先看1个实际消息示例 :

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something

img

content-type 参数的语法格式

type/subtype
  • type 代表数据资源的大类,如 text (文本类型), video(视频类型)等
  • subtype 是资源子类,如,对于 text类,subtype 可能是 plain(纯文本),csv 或者html等。

content-type还可以附加参数

type/subtype;parameter=value

常见情形:当type是 text类型,文本内容是中文,需添加charset参数,指定编码类型:
Content-Type: text/html;charset=UTF-8

在http协议以及行业内,有很多通用的建议值,最常见的:

  • application/x-www-form-urlencoded, 这是提交表单默认的content-type设置, 对应form属性为 enctype.
  • multipart/form-data , 用于 form 上传文件
  • application/json 传json数据
  • text/csv 传送csv
  • text/html 传网页
  • text/plain text/xml 传文本
  • image/jpeg 传图片
  • video/mp4 传MP4视频

注:

  • 对于"application/x-www-form-urlencoded" 编码,如果两端都是用request编程,则不需要编解码,request 模块会自动完成。

下面是 content-type 可能用到的 type/subtype 列表:

TypeSubtype
Applicationapplication/javascript
application/pdf
application/xhtml+xml
application/json
application/ld+json
application/xml
application/zip
application/x-www-form-urlencoded
application/octet-stream : 二进制流数据(如常见的文件下载)
Audioaudio/mpeg
audio/x-ms-wma
|audio audio/x-wav
Imageimage/gif
image/jpeg
image/png
image/tiff i
mage/vnd.microsoft.icon
image/x-icon
image/vnd.djvu
image/svg+xml
Multipartmultipart/mixed
multipart/alternative
multipart/related (using by MHTML (HTML mail).)
multipart/form-data
Texttext/css
text/csv
text/html
text/plain
text/xml
Videovideo/mpeg
video/mp4
video/quicktime
video/x-ms-wmv
video/x-msvideo
video/x-flv
video/webm
VNDapplication/vnd.oasis.opendocument.text
application/vnd.oasis.opendocument.spreadsheet
application/vnd.oasis.opendocument.presentation
application/vnd.oasis.opendocument.graphics
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.ms-powerpoint
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.mozilla.xul+xml

5. 用 json 做 payload

payload 就是通过http Post,get发送的数据,包括请求参数,文件,图片等, 发送方可以将用json类型来准备这些数据,接收方用json解开。

这在Header 里约定好。如下, 但注意,header 不能是json格式。

POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 52

{
  "Id": 12345
}

response内容也可以用json来发送

{
  "success": "true",
  "data": {
    "TotalOrders": 100
  }
}

6. 其它requests 方法

其它请求消息, PUT与PATCH与 POST类似。 DELETE, HEAD与GET类似。

>>> requests.put('https://httpbin.org/put', data={'key':'value'})
>>> requests.delete('https://httpbin.org/delete')
>>> requests.head('https://httpbin.org/get')
>>> requests.patch('https://httpbin.org/patch', data={'key':'value'})
>>> requests.options('https://httpbin.org/get')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 python requests 模块发送 http 请求及接收响应 的相关文章

随机推荐

  • 基于AList实现网盘挂载和WebDAV本地挂载网盘

    AList AList是一个支持多种存储 xff0c 支持网页浏览和 WebDAV 的文件列表程序 xff0c 由 gin 和 Solidjs 驱动 AList官方文档 xff1a https alist nn ci AList官方GitH
  • 常用Windows快捷键大全

    0 简要 要将电脑玩的溜 xff0c 快捷键是必须要掌握的技能 xff0c 本文汇总了一些常用的快捷键 xff0c 相信加以练习 xff0c 一定能提高你的工作效率 笔者将常用快捷键分为四个系列 xff0c 如下所示 xff1a Win 系
  • Centos8安装MySql,完美解决

    本文使用yum安装mysql linux版本为 centos 8 参考 xff1a MySQL官网yum源 MySQL官网Linux yum安装Mysql CentOS 8 yum安装软件时 xff0c 提示无法从AppStream下载 c
  • aws亚马逊服务器Ubuntu18脚本一键重装系统为centos7

    这两天注册了aws xff0c 送了一年的最低配服务器嘛 但是可使用的系统就是有Ubuntu和Redhat 都试了试不太好用 今天就在网上看到了一键重装的脚本 就记录分享一下 先后执行下列两条命令就可以 xff1a apt get inst
  • 利用excel求特定条件下的最大/小值(maxif/minif)

    欢迎关注我的公众号 xff1a Smilecoc的杂货铺 在Excel中有sumif countif等函数可以实现求特定条件下数值的加总和计数 xff0c 那么如何在一个或多个条件下求出此时的最大值或者最小值呢 xff1f 其实sumif函
  • 时间序列(一):时间序列数据与时间序列预测模型

    时间序列系列文章 xff1a 时间序列 xff08 一 xff09 xff1a 时间序列数据与时间序列预测模型 时间序列 xff08 二 xff09 xff1a 时间序列平稳性检测 时间序列 xff08 三 xff09 xff1a ARIM
  • Windows下解压tar.gz压缩文件

    一 tar gz是什么文件 xff1f 以 tar gz为后缀的文件是一种压缩文件 xff0c 在Linux和macOS下常见 xff0c Linux和macOS都可以直接解压使用这种压缩文件 二 怎么解压tar gz 一些软件支持解压ta
  • Python安装模块(包/库)的方法

    这里写目录标题 通过pip安装正常在线安装pip命令补全更改下载镜像 离线包安装库的下载库的安装whl的安装 tar gz的安装源码安装 本地安装报错 xff08 依赖 xff09 Pycharm中安装手动安装终端命令行安装 Jupyter
  • win10 安装visual studio C++ build tools 【visualcppbuildtools_full.exe】提示安装包丢失 毁坏

    win10 安装visual studio C 43 43 build tools visualcppbuildtools full exe 提示安装包丢失 毁坏 1 问题 xff1a 安装visualcppbuildtools full
  • Excel:使用powerquery进行多表合并

    注 xff1a 本文原创为 xff1a https www cnblogs com fanyu2019 p 11175827 html 本文在原创的基础上添加修改了一点内容 目录 一 单工作簿多工作表合并二 多工作簿单工作表合并三 多工作簿
  • 利用Python调用outlook自动发送邮件

    欢迎关注我的公众号 xff0c 在这里有数据相关技术经验的优质原创文章 使用Python发送邮件有两种方式 xff0c 一种是使用smtp调用邮箱的smtp服务器 xff0c 另一种是直接调用程序直接发送邮件 而在outlook中我们一般是
  • 从 Tableau文件中获取数据方法汇总

    欢迎关注我的公众号 xff0c 在这里有数据相关技术经验的优质原创文章 在实际使用Tableau中经常会遇到需要从已有的tableau文件或仪表板中导出 提取 复制数据 xff0c 本篇文章整理了相关从Tableau文件中获取数据的方法 一
  • Excel中的数字转文本和文本转数字

    公式方法 xff1a 数字转文本 xff1a 61 TEXT A1 34 34 文本转数字 xff1a 直接乘以1即可 数字转文本 xff1a 61 A1 1 或者使用value函数 61 value 分列方法 xff1a 在数据工具 下选
  • vlookup查找匹配值超过255个字符显示#Value的解决办法

    错误原因 这一个错误的起源于在匹配字符串是否相等时出现 Value错误 xff0c 如下图黄色标注的部分 在Excel中提示的错误是 公式中所用的某个值是错误的数据类型 xff08 a value used in the formula i
  • HEXO部署博客内容到github报错

    今天在更新部署博客内容时出现了如下报错 xff1a 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 6
  • GO通过HTTP获取API的返回值(response)

    目录 net httpnet url net http span class token keyword import span span class token string 34 net http 34 span Go语言提供了HTTP
  • STM32F103学习笔记(2.3)——读GPIO 按键

    为了读取引脚的高低电平 xff0c 就需要将引脚配置成输入模式 xff0c 并读取IDR寄存器 目录 寄存器配置 端口配置低寄存器 GPIOx CRL x 61 A E 端口输入数据寄存器 GPIOx IDR x 61 A E 按键点灯 寄
  • Windows系统下,Ubuntu安装至移动硬盘(简单分析与详细安装教程)

    前期说明 博主因学业要求 xff0c 需要同时使用Windows系统与Linux系统 xff0c 故而考虑安装双系统 但个人电脑硬盘仅剩100G左右大小 xff0c 安装双系统可能导致硬盘容量不足 xff0c 恰好博主手中有个空闲的移动硬盘
  • QT开发学习4(远程调试 Qt 程序)

    2 5 1 rsync 方式 Qt 远程调试 在 Qt Creator 中默认情况下 xff0c 会使用 sftp 或 rsync 发送程序到板卡 由于正点原子 I MX6 U 出厂 Qt 文件系统 xff08 文件系统 V1 9 及之后的
  • 使用 python requests 模块发送 http 请求及接收响应

    内容概要 如何构建GET 与 POST request 请求消息对 request 的header query string message body 定制化http header参数 content type 的设置分析request r