def upload(self, photos, **params):#{{{ params['format'] = self.format params['api_key'] = self.api_key if self.token: params['auth_token'] = self.token params['api_sig'] =self.sign(**params) req_url = self.api_host + '/api/upload/' body = Multipart() for arg, value in params.iteritems(): part = Part({'name': arg}, value) body.attach(part) if type(photos) != types.ListType: photos = [photos] for photo in photos: filepart = Part({'name': 'photo', 'filename':params['title'].encode('utf8')}, photo['data'], 'image/'+photo['type']) body.attach(filepart) request = urllib2.Request(url=req_url) request.add_data(str(body)) (header, value) = body.header() request.add_header(header, value) #request.add_header('content-length', len(str(body))) return json.loads(urllib2.urlopen(request).read())#}}}
url = 'http://acm.hdu.edu.cn/diy/diy_uploaddata.php?action=upload&cid=' + cid + '&pid=' + pid body = Multipart() intext = open(infile, "rb").read() outtext = open(outfile, "rb").read() inpart = Part({"name":"inputfile", "filename":infile}, intext, "text/plain") outpart = Part({"name":"outputfile", "filename":outfile}, outtext, "text/plain") body.attach(inpart) body.attach(outpart) request = Request(url) request.add_data(str(body)) (header, value) = body.header() request.add_header(header, value) cookiejar = CookieJar() cookieproc = HTTPCookieProcessor(cookiejar) urlOpener = build_opener(cookieproc) # login loginurl = "http://acm.hdu.edu.cn/userloginex.php?action=login&cid=0¬ice=0" loginval = {"username":"******", "userpass":"******", "login":"******"} urlOpener.open(Request(loginurl, urlencode(loginval))) #print >>open("uploaddata.multipart", "w"), str(body) urlOpener.open(request) raw_input("I have finished my work!")