使用POST从Python脚本发送文件

使用POST从Python脚本发送文件

Send file using POST from a Python script

有没有一种方法可以使用Python脚本中的POST发送文件?


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

通过请求,上传Multipart编码的文件非常简单:

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

而已。我不是在开玩笑-这是一行代码。文件已发送。让我们检查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> 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":""
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 = '\
\
'
.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------------------------------\
\
'
, 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!')

看起来python请求无法处理非常大的多部分文件。

该文档建议您查看requests-toolbelt

这是他们文档中的相关页面。


阻止您直接在文件对象上直接使用urlopen的唯一原因是内置文件对象缺少len定义。一种简单的方法是创建一个子类,该子类为urlopen提供正确的文件。
我还修改了下面文件中的Content-Type标头。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

我正在尝试测试Django Rest API及其对我的工作:

1
2
3
4
5
6
7
8
9
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)

克里斯·阿特里(Chris Atlee)的海报库在此方面非常有效(尤其是便捷功能poster.encode.multipart_encode())。另外,它支持流式传输大文件,而无需将整个文件加载到内存中。另请参阅Python问题3244。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()

您可能还想看一下带有示例的httplib2。我发现使用httplib2比使用内置HTTP模块更为简洁。


推荐阅读

    linux文件异或命令?

    linux文件异或命令?,数字,系统,工作,管理,命令,数据,网络,文件,第一,单位,基

    linux脚本命令教学?

    linux脚本命令教学?,标准,数据,系统,脚本,代码,流程,官网,底部,命令,变量,lin

    linux文件复制的命令?

    linux文件复制的命令?,系统,文件,命令,目录,源文件,基本知识,位置,目标,选

    linux复制命令文件?

    linux复制命令文件?,系统,文件,命令,目录,基本知识,源文件,目标,文件夹,路

    linux下文件均分命令?

    linux下文件均分命令?,管理,情况,系统,工作,信息,地址,命令,目录,单位,设备,L

    linux的文件替换命令?

    linux的文件替换命令?,系统,工作,实时,命令,文件,批量,字符串,内容,方法,表

    脚本linux上运行命令?

    脚本linux上运行命令?,工具,代码,时间,密码,系统,环境,名字,位置,第三,下来,t

    linux查文件数量命令?

    linux查文件数量命令?,系统,数据,电脑,命令,文件,信息,代码,对比,软件,第三,l

    linux修改脚本的命令?

    linux修改脚本的命令?,系统,密码,服务,工作,工具,环境,信息,百度,代码,脚本,

    linux命令去重文件?

    linux命令去重文件?,系统,工作,命令,信息,数据,环境,代码,文件,目录,操作,Lin

    linux脚本命令单引号?

    linux脚本命令单引号?,系统,工作,美元,地址,命令,信息,情况,标准,管理,引号,l

    linux匹配文件名命令?

    linux匹配文件名命令?,系统,时间,发行,位置,工具,软件,名称,盘后,电脑,盘中,l

    执行linux脚本命令行?

    执行linux脚本命令行?,工具,位置,地方,环境,数据,状态,暂停,增长,系统,基础,

    linux上编辑文件命令?

    linux上编辑文件命令?,系统,信息,工作,状态,命令,文件,标准,检测,工具,设备,L

    linux转发请求命令?

    linux转发请求命令?,系统,工作,密码,网络,服务,信息,工具,项目,状态,标准,关

    改文件名linux命令?

    改文件名linux命令?,名字,软件,文件,命令,位置,系统,文件名,目录,指令,方面,l

    linux运行脚本的命令?

    linux运行脚本的命令?,系统,工具,代码,服务,脚本,状态,密码,环境,位置,暂停,l

    linux命令文件加锁?

    linux命令文件加锁?,数据,密码,系统,设备,代码,地址,名单,信息,数字,统一,请

    linux拼接文件命令?

    linux拼接文件命令?,文件,数据,命令,代码,时间,信息,系统,情况,管理,标准,Lin