requests上传下载
1.下载
# 下载
import requests
url = 'http://img3.imgtn.bdimg.com/it/u=3381573685,1866477444&fm=26&gp=0.jpg'
res = requests.get(url, stream=True) # stream 为True表示流式请求
with open('logo.jpg', 'wb') as f:
for i in res.iter_content(chunk_size=1024): # iter_content边下载边存入,最大字节 1024
f.write(i)
2.上传
# 上传
import requests
files = {'file1': open('logo.png', 'rb')}
response = requests.post('http://www.ftp.com', files=files) # ftp服务地址
print(response.text)
'''
1.如果同时需要传data,那就把参数data加上
2.如果同时上传多个文件,就把files改成列表套元组
files = [
('file1',('1.png',open('logo.png', 'rb'), 'image/png')),
('file2',('2.jpg',open('logo.jpg', 'rb'), 'image/jpg')),
]
'''