Python学习计划——8.1HTTP协议与请求

HTTP(HyperText Transfer Protocol)是用于在客户端和服务器之间传输超文本的协议。理解HTTP协议是进行网络编程和使用API的基础。HTTP协议主要包括以下几种常见的请求方法:

  1. GET:请求指定的资源。常用于获取数据。
  2. POST:向指定资源提交数据进行处理请求。常用于提交表单或上传文件。
  3. PUT:请求服务器将客户端上传的内容存储在指定的资源位置。
  4. DELETE:请求服务器删除指定的资源。
1. 使用requests库发送HTTP请求

requests是一个用于发送HTTP请求的第三方库,简单易用。可以使用pip安装requests库:

pip install requests
示例1:发送GET请求
import requests

# 发送GET请求
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}")
示例2:发送POST请求
import requests

# 发送POST请求
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}")
示例3:发送PUT请求
import requests

# 发送PUT请求
data = {'id': 1, 'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data)
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}")
示例4:发送DELETE请求
import requests

# 发送DELETE请求
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.text}")
2. 使用查询参数和请求头

可以通过params参数在GET请求中添加查询参数,通过headers参数在请求中添加请求头。

示例:使用查询参数和请求头
import requests

# 发送GET请求,添加查询参数和请求头
params = {'userId': 1}
headers = {'User-Agent': 'my-app/0.0.1'}
response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params, headers=headers)
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}")
3. 处理响应

可以通过response对象访问响应的状态码、内容、头信息等。

示例:处理响应
import requests

# 发送GET请求
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')

# 处理响应
print(f"状态码: {response.status_code}")
print(f"响应头: {response.headers}")
print(f"响应内容: {response.json()}")
4. 可运行的Python案例

下面是一个完整的Python程序,演示了如何使用requests库发送各种HTTP请求、使用查询参数和请求头,以及处理响应。

import requests

# 1. 发送GET请求
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print("GET请求:")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}\n")

# 2. 发送POST请求
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)
print("POST请求:")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}\n")

# 3. 发送PUT请求
data = {'id': 1, 'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data)
print("PUT请求:")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}\n")

# 4. 发送DELETE请求
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print("DELETE请求:")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.text}\n")

# 5. 使用查询参数和请求头发送GET请求
params = {'userId': 1}
headers = {'User-Agent': 'my-app/0.0.1'}
response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params, headers=headers)
print("GET请求(带查询参数和请求头):")
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.json()}\n")

# 6. 处理响应
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print("处理响应:")
print(f"状态码: {response.status_code}")
print(f"响应头: {response.headers}")
print(f"响应内容: {response.json()}")

可以将上面的代码复制到你的IDE中运行,观察程序的输出。这个案例综合了HTTP请求的基本知识,帮助你理解和掌握这些操作。继续加油,学习Python会越来越有趣和有用!

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值