使用Python脚本通过POST方式发送文件

172
有没有一种方法可以使用Python脚本发送POST请求来传输文件?
10个回答

250

来源: https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file

使用Requests上传多部分编码的文件非常简单:

with open('report.xls', 'rb') as f:
    r = requests.post('http://httpbin.org/post', files={'report.xls': f})

就这样了。我不是在开玩笑——这只有一行代码。文件已经发送。让我们检查一下:

>>> r.text
{
  "origin": "179.13.100.4",
  "files": {
    "report.xls": "<censored...binary...data>"
  },
  "form": {},
  "url": "http://httpbin.org/post",
  "args": {},
  "headers": {
    "Content-Length": "3196",
    "Accept-Encoding": "identity, deflate, compress, gzip",
    "Accept": "*/*",
    "User-Agent": "python-requests/0.8.0",
    "Host": "httpbin.org:80",
    "Content-Type": "multipart/form-data; boundary=127.0.0.1.502.21746.1321131593.786.1"
  },
  "data": ""
}

2
我正在尝试同样的事情,如果文件大小小于约1.5 MB,则可以正常工作。否则会抛出错误...请查看此处:http://stackoverflow.com/questions/20217348/requests-post-files-upload-large-file-more-than-1-5-mb-python。 - Niks Jain
1
我所尝试做的是使用已成功完成的请求登录到某个网站,但现在我想在登录后上传视频,该表格具有不同的字段需要填写才能提交。那么我应该如何传递这些值,例如视频描述、视频标题等? - TaraGurung
20
你可能希望使用with open('report.xls', 'rb') as f: r = requests.post('http://httpbin.org/post', files={'report.xls': f}),这样在打开文件后会自动关闭。 - Hjulle
2
这个答案应该更新,包括Hjulle的建议使用上下文管理器来确保文件被关闭。 - bmoran
这对我不起作用,它显示“405方法不允许”。 使用以下代码打开文件: with open(file_path, 'rb') as f: 然后使用以下代码进行POST请求: response = requests.post(url=url, data=f, auth=HTTPBasicAuth(username=id, password=password)) - Saurabh Jain
显示剩余2条评论

31

是的。您可以使用urllib2模块,并使用multipart/form-data内容类型进行编码。以下是一些示例代码,可帮助您入门 - 它不仅适用于文件上传,但您应该能够阅读它并了解其工作方式:

user_agent = "image uploader"
default_message = "Image $current of $total"

import logging
import os
from os.path import abspath, isabs, isdir, isfile, join
import random
import string
import sys
import mimetypes
import urllib2
import httplib
import time
import re

def random_string (length):
    return ''.join (random.choice (string.letters) for ii in range (length + 1))

def encode_multipart_data (data, files):
    boundary = random_string (30)

    def get_content_type (filename):
        return mimetypes.guess_type (filename)[0] or 'application/octet-stream'

    def encode_field (field_name):
        return ('--' + boundary,
                'Content-Disposition: form-data; name="%s"' % field_name,
                '', str (data [field_name]))

    def encode_file (field_name):
        filename = files [field_name]
        return ('--' + boundary,
                'Content-Disposition: form-data; name="%s"; filename="%s"' % (field_name, filename),
                'Content-Type: %s' % get_content_type(filename),
                '', open (filename, 'rb').read ())

    lines = []
    for name in data:
        lines.extend (encode_field (name))
    for name in files:
        lines.extend (encode_file (name))
    lines.extend (('--%s--' % boundary, ''))
    body = '\r\n'.join (lines)

    headers = {'content-type': 'multipart/form-data; boundary=' + boundary,
               'content-length': str (len (body))}

    return body, headers

def send_post (url, data, files):
    req = urllib2.Request (url)
    connection = httplib.HTTPConnection (req.get_host ())
    connection.request ('POST', req.get_selector (),
                        *encode_multipart_data (data, files))
    response = connection.getresponse ()
    logging.debug ('response = %s', response.read ())
    logging.debug ('Code: %s %s', response.status, response.reason)

def make_upload_file (server, thread, delay = 15, message = None,
                      username = None, email = None, password = None):

    delay = max (int (delay or '0'), 15)

    def upload_file (path, current, total):
        assert isabs (path)
        assert isfile (path)

        logging.debug ('Uploading %r to %r', path, server)
        message_template = string.Template (message or default_message)

        data = {'MAX_FILE_SIZE': '3145728',
                'sub': '',
                'mode': 'regist',
                'com': message_template.safe_substitute (current = current, total = total),
                'resto': thread,
                'name': username or '',
                'email': email or '',
                'pwd': password or random_string (20),}
        files = {'upfile': path}

        send_post (server, data, files)

        logging.info ('Uploaded %r', path)
        rand_delay = random.randint (delay, delay + 5)
        logging.debug ('Sleeping for %.2f seconds------------------------------\n\n', rand_delay)
        time.sleep (rand_delay)

    return upload_file

def upload_directory (path, upload_file):
    assert isabs (path)
    assert isdir (path)

    matching_filenames = []
    file_matcher = re.compile (r'\.(?:jpe?g|gif|png)$', re.IGNORECASE)

    for dirpath, dirnames, filenames in os.walk (path):
        for name in filenames:
            file_path = join (dirpath, name)
            logging.debug ('Testing file_path %r', file_path)
            if file_matcher.search (file_path):
                matching_filenames.append (file_path)
            else:
                logging.info ('Ignoring non-image file %r', path)

    total_count = len (matching_filenames)
    for index, file_path in enumerate (matching_filenames):
        upload_file (file_path, index + 1, total_count)

def run_upload (options, paths):
    upload_file = make_upload_file (**options)

    for arg in paths:
        path = abspath (arg)
        if isdir (path):
            upload_directory (path, upload_file)
        elif isfile (path):
            upload_file (path)
        else:
            logging.error ('No such path: %r' % path)

    logging.info ('Done!')

1
在Windows上使用此代码时,我在Python 2.6.6中遇到了Multipart边界解析错误。我不得不像https://dev59.com/xHE85IYBdhLWcg3wViA4#2823331中讨论的那样从string.letters更改为string.ascii_letters才能使其正常工作。边界要求在此处讨论:https://dev59.com/4nVC5IYBdhLWcg3w51ry#147467。 - amit kumar
调用以下代码: run_upload({'server':'', 'thread':''}, paths=['/path/to/file.txt']) 会导致以下错误: 在这一行中,upload_file(path)需要3个参数 因此我将其替换为以下代码: upload_file(path, 1, 1) - tabdulradi

5

4
唯一阻止您直接在文件对象上使用urlopen的事情是,内置文件对象缺少len定义。一个简单的方法是创建一个子类,为urlopen提供正确的文件。 我还修改了下面文件中的Content-Type头。
import os
import urllib2
class EnhancedFile(file):
    def __init__(self, *args, **keyws):
        file.__init__(self, *args, **keyws)

    def __len__(self):
        return int(os.fstat(self.fileno())[6])

theFile = EnhancedFile('a.xml', 'r')
theUrl = "http://example.com/abcde"
theHeaders= {'Content-Type': 'text/xml'}

theRequest = urllib2.Request(theUrl, theFile, theHeaders)

response = urllib2.urlopen(theRequest)

theFile.close()


for line in response:
    print line

@robert 我在Python2.7中测试了你的代码,但它没有起作用。urlopen(Request(theUrl, theFile, ...))仅将文件内容编码为普通的post方式,但无法指定正确的表单字段。我甚至尝试了变体urlopen(theUrl, urlencode({'serverside_field_name': EnhancedFile('my_file.txt')})),它上传了一个文件,但(当然!)内容不正确,如<open file 'my_file.txt', mode 'r' at 0x00D6B718>。我错过了什么吗? - RayLuo
感谢您的回答。通过使用上述代码,我已经使用PUT请求将2.2 GB的原始图像文件传输到了Web服务器。 - Akshay Patil

2
我将翻译如下:

我正在尝试测试Django REST API,并且它对我有效:

def test_upload_file(self):
        filename = "/Users/Ranvijay/tests/test_price_matrix.csv"
        data = {'file': open(filename, 'rb')}
        client = APIClient()
        # client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
        response = client.post(reverse('price-matrix-csv'), data, format='multipart')

        print response
        self.assertEqual(response.status_code, status.HTTP_200_OK)

3
这段代码存在内存泄漏 - 你忘记了将一个文件关闭(close())。 - Chiefir

2

Chris Atlee的poster库非常适用于此(特别是方便的函数poster.encode.multipart_encode())。作为奖励,它支持流式传输大文件而无需将整个文件加载到内存中。另请参见Python问题3244


0
def visit_v2(device_code, camera_code):
    image1 = MultipartParam.from_file("files", "/home/yuzx/1.txt")
    image2 = MultipartParam.from_file("files", "/home/yuzx/2.txt")
    datagen, headers = multipart_encode([('device_code', device_code), ('position', 3), ('person_data', person_data), image1, image2])
    print "".join(datagen)
    if server_port == 80:
        port_str = ""
    else:
        port_str = ":%s" % (server_port,)
    url_str = "http://" + server_ip + port_str + "/adopen/device/visit_v2"
    headers['nothing'] = 'nothing'
    request = urllib2.Request(url_str, datagen, headers)
    try:
        response = urllib2.urlopen(request)
        resp = response.read()
        print "http_status =", response.code
        result = json.loads(resp)
        print resp
        return result
    except urllib2.HTTPError, e:
        print "http_status =", e.code
        print e.read()

0

我尝试了这里的一些选项,但是我的头部有一些问题('files'字段为空)。

使用requests进行POST并解决问题的简单模拟:

import requests

url = 'http://127.0.0.1:54321/upload'
file_to_send = '25893538.pdf'

files = {'file': (file_to_send,
                  open(file_to_send, 'rb'),
                  'application/pdf',
                  {'Expires': '0'})}

reply = requests.post(url=url, files=files)
print(reply.text)

更多内容请参见https://requests.readthedocs.io/en/latest/user/quickstart/

为了测试这段代码,您可以使用一个简单的虚拟服务器,例如这个(适用于GNU/Linux或类似系统):

import os
from flask import Flask, request, render_template

rx_file_listener = Flask(__name__)

files_store = "/tmp"
@rx_file_listener.route("/upload", methods=['POST'])
def upload_file():
    storage = os.path.join(files_store, "uploaded/")
    print(storage)
    
    if not os.path.isdir(storage):
        os.mkdir(storage)

    try:
        for file_rx in request.files.getlist("file"):
            name = file_rx.filename
            destination = "/".join([storage, name])
            file_rx.save(destination)
        
        return "200"
    except Exception:
        return "500"

if __name__ == "__main__":
    rx_file_listener.run(port=54321, debug=True)

0

你可能也想看看httplib2,以及examples。我发现使用httplib2比使用内置的HTTP模块更加简洁。


2
没有示例展示如何处理文件上传。 - dland
链接已过期,且没有内联示例。 - jlr
3
自此,它已经迁移到https://github.com/httplib2/httplib2。另一方面,现在我可能会推荐使用`requests`。 - pdc

0

pip install http_file

#импорт вспомогательных библиотек
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import requests
#импорт http_file
from http_file import download_file
#создание новой сессии
s = requests.Session()
#соеденение с сервером через созданную сессию
s.get('URL_MAIN', verify=False)
#загрузка файла в 'local_filename' из 'fileUrl' через созданную сессию
download_file('local_filename', 'fileUrl', s)

2
通常最好的做法是用英文编写注释。 - obotezat
糟糕的例子。原始问题是关于上传文件,而不是下载。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接