日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2025/5/22 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python3 内置http.client,urllib.request及三方库requests发送请求对比 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、HTTP,GET請(qǐng)求,無(wú)參

GET http://httpbin.org/get

Python3 http.client

import http.client

# 1. 建立HTTP連接
conn = http.client.HTTPConnection("httpbin.org")
# 2. 發(fā)送GET請(qǐng)求,制定接口路徑
conn.request("GET", '/get')
# 3. 獲取相應(yīng)
res = conn.getresponse()
# 4. 解析相應(yīng).進(jìn)行解碼
print(res.read().encode("utf-8")) # 自己解碼

Python3 urllib.request

import urllib

res = urllib.request.urlopen("http://httpbin.org/get")
print(res.read().decode("utf-8"))? # 自己解碼

Python3 requests

import requests

res = requests.get("http://httpbin.org/get")
print(res.text) # 自動(dòng)按默認(rèn)utf-8解碼

二、HTTPS,GET請(qǐng)求,帶中文參數(shù)

GET http://httpbin.org/get?name=張三&age=12

Python3 http.client

import http.client
import urllib.parse

conn = http.client.HTTPSConnection("httpbin.org")
url = urllib.parse.quote("/get?name=張三&age=12", safe=':/?=&') # 進(jìn)行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("https://httpbin.org/get?name=張三&age=12", safe=':/?=&') # 進(jìn)行url編碼
res = urllib.request.urlopen("url")
print(res.read().decode("utf-8"))? # 自己解碼

Python3 requests

import requests

res = requests.get("https://httpbin.org/get?name=張三&age=12") # 自動(dòng)編碼
print(res.text) # 自動(dòng)按默認(rèn)utf-8解碼

三、Post x-www-form-urlencoded傳統(tǒng)表單請(qǐng)求

POST http://httpbin.org/post 請(qǐng)求數(shù)據(jù): name=張三&age=12

Python3 http.client


import http.client
import urllib.parse

conn = http.client.HTTPConnection("httpbin.org")
data = urllib.parse.urlencode({"name":"張三", "age": 12}).encode("utf-8") # 對(duì)數(shù)據(jù)進(jìn)行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") # 對(duì)數(shù)據(jù)進(jìn)行url編碼及utf-8編碼
req = urllib.request.Request("http://httpbin.org/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("http://httpbin.org/post", data=data) # 自動(dòng)編碼
print(res.text)

四、Post application/json請(qǐng)求

POST http://httpbin.org/post 請(qǐng)求數(shù)據(jù): {"name": "張三","age": 12}

Python3 http.client

import http.client
import urllib.parse
import json

conn = http.client.HTTPConnection("httpbin.org")
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("http://httpbin.org/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("http://httpbin.org/post", json=data)
print(res.json())? # 轉(zhuǎn)為字典格式


import requests
import json

data = {"name":"張三", "age": 12}
headers = {"Content-Type": "application/json"}
res = requests.post("http://httpbin.org/post", data=json.dumps(data), headers=headers)
print(res.json())? # 轉(zhuǎn)為字典格式

作者:韓志超
鏈接:https://www.jianshu.com/p/491d6590b2a0

總結(jié)

以上是生活随笔為你收集整理的Python3 内置http.client,urllib.request及三方库requests发送请求对比的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。