python上传文件到HTTP服务器

首先,标准HTTP协议对上传文件等表单的定义在这里:  http://www.ietf.org/rfc/rfc1867.txt  大概数据包格式如下:

单文件:

         Content-type: multipart/form-data, boundary=AaB03x

        --AaB03x
        content-disposition: form-data; name="field1"

        Joe Blow
        --AaB03x
        content-disposition: form-data; name="pics"; filename="file1.txt"
        Content-Type: text/plain

         ... contents of file1.txt ...
        --AaB03x--

多文件:

Content-type: multipart/form-data, boundary=AaB03x

        --AaB03x
        content-disposition: form-data; name="field1"

        Joe Blow
        --AaB03x
        content-disposition: form-data; name="pics"
        Content-type: multipart/mixed, boundary=BbC04y

        --BbC04y
        Content-disposition: attachment; filename="file1.txt"

其次,python上传文件的几种方法:

1 自己封装HTTP的POST数据包:http://stackoverflow.com/questions/680305/using-multipartposthandler-to-post-form-data-with-python

import httplibimport mimetypesdef post_multipart(host, selector, fields, files):     content_type, body = encode_multipart_formdata(fields, files)     h = httplib.HTTP(host)     h.putrequest('POST', selector)     h.putheader('content-type', content_type)     h.putheader('content-length', str(len(body)))     h.endheaders()     h.send(body)     errcode, errmsg, headers = h.getreply()     return h.file.read() def encode_multipart_formdata(fields, files):     LIMIT = '----------lImIt_of_THE_fIle_eW_$'     CRLF = '\r\n'     L = []     for (key, value) in fields:         L.append('--' + LIMIT)         L.append('Content-Disposition: form-data; name="%s"' % key)         L.append('')         L.append(value)     for (key, filename, value) in files:         L.append('--' + LIMIT)         L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))         L.append('Content-Type: %s' % get_content_type(filename))         L.append('')         L.append(value)     L.append('--' + LIMIT + '--')     L.append('')     body = CRLF.join(L)     content_type = 'multipart/form-data; boundary=%s' % BOUNDARY     return content_type, bodydef get_content_type(filename):     return mimetypes.guess_type(filename)[] or 'application/octet-stream'

2 使用现有模块 MultipartPostHandler  http://pypi.python.org/pypi/MultipartPostHandler/

import MultipartPostHandler, urllib2, cookielib

  cookies = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
                                MultipartPostHandler.MultipartPostHandler)
  params = { "username" : "bob", "password" : "riviera",
             "file" : open("filename", "rb") }
  opener.open("http://wwww.bobsite.com/upload/", params)

3 使用现有模块 poster http://stackoverflow.com/questions/680305/using-multipartposthandler-to-post-form-data-with-python

# test_client.py from poster.encode import multipart_encodefrom poster.streaminghttp import register_openersimport urllib2# Register the streaming http handlers with urllib2 register_openers() # Start the multipart/form-data encoding of the file "DSC0001.jpg" # "image1" is the name of the parameter, which is normally set # via the "name" parameter of the HTML <input> tag. # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters datagen, headers = multipart_encode({"image1": open("DSC0001.jpg")}) # Create the Request object request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers) # Actually do the request, and get the response print urllib2.urlopen(request).read()

 

 

 

 

 

你可能感兴趣的:(http,python,上传文件)