http文件上传服务器python,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

importhttplibimportmimetypesdefpost_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()returnh.file.read()defencode_multipart_formdata(fields,files):LIMIT='----------lImIt_of_THE_fIle_eW_$'CRLF='\r\n'L=[]for(key,value)infields:L.append('--'+LIMIT)L.append('Content-Disposition: form-data; name="%s"'%key)L.append('')L.append(value)for(key,filename,value)infiles: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'%BOUNDARYreturncontent_type,bodydefget_content_type(filename):returnmimetypes.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.pyfromposter.encodeimportmultipart_encodefromposter.streaminghttpimportregister_openersimporturllib2# Register the streaming http handlers with urllib2register_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 parametersdatagen,headers=multipart_encode({"image1":open("DSC0001.jpg")})# Create the Request objectrequest=urllib2.Request("http://localhost:5000/upload_image",datagen,headers)# Actually do the request, and get the responseprinturllib2.urlopen(request).read()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值