5、 ... and 、 Upload files (Form Form method )
1, Single file upload
(1) The following is the simplest file upload code , After running, the logo.png This file is uploaded to the server :
import requests
files = {'file1': open('logo.png', 'rb')}
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)
(2) We can also explicitly set the file name , File types and request headers :
import requests
files = {'file1': ('hangge.png', open('logo.png', 'rb'), 'image/png', {'Expires': '0'})}
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)
(3) Server code ( upload.php)
<?
move_uploaded_file($_FILES["file1"]["tmp_name"],
$_SERVER["DOCUMENT_ROOT"]."/uploadFiles/" . $_FILES["file1"]["name"]);
?>
2, Multiple file upload
(1) Sometimes we need to send multiple files in one request at the same time , Also use files Parameters can be passed into an array :
import requests
files = [
('file1', ('1.png', open('logo.png', 'rb'), 'image/png')),
('file2', ('2.png', open('logo.png', 'rb'), 'image/png'))
]
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)
(2) Server code ( upload.php)
<?
move_uploaded_file($_FILES["file1"]["tmp_name"],
$_SERVER["DOCUMENT_ROOT"]."/uploadFiles/" . $_FILES["file1"]["name"]);
move_uploaded_file($_FILES["file2"]["tmp_name"],
$_SERVER["DOCUMENT_ROOT"]."/uploadFiles/" . $_FILES["file2"]["name"]);
?>
3, Upload with other parameters
(1) If we need to pass some other parameters while uploading the file , It's OK, too :
import requests
data = {
"name": "hangge.com",
"age": 100
}
files = [
('file1', ('1.png', open('logo.png', 'rb'), 'image/png')),
('file2', ('2.png', open('logo.png', 'rb'), 'image/png'))
]
response = requests.post('http://www.hangge.com/upload.php', data=data, files=files)
print(response.text)
(2) Server code ( upload.php)
<?
$value1 = $_POST["name"];
$value2 = $_POST["age"];
move_uploaded_file($_FILES["file1"]["tmp_name"],
$_SERVER["DOCUMENT_ROOT"]."/uploadFiles/" . $_FILES["file1"]["name"]);
move_uploaded_file($_FILES["file2"]["tmp_name"],
$_SERVER["DOCUMENT_ROOT"]."/uploadFiles/" . $_FILES["file2"]["name"]);
?>
attach : Streaming files
1,requests-toolbelt expanded memory bank
(1) Sometimes we need to upload a very large file ( such as 1G about ), If you use it directly in the way above Requests Submit , May cause memory shortage and crash .
(2) So when sending a large file, it is recommended to make the request into a data stream . But by default Requests Streaming upload is not supported , But there's a third party package requests-toolbelt Is to support the ( In essence multipart/form-data Upload ) (3) In the use of requests-toolbelt Before , We first pass pip Installation :
pip install requests-toolbelt
2, Use streaming to upload files
In the following example, we use requests-toolbelt To realize the streaming upload of files :
- differ requests Read all to memory and upload ,requests-toolbelt Yes, upload while reading .
- Its essence is multipart/form-data Submit data , So the server code does not need to be changed .
import requests
from requests_toolbelt import MultipartEncoder
m = MultipartEncoder(
fields={'name': 'hangge.com', "age": '100',
'file1': ('1.png', open('logo.png', 'rb'), 'image/png'),
'file2': ('2.png', open('logo.png', 'rb'), 'image/png')}
)
r = requests.post('http://www.hangge.com/upload.php', data=m,
headers={'Content-Type': m.content_type})
print(r.text)
3, Monitor upload progress
(1) requests-toolbelt There's also a monitor ( MultipartEncoderMonitor), The monitor accepts a callback function , We can track progress in real time in callbacks .
import requests
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
def my_callback(monitor):
progress = (monitor.bytes_read / monitor.len) * 100
print("\r File upload progress :%d%%(%d/%d)"
% (progress, monitor.bytes_read, monitor.len), end=" ")
e = MultipartEncoder(
fields={'name': 'hangge.com', "age": '100',
'file1': ('1.png', open('logo.png', 'rb'), 'image/png'),
'file2': ('2.png', open('logo.png', 'rb'), 'image/png')}
)
m = MultipartEncoderMonitor(e, my_callback)
r = requests.post('http://www.hangge.com/upload.php', data=m,
headers={'Content-Type': m.content_type})
print(r.text)
(2) The operation effect is as follows , You can see that the progress will be displayed in real time during the submission process :