def __call__(self, *args, **kwargs): self._attribute_stack += [str(a) for a in args] self._params = kwargs headers = self._headers_factory() if 'url' not in kwargs: url = self.get_url() else: url = self.get_url(kwargs['url']) if self._method == "POST" and 'type' not in kwargs: headers.update( {'content-type':'application/x-www-form-urlencoded'}) body = self._generate_body() elif('type' in kwargs): if kwargs['type'] == 'multipart/form-data': body, new_headers = multipart_encode(kwargs['body']) body = "".join(body) headers.update(new_headers) else: body = kwargs['body'] headers.update({'content-type': kwargs['type']}) else: body = self._generate_body() #hack response, data = self._http.request(url, self._method, body=body, headers=headers) self._attribute_stack = [] handler = kwargs.get('handler', _handle_response) return handler(response, data)
def __call__(self, *args, **kwargs): self._attribute_stack += [str(a) for a in args] self._params = kwargs headers = self._headers_factory() if 'url' not in kwargs: url = self.get_url() else: url = self.get_url(kwargs['url']) if (self._method == "POST" or self._method == "PUT") and 'type' not in kwargs: headers.update( {'content-type': 'application/x-www-form-urlencoded'}) body = self._generate_body() elif ('type' in kwargs): if kwargs['type'] == 'multipart/form-data': body, new_headers = multipart_encode(kwargs['body']) body = "".join(body) headers.update(new_headers) else: body = kwargs['body'] headers.update({'content-type': kwargs['type']}) else: body = self._generate_body() #hack response, data = self._http.request(url, self._method, body=body, headers=headers) self._attribute_stack = [] handler = kwargs.get('handler', _handle_response) return handler(response, data)
def __call__(self, *args, **kwargs): self._attribute_stack += [str(a) for a in args] self._params = kwargs headers = self._headers_factory() if 'url' not in kwargs: url = self.get_url() else: url = self.get_url(kwargs['url']) if (self._method == "POST" or self._method == "PUT") and 'type' not in kwargs: headers.update( {'content-type':'application/json'}) # Not sure if this will always work, but for validate/verfiy nothing else was working: body = json.dumps(kwargs) elif('type' in kwargs): if kwargs['type'] == 'multipart/form-data': body, new_headers = multipart_encode(kwargs['body']) body = "".join(body) headers.update(new_headers) else: body = kwargs['body'] headers.update({'content-type': kwargs['type']}) else: body = self._generate_body() #hack response, data = self._http.request(url, self._method, body=body, headers=headers) self._attribute_stack = [] handler = kwargs.get('handler', _handle_response) return handler(response, data)
def API_LoadLoggerData(LoggerFile, site_id, api, project, template_id): from encode import multipart_encode from streaminghttp import register_openers import urllib2, os, csv print LoggerFile a = open(LoggerFile, 'rb') logger = csv.reader(a) UpdateFile = 'Updater.csv' b = open(UpdateFile, 'rb') update = csv.reader(b) c = open('Upload_File.csv','wb') upload_data = csv.writer(c, delimiter=',',quoting=csv.QUOTE_NONE) data = list(logger) up = list(update) diff = len(data)-len(up) print "# New Data Rows=", diff if diff > 0: print "Creating Upload File" startline = 1 for row in data[-(diff):]: upload_data.writerow(row) a.close() b.close() c.close() upload_file = os.getcwd()+'\Upload_File.csv' print "File to Upload to VOEIS="+upload_file url='https://voeis.msu.montana.edu/projects/'+str(project)+'/apivs/upload_data.json?' # 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 ({'datafile': open('Upload_File.csv', 'rb'), 'data_template_id': template_id, 'site_id': site_id, 'start_line': startline, 'api_key': api }) # Create the Request object request = urllib2.Request(url, datagen, headers) # Actually do the request, and get the response print urllib2.urlopen(request).read()
def post(self): rq_cid = self.request.form.get('cid') rq_token = self.request.form.get('token') rq_image = self.request.files.get('image') if rq_token is None or rq_cid is None: return MissingParams() else: profile = Profile.all().filter('token', rq_token).get() if profile is None: return InvalidToken() elif not profile.game.is_on: return NoGameOn() else: point = CheckinPoint.all().filter('cid', int(rq_cid)).get() if point is None: return InvalidCid() else: ckin = Checkin.all().filter('profile', profile) if ckin.filter('achieved', False).count() == 0: return render_json_response({'status':{'code':13,'message':'You have no unachieved checkins.'}}) else: ckin = ckin.filter('point', point).get() if ckin is None: return render_json_response({'status':{'code':11,'message':'You are checked in at another location!'}}) else: rq_image.seek(0,2) img_content_length = rq_image.tell() rq_image.seek(0,0) if img_content_length == 0: return render_json_response({'status':{'code':95,'message':'You have not included a picture!'}}) else: params = [] params.append( MultipartParam( "file", filename=ckin.cid, value=rq_image.read() ) ) payloadgen, headers = multipart_encode(params) payload = str().join(payloadgen) url = blobstore.create_upload_url('/handle_upload') try: result = urlfetch.fetch( url=url, payload=payload, method=urlfetch.POST, headers=headers, deadline=10, follow_redirects=False ) return render_json_response({'status':{'code':0,'message':'Success'}}) except: return render_json_response({'status':{'code':81,'message':'There was a server error uploading the image.'}})
def create(self): print >> sys.stderr, 'making post request to: %s%s' % (self.dest,self.path) self.datagen = {} if len(self.audioFilename) > 0: if 'http' in self.audioFilename : self.path = self.path +"?media="+ urllib.quote(self.audioFilename) self.datagen = "" # should not be empty dict but empty string! else : if len(self.metadataFilename) > 0: self.datagen, headers_ = multipart_encode({ 'metadata' : create_metadata(self.metadataFilename), 'media' : open(self.audioFilename, "rb") }) else: self.datagen, headers_ = multipart_encode({ 'media' : open(self.audioFilename, "rb") }) self.headers.update(headers_) #print >> sys.stderr, "request headers: ", self.headers request = urllib2.Request(self.dest+self.path, data= self.datagen, headers=self.headers) BaseObject._execute(self, request)
def PhotosUpload(self, photo_full_path, status="", source="", location=""): '''Upload img file via python 3rd-party lib ,poster.''' register_openers() img=open(photo_full_path,"rb") jsonData={ 'photo': img, 'status': status, 'source': source, 'location': location, } data,headers=multipart_encode(jsonData) api='photos_upload' url=str(Api(api)) return self._getJson(self.parent.xauth.apiUploadPhoto(url,data,headers))
def get_url(self, url = "", data = None, headers = None): if not url: url = self.url if not headers: headers = {} rq = None if data: #data = urllib.urlencode(data) data, headerss = multipart_encode(data) headers.update(headerss) rq = urllib2.Request(url, data, headers) else: rq = urllib2.Request(url, headers = headers) r = urllib2.urlopen(rq).read() return r
def downloadAdditionalFiles(url, target_path): try: opener = register_openers() params = {} datagen, headers = multipart_encode(params) request = urllib2.Request(url, datagen, headers) data = urllib2.urlopen(request).read() dom = parseString(data) for design in dom.getElementsByTagName('entry'): url = str(design.getAttributeNode('url').nodeValue) if not os.path.exists(target_path): os.makedirs(target_path) metrixTools.downloadFile(url, target_path + url.split('file=')[-1]) except: pass
def downloadAdditionalFiles(url, target_path): try: opener = register_openers() params = {} datagen, headers = multipart_encode(params) request = urllib2.Request(url, datagen, headers) data = urllib2.urlopen(request).read() dom = parseString(data) for design in dom.getElementsByTagName("entry"): url = str(design.getAttributeNode("url").nodeValue) if not os.path.exists(target_path): os.makedirs(target_path) metrixTools.downloadFile(url, target_path + url.split("file=")[-1]) except: pass
def getWeb(url, login = False, parameters = {}): try: opener = register_openers() if login and config.plugins.MetrixConnect.auth_token.value != 'None' and config.plugins.MetrixConnect.auth_token.value != '': params = {'auth_id': config.plugins.MetrixConnect.auth_id.value, 'device_id': getDeviceID(), 'auth_token': config.plugins.MetrixConnect.auth_token.value} params.update(parameters) else: params = parameters datagen, headers = multipart_encode(params) request = urllib2.Request(url, datagen, headers) data = urllib2.urlopen(request).read() return data except Exception as e: print '[MetrixCore] Error getting web: ' + url return False
def put_file(self, root, to_path, file_obj): """ Retrieve or upload file contents relative to the user's Dropbox root or the application's sandbox directory within the user's Dropbox. * root is one of "dropbox" or "sandbox", most clients will use "sandbox". * to_path is the `directory` path to put the file (NOT the full path). * file_obj is an open and ready to read file object that will be uploaded. The filename is taken from the file_obj name currently, so you can't have the local file named differently than it's target name. This may change in future versions. Finally, this function is not terribly efficient due to Python's HTTPConnection requiring all of the file be read into ram for the POST. Future versions will avoid this problem. """ assert root in ["dropbox", "sandbox"] path = "/files/%s%s" % (root, to_path) params = { "file": file_obj.name, } url, headers, params = self.request(self.content_host, "POST", path, params, None) params['file'] = file_obj data, mp_headers = encode.multipart_encode(params) if 'Content-Length' in mp_headers: mp_headers['Content-Length'] = str(mp_headers['Content-Length']) headers.update(mp_headers) conn = httplib.HTTPConnection(self.content_host, self.port) conn.request("POST", url, "".join(data), headers) resp = rest.RESTResponse(conn.getresponse()) conn.close() file_obj.close() return resp
def send_screenshot(self, event): import time settings = get_settings() #t = time.strftime("%I%M%S") t = 'temp' print t time.sleep(1) screen = wx.ScreenDC() size = screen.GetSize() bmp = wx.EmptyBitmap(size[0], size[1]) mem = wx.MemoryDC(bmp) mem.Blit(0, 0, size[0], size[1], screen, 0, 0) del mem # Release bitmap bmp.SaveFile(t+'.png', wx.BITMAP_TYPE_PNG) opener = streaminghttp.register_openers() params = {'image': open(t+'.png','rb'), 'topic': settings['id']} datagen, headers = encode.multipart_encode(params) response = opener.open(urllib2.Request(settings['url2'], datagen, headers)) print 'Hello, world!'
def send_screenshot(self, event): import time settings = get_settings() #t = time.strftime("%I%M%S") t = 'temp' print t time.sleep(1) screen = wx.ScreenDC() size = screen.GetSize() bmp = wx.EmptyBitmap(size[0], size[1]) mem = wx.MemoryDC(bmp) mem.Blit(0, 0, size[0], size[1], screen, 0, 0) del mem # Release bitmap bmp.SaveFile(t + '.png', wx.BITMAP_TYPE_PNG) opener = streaminghttp.register_openers() params = {'image': open(t + '.png', 'rb'), 'lesson': settings['id']} datagen, headers = encode.multipart_encode(params) response = opener.open( urllib2.Request(settings['url2'], datagen, headers)) print 'Hello, world!'
def create(self): print >>sys.stderr, "making post request to: %s%s" % (self.dest, self.path) data = {} if self.service: data.update({"service": self.service, "item_id": self.item_id}) if "http" in self.audioFilename: data.update({"media": self.audioFilename}) else: data.update({"media": FileProgress(open(self.audioFilename, "rb"))}) if self.transcriptFilename: self.datagen.update({"transcript": read_file(self.transcriptFilename)}) headers = self.headers data, headers_ = multipart_encode(data) headers.update(headers_) request = urllib2.Request(self.dest + self.path, data=data, headers=headers) BaseObject._execute(self, request)
def put_file(self, root, to_path, file_obj): """ Retrieve or upload file contents relative to the user's Dropbox root or the application's sandbox directory within the user's Dropbox. * root is one of "dropbox" or "sandbox", most clients will use "sandbox". * to_path is the `directory` path to put the file (NOT the full path). * file_obj is an open and ready to read file object that will be uploaded. The filename is taken from the file_obj name currently, so you can't have the local file named differently than it's target name. This may change in future versions. Finally, this function is not terribly efficient due to Python's HTTPConnection requiring all of the file be read into ram for the POST. Future versions will avoid this problem. """ assert root in ["dropbox", "sandbox"] path = "/files/%s%s" % (root, to_path) params = { "file" : file_obj.name, } url, headers, params = self.request(self.content_host, "POST", path, params, None) params['file'] = file_obj data, mp_headers = encode.multipart_encode(params) if 'Content-Length' in mp_headers: mp_headers['Content-Length'] = str(mp_headers['Content-Length']) headers.update(mp_headers) conn = httplib.HTTPConnection(self.content_host, self.port) conn.request("POST", url, "".join(data), headers) resp = rest.RESTResponse(conn.getresponse()) conn.close() file_obj.close() return resp
def upload_music(music_file): register_openers() datagen, headers = multipart_encode({"files[]": open(music_file, "rb")}) headers[KEY_HOST] = HOST headers[KEY_CONNECTION] = CONNECTION headers[KEY_ACCEPT] = ACCEPT headers[KEY_USER_AGENT] = USER_AGENT headers[KEY_REFERER] = REFERER headers[KEY_ACCEPT_LANGUAGE] = ACCEPT_LANGUAGE headers[KEY_COOKIE] = COOKIE headers[KEY_ORIGIN] = ORIGIN headers[KEY_X_REQUESTED_WITH] = X_REQUESTED_WITH url = "https://chordify.net/song/file:" request = urllib2.Request(url, datagen, headers) print("POST: %s" % url) response = urllib2.urlopen(request).read() response_json = json.loads(response) print("response: %s" % response_json) if response_json.has_key("slug"): return (True, response_json["slug"]) else: return (False, "")
def rehost(url, force_cache=False, image=False, referer=''): """Take URL or file path, return download URL. If image=True, also try to retrieve direct link before rehosting. """ if re.match(DOWNLOAD_URL, url): return url # already there cl = cache_search(url) if cl is not None: return cl # already in cache if image: s = recover_image(url) ts = IMAGE_TYPES else: s = url ts = None if referer: op = uaopener() op.addheaders += [('Referer', referer)] install_opener(op) fd, ftype, finfo = open_thing(s, accept_types=ts) if fd is None: return url # failed to open or wrong type if finfo is not None: fname = ''.join(random.sample(string.lowercase, 6)) e = re.search(r'\.\w+$', finfo.url) if e is None: e = '' else: e = e.group() if image and ftype is not None: e = IMAGE_EXT[IMAGE_TYPES.index(ftype)] fname += e else: fname = fd.name pf = MultipartParam('file', filetype=ftype, fileobj=fd, filename=fname) if pf.get_size(gen_boundary()) > MAX_SIZE: print(ERR, 'Too big object:', s) return url datagen, headers = multipart_encode([pf]) req = urllib2.Request(UPLOAD_URL, datagen, headers) try: pd = streaming_opener().open(req, timeout=TIMEOUT) page = pd.read().decode(pd.info().getparam('charset')) except URLError as ex: print_urlerror(UPLOAD_URL, ex) return url g = re.search(FLAGS + DOWNLOAD_URL, page) if g: g = g.group(0) else: g = re.search(FLAGS + '<div id="error">(.*?)</div>', page) if g: g = re.sub('<[^>]+>', '', g.group(1)).strip() print(ERR, 'file.kirovnet.ru says:', g) else: print(ERR, 'Failed to get URL (layout changed?)') return url # falling back if force_cache or finfo is not None: cache_write(url, g) return g
#!/usr/bin/env python from encode import multipart_encode from streaminghttp import register_openers import urllib2,tkproductor,iteration,log register_openers() product_token = tkproductor.present_token upload_dic = {} upload_file = iteration.upload_file for upload_name in upload_file : upload_dic["key"] = upload_name upload_dic["token"] = product_token upload_dic["file"] = open(upload_name,"rb") log.writedown (upload_name) datagen, headers = multipart_encode(upload_dic) request = urllib2.Request("http://upload.qiniu.com/", datagen, headers) print urllib2.urlopen(request).read()
def API_LoadLoggerData(LoggerFile, site_id, api, project, template_id): from encode import multipart_encode from streaminghttp import register_openers import urllib2, os, csv class Test: def __init__(self): self.contents = '' def body_callback(self, buf): self.contents = self.contents + buf print LoggerFile a = open(LoggerFile, 'rb') logger = csv.reader(a) UpdateFile = 'Updater.csv' b = open(UpdateFile, 'rb') update = csv.reader(b) c = open('Upload_File.csv','wb') upload_data = csv.writer(c, delimiter=',',quoting=csv.QUOTE_NONE) data = list(logger) up = list(update) diff = len(data)-len(up) print "# New Data Rows=", diff if diff > 0: print "Creating Upload File" startline = 1 for row in data[-(diff):]: upload_data.writerow(row) a.close() b.close() c.close() upload_file = os.getcwd()+'\Upload_File.csv' print "File to Upload to VOEIS="+upload_file url='https://voeis.msu.montana.edu/projects/'+str(project)+'/apivs/upload_data.json?' print 'URL='+url params = ({'datafile': open('Upload_File.csv', 'rb'), 'data_template_id': template_id, 'site_id': site_id, 'start_line': startline, 'api_key': api }) # Register the streaming http handlers with urllib2 register_openers() # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters datagen, headers = multipart_encode(params) print headers # Create the Request object request = urllib2.Request(url, datagen, headers) # Actually do the request, and get the response result = urllib2.urlopen(request).read() print result return result
def make_request(method,accept,path,audioFilename, metadataFilename): datagen = {} headers = {} dest = 'http://www.koemei.com/REST/' #dest = 'http://www.koemei.com/REST/' #dest = 'http://localhost:8080/REST/' print >> sys.stderr, 'making %s request to: %s%s' % (method,dest,path) import base64 auth_string = base64.encodestring('%s:%s' % ('myemail address', 'changeme'))[:-1] headers.update( { 'authorization': 'basic %s' % auth_string, 'accept' : accept,}) if method == 'GET': return urllib2.Request(dest+path, headers=headers) if method == 'HEAD': request = urllib2.Request(dest+path, headers=headers) request.get_method = lambda : 'HEAD' return request if method == 'DELETE': request = urllib2.Request(dest+path, headers=headers) request.get_method = lambda: 'DELETE' return request if method == 'POST': if path == 'media' : datagen = {} if len(audioFilename) > 0: if 'http' in audioFilename : path = path +"?media="+ urllib.quote(audioFilename) datagen = "" # should not be empty dict but empty string! else : if len(metadataFilename) > 0: datagen, headers_ = multipart_encode({ 'metadata' : create_metadata(metadataFilename), 'media' : open(audioFilename, "rb") }) else: datagen, headers_ = multipart_encode({ 'media' : open(audioFilename, "rb") }) headers.update(headers_) print >> sys.stderr, "request headers: ", headers request = urllib2.Request(dest+path, data= datagen, headers=headers) return request elif path == 'kobjects' : datagen = {} request = urllib2.Request(dest+path, data="", headers=headers) return request elif 'transcribe' in path : # transcription request datagen = {} request = urllib2.Request(dest+path, data="", headers=headers) return request elif 'kobjects' in path and 'media' in path : # upload a media to an existing kobject datagen = {} if len(audioFilename) > 0: if len(metadataFilename) > 0: datagen, headers_ = multipart_encode({ 'metadata' : create_metadata(metadataFilename), 'media' : open(audioFilename, "rb") }) else: datagen, headers_ = multipart_encode({ 'media' : open(audioFilename, "rb") }) headers.update(headers_) print >> sys.stderr, "request headers: ", headers request = urllib2.Request(dest+path, data= datagen, headers=headers) return request