python 文件上传http_python上传文件到HTTP服务器

[使用http往服务端上传文件,要使用MultipartEntity,需要引入httpmime包,在附件中。 public static String postFile(String uploadFile, final String uploadType, Strin

首先,标准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上传文件的几种方法:

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'

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)

# 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 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()

[# -*- coding:UTF-8 -*- ''' Created on 2010-4-26 @author: shiyong ''' import urllib import sys import urllib2 import mimetools, mimetypes import os,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值