filetype.guess(文件路径).mime 返回结果是一个文件类型
import filetype
import os
filetype_= filetype.guess("美女.png").mime
print(filetype_)
# 结果 image/png
import requests
from requests_toolbelt import MultipartEncoder
# 如何请求文件上传的接口
url = "http://127.0.0.1:8080/upload"
file_path = "美女.png"
# 请求参数
data = MultipartEncoder(
fields={
"pic": (os.path.basename(file_path), open(file_path, "rb"), filetype.guess(file_path))}
)
# 构造请求头
headers={'Content-Type': data.content_type}
# 发送请求
response = requests.post(url=url,data=data,headers=headers)
# 打印结果
print(response.json())
urlencode编码
- 对字典和字符串进行编码
- 对 响应结果进行解码
对字典进行编码
# 对字典进行编码
from urllib.parse import urlencode,quote,unquote
b={
"content":"夏天",
"温度":"高"
}
print(urlencode(b))
结果
content=%E5%A4%8F%E5%A4%A9&%E6%B8%A9%E5%BA%A6=%E9%AB%98
对字符串进行编码 quote
# 对字符串惊醒编码 使用quote
url = "http://www.example.com/?id=767&mout=陪绑"
print(quote(url)) %91
http%3A//www.example.com/%3Fid%3D767%26mout%3D%E9%99%AA%E7%BB
响应结果进行解码 unquote
url = "http://www.example.com/?id=767&mout=陪绑"
print(quote(url)) # http%3A//www.example.com/%3Fid%3D767%26mout%3D%E9%99%AA%E7%BB%91
# 解码 unquote
import requests
from urllib.parse import urlencode,quote,unquote
url="http://www.example.com/"
p={
"season":"夏天",
"温度":"37"
}
r=requests.get(url,params=p)
print(r.url)
print(unquote(r.url))
"""
http://www.example.com/?season=%E5%A4%8F%E5%A4%A9&%E6%B8%A9%E5%BA%A6=37
http://www.example.com/?season=夏天&温度=37
"""
网友评论