失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python3 内置http.client urllib.request及三方库requests发送请求对比

Python3 内置http.client urllib.request及三方库requests发送请求对比

时间:2020-07-26 09:00:21

相关推荐

Python3 内置http.client urllib.request及三方库requests发送请求对比

一、HTTP,GET请求,无参

GET /get

Python3 http.client

import http.client

# 1. 建立HTTP连接

conn = http.client.HTTPConnection("")

# 2. 发送GET请求,制定接口路径

conn.request("GET", '/get')

# 3. 获取相应

res = conn.getresponse()

# 4. 解析相应.进行解码

print(res.read().encode("utf-8")) # 自己解码

Python3 urllib.request

import urllib

res = urllib.request.urlopen("/get")

print(res.read().decode("utf-8")) # 自己解码

Python3 requests

import requests

res = requests.get("/get")

print(res.text) # 自动按默认utf-8解码

二、HTTPS,GET请求,带中文参数

GET /get?name=张三&age=12

Python3 http.client

import http.client

import urllib.parse

conn = http.client.HTTPSConnection("")

url = urllib.parse.quote("/get?name=张三&age=12", safe=':/?=&') # 进行url编码

conn.request("GET", url)

res = conn.getresponse()

print(res.read().decode("utf-8")) # 自己解码

Python3 urllib.request

import urllib

import urllib.parse

url = urllib.parse.quote("/get?name=张三&age=12", safe=':/?=&') # 进行url编码

res = urllib.request.urlopen("url")

print(res.read().decode("utf-8")) # 自己解码

Python3 requests

import requests

res = requests.get("/get?name=张三&age=12") # 自动编码

print(res.text) # 自动按默认utf-8解码

三、Post x-www-form-urlencoded传统表单请求

POST /post 请求数据: name=张三&age=12

Python3 http.client

import http.client

import urllib.parse

conn = http.client.HTTPConnection("")

data = urllib.parse.urlencode({"name":"张三", "age": 12}).encode("utf-8") # 对数据进行url编码及utf-8编码

conn.request("POST", '/post', data)

res = conn.getresponse()

print(res.read().decode("utf-8"))

Python3 urllib.request

import urllib

import urllib.parse

import urllib.request

data = urllib.parse.urlencode({"name":"张三", "age": 12}).encode("utf-8") # 对数据进行url编码及utf-8编码

req = urllib.request.Request("/post", data=data)

res = urllib.request.urlopen(req)

print(res.read().decode("utf-8"))

Python3 requests

import requests

data = {"name":"张三", "age": 12}

res = requests.post("/post", data=data) # 自动编码

print(res.text)

四、Post application/json请求

POST /post 请求数据: {"name": "张三","age": 12}

Python3 http.client

import http.client

import urllib.parse

import json

conn = http.client.HTTPConnection("")

data = '{"name":"张三", "age": 12}'.encode('utf-8') # 或data = json.dumps({"name":"张三", "age": 12})

headers = {"Content-Type": "application/json"}

conn.request("POST", '/post', data, headers)

res = conn.getresponse()

print(res.read().decode("utf-8"))

Python3 urllib.request

import urllib

import urllib.parse

import urllib.request

import json

data = '{"name":"张三", "age": 12}'.encode('utf-8') # 或data = json.dumps({"name":"张三", "age": 12})

headers = {"Content-Type": "application/json"}

req = urllib.request.Request("/post", data=data, headers=headers)

res = urllib.request.urlopen(req)

print(res.read().decode("utf-8"))

Python3 requests

import requests

data = {"name":"张三", "age": 12}

res = requests.post("/post", json=data)

print(res.json()) # 转为字典格式

import requests

import json

data = {"name":"张三", "age": 12}

headers = {"Content-Type": "application/json"}

res = requests.post("/post", data=json.dumps(data), headers=headers)

print(res.json()) # 转为字典格式

作者:韩志超

链接:/p/491d6590b2a0

如果觉得《Python3 内置http.client urllib.request及三方库requests发送请求对比》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。