Python使用Http上传文件

import sys, MultipartPostHandler, cookielib, urllib2;

print "hello world";
#import sys
reload(sys)
sys.setdefaultencoding('gbk')
image = r'C:\a.jpg'
#open(image, 'rb')
params = {
          'fmd5':'FD6D523131FC92CE975BA0A042C8FED7',
          'path':'abc/ab',
          'radar':'leida1',
          'explainn':'explain',
          'data':open(image, 'rb'),
          'site':'site'
          }
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler.MultipartPostHandler)
res = opener.open("http://192.168.107.1:8087/test/upload", params);

print res.read()

貌似MultipartPostHandler有bug, 如果不设置gbk编码就会报错

你可能感兴趣的:(python)