Hướng dẫn upload zip file python requests - tải lên tệp zip yêu cầu python

Theo như

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 có liên quan, không có sự khác biệt giữa tệp zip và bất kỳ đốm dữ liệu nhị phân nào khác.no difference between a zip file and any other binary blob of data.

Máy chủ của bạn bị hỏng ở đây; Nó đang cắt kết nối khi bạn gửi tệp zip. Đó không phải là điều

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 có thể làm bất cứ điều gì về.

Bạn có thể muốn kiểm tra chống lại

import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

1 khi bạn gặp phải những vấn đề như thế này; Đây là một dịch vụ thử nghiệm được xây dựng bởi tác giả của thư viện
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9.

Một mẹo khác: Bạn không cần phải đọc toàn bộ đối tượng tệp vào bộ nhớ khi gửi. Thay vào đó, chỉ cần chuyển đối tượng sang

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9:

fileobj = open('/Users/.../test.zip', 'rb')
r = requests.post(url, auth=HTTPDigestAuth('dev', 'dev'), data = {"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})

Bản demo chống lại

import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

4:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}

Hello,

Tôi hiện đang viết một kịch bản Python để đẩy một số môi trường vào một số môi trường cùng một lúc. Tôi không làm cho nó hoạt động. Tôi không nhận được nó để tải lên tệp zip, vì repsonse là 400 - yêu cầu xấu. Hy vọng ai đó ở đây với một số trải nghiệm Python biết điều gì là sai 🙂 khối mã không hoạt động theo sau:🙂 Block of code not working follows:

import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

Tệp zip nằm trong cùng một dự án với tệp Python. & NBSP; & nbsp;

Giới thiệu

Python được hỗ trợ bởi nhiều thư viện giúp đơn giản hóa việc truyền dữ liệu qua HTTP. Thư viện

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 là một trong những gói Python phổ biến nhất vì nó được sử dụng rất nhiều trong việc quét web. Nó cũng phổ biến để tương tác với máy chủ! Thư viện giúp dễ dàng tải lên dữ liệu theo định dạng phổ biến như JSON, nhưng cũng giúp bạn dễ dàng tải lên các tệp.

Trong hướng dẫn này, chúng tôi sẽ xem cách tải lên các tệp bằng thư viện

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 của Python. Bài viết sẽ bắt đầu bằng cách bao gồm thư viện
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 và chữ ký chức năng
import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

8. Tiếp theo, chúng tôi sẽ bao gồm cách tải lên một tệp bằng cách sử dụng gói
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9. Cuối cùng nhưng không kém phần quan trọng, chúng tôi tải lên nhiều tệp trong một yêu cầu.

Tải lên một tệp duy nhất với thư viện yêu cầu của Python

Hướng dẫn này bao gồm cách gửi các tệp, chúng tôi không quan tâm đến cách chúng được tạo. Để theo dõi, tạo ba tệp được gọi là

$ python3 -m venv .
0,
$ python3 -m venv .
1 và
$ python3 -m venv .
2.

Điều đầu tiên chúng tôi cần làm là cài đặt thư viện

$ python3 -m venv .
3 của chúng tôi trong không gian làm việc của chúng tôi. Mặc dù không cần thiết, bạn nên cài đặt thư viện trong môi trường ảo:

$ python3 -m venv .

Kích hoạt môi trường ảo để chúng ta không còn tác động đến việc cài đặt Python toàn cầu:

$ . bin/activate

Bây giờ chúng ta hãy cài đặt thư viện

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 với
$ python3 -m venv .
5:

$ pip install requests

Tạo một tệp mới có tên

$ python3 -m venv .
6 sẽ lưu trữ mã của chúng tôi. Trong tệp đó, hãy bắt đầu bằng cách nhập thư viện
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9:

import requests

Bây giờ chúng tôi đã thiết lập để tải lên một tập tin! Khi tải lên một tệp, chúng ta cần mở tệp và truyền phát nội dung. Rốt cuộc, chúng tôi không thể tải lên một tệp mà chúng tôi không có quyền truy cập. Chúng tôi sẽ làm điều này với chức năng

$ python3 -m venv .
8.

Hàm

$ python3 -m venv .
8 chấp nhận hai tham số: đường dẫn của tệp và chế độ. Đường dẫn của tệp có thể là một đường dẫn tuyệt đối hoặc đường dẫn tương đối đến nơi tập lệnh đang được chạy. Nếu bạn đang tải lên một tệp trong cùng một thư mục, bạn chỉ có thể sử dụng tên của tệp.

Đối số thứ hai, chế độ, sẽ lấy giá trị "đọc nhị phân" được biểu thị bằng

$ . bin/activate
0. Đối số này nói với máy tính rằng chúng tôi muốn mở tệp ở chế độ đọc và chúng tôi muốn tiêu thụ dữ liệu của tệp ở định dạng nhị phân:

test_file = open("my_file.txt", "rb")

Lưu ý: Điều quan trọng là phải đọc tệp ở chế độ nhị phân. Thư viện

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 thường xác định tiêu đề
$ . bin/activate
2, là giá trị trong byte. Nếu tệp không được đọc trong chế độ byte, thư viện có thể nhận được giá trị không chính xác cho
$ . bin/activate
2, điều này sẽ gây ra lỗi trong quá trình gửi tệp.
: it's important to read the file in binary mode. The
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 library typically determines the
$ . bin/activate
2 header, which is a value in bytes. If the file is not read in bytes mode, the library may get an incorrect value for
$ . bin/activate
2, which would cause errors during file submission.

Đối với hướng dẫn này, chúng tôi sẽ thực hiện các yêu cầu cho dịch vụ HTTPBIN miễn phí. API này cho phép các nhà phát triển kiểm tra các yêu cầu HTTP của họ. Hãy tạo một biến lưu trữ URL, chúng tôi sẽ đăng các tệp của chúng tôi lên:

test_url = "http://httpbin.org/post"

Bây giờ chúng tôi có mọi thứ để đưa ra yêu cầu. Chúng tôi sẽ sử dụng phương thức

import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

8 của thư viện
>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9 để tải lên tệp. Chúng tôi cần hai đối số để thực hiện công việc này: URL của thuộc tính máy chủ và
$ . bin/activate
6. Chúng tôi cũng sẽ lưu phản hồi trong một biến, viết mã sau:

test_response = requests.post(test_url, files = {"form_field_name": test_file})

Tài sản

$ . bin/activate
6 lấy một từ điển. Khóa là tên của trường biểu mẫu chấp nhận tệp. Giá trị là byte của tệp đã mở mà bạn muốn tải lên.

Thông thường để kiểm tra xem phương thức

import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

8 của bạn có thành công không, chúng tôi kiểm tra mã trạng thái HTTP của phản hồi. Chúng ta có thể sử dụng thuộc tính
$ . bin/activate
9 của đối tượng phản hồi,
$ pip install requests
0. Nếu đó là sự thật, chúng tôi sẽ in ra phản hồi từ máy chủ HTTP, trong trường hợp này, nó sẽ lặp lại yêu cầu:

Kiểm tra hướng dẫn thực hành của chúng tôi, thực tế để học Git, với các thực hành tốt nhất, các tiêu chuẩn được công nghiệp chấp nhận và bao gồm bảng gian lận. Ngừng các lệnh git googling và thực sự tìm hiểu nó!

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
0

Hãy thử nó! Trong thiết bị đầu cuối, thực thi tập lệnh của bạn bằng lệnh

$ pip install requests
1:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
1

Đầu ra của bạn sẽ tương tự như thế này:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
2

Khi kiểm tra Sanity, bạn có thể xác minh giá trị

$ pip install requests
2 khớp với những gì trong tệp của bạn.

Tải lên nhiều tệp với thư viện yêu cầu của Python

Tải lên nhiều tệp bằng cách sử dụng các yêu cầu khá giống với một tệp duy nhất, với sự khác biệt chính là việc chúng tôi sử dụng danh sách. Tạo một tệp mới có tên

$ pip install requests
3 và mã thiết lập sau:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
3

Bây giờ, hãy tạo một biến có tên là

$ pip install requests
4 là từ điển có nhiều tên và tệp:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
4

Giống như trước đây, các khóa là tên của các trường biểu mẫu và các giá trị là các tệp trong byte.

Chúng tôi cũng có thể tạo các biến tệp của chúng tôi như một danh sách các bộ dữ liệu. Mỗi tuple chứa tên của trường biểu mẫu Chấp nhận tệp, theo sau là nội dung của tệp trong byte:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
5

Hoặc là hoạt động nên chọn bất cứ thứ gì bạn thích!

Khi danh sách các tệp đã sẵn sàng, bạn có thể gửi yêu cầu và kiểm tra phản hồi của nó như trước:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
6

Thực hiện tập lệnh này bằng lệnh

$ pip install requests
1:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
7

Bạn sẽ thấy đầu ra này:

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
8

Làm tốt lắm! Bạn có thể tải lên một và nhiều tệp với

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9!

Sự kết luận

Trong bài viết này, chúng tôi đã học cách tải lên các tệp trong Python bằng thư viện

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r

>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 CPython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
9. Trong đó đó là một tệp hoặc nhiều tệp, chỉ cần một vài điều chỉnh với phương thức
import requests
import json

# ...
# Other blocks of code here that works fine
# ...

for

tenant_id, token in zip(tenant_id_list, token_list):
url = "https://dtmanaged.com/e/" + tenant_id + "/api/config/v1/extensions?overrideAlerts=false" payload = {'file': '@custom.python.ssl_cert_check.zip;type=application/x-zip-compressed'}

files = {'file': open('custom.python.ssl_cert_check.zip', 'rb')}

headers = {
'accept': 'application/json; charset=utf-8',
'Authorization': 'Api-Token ' + token,
'Content-Type': 'multipart/form-data'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
print(response.status_code)

8. Chúng tôi cũng đã xác minh phản hồi của chúng tôi để đảm bảo rằng tải lên của chúng tôi đã thành công.