def get(self, format): # allow cross-origin requests self.response.headers["Access-Control-Allow-Origin"] = "*" self.response.headers["Access-Control-Allow-Methods"] = "GET" if format == "json": # set the type to JSON self.response.headers["Content-Type"] = "application/json" # Get the parameters, filling in default values where needed query = self.request.get("q") if not query: query = "" limit = parseInt(self.request.get("limit"), DEFAULT_QUERY_LIMIT) offset = parseInt(self.request.get("offset"), DEFAULT_QUERY_OFFSET) sortBy = self.request.get("sortby") if not sortBy: sortBy = SORT_BEST_MATCH # Get the search results extList = searchFor(query, limit, offset, sortBy) # Turn the list of Extensions into a list of dictionaries extDictList = createExtDictList(extList, self.request.host_url) # Convert the dictionaries to JSON and print it self.response.out.write(json.dumps(extDictList)) else: error404(self.response)
def parseContainer(data): """Parses an spk file Returns: ('encrypted key', 'encrypted apk data') """ if data[:4] != constants.header: raise offset = util.parseInt(data[4:8]) keyLen = util.parseInt(data[offset + 8:offset + 12]) return data[offset + 12:offset + 12 + keyLen], data[offset + 12 + keyLen:]
def parseContainer(data): """Parses an spk file Returns: ('encrypted key', 'encrypted apk data') """ if data[:4] != constants.header: raise offset = util.parseInt(data[4:8]) keyLen = util.parseInt(data[offset+8:offset+12]) return data[offset+12:offset+12+keyLen], data[offset+12+keyLen:]
def get(self,extID,number): ext = Extension.gql('WHERE extID = :1',extID).get() number = parseInt(number,-1) if number != -1 and ext and ext.screenshots and number < len(ext.screenshots): self.response.headers['Content-Type'] = 'image/png' self.response.out.write(ext.screenshots[number]) else: self.error(404)
def __createMember(self, memProp): # number of member properties should be 6 if len(memProp) == self.__COL_AMT: member = model.Member() # create a new instance of member age = util.parseInt(memProp[self.__COL_INDEX_AGE]) # get Age at index 0 of Member Properties if type(age) == int: # if Age is int, then member.setAge(age) # set the value to the prop else: raise Exception('Age should be integer!') # inform an error win_loss = util.parseInt(memProp[self.__COL_INDEX_WINLOSS]) # get Win-Loss at index 1 of Member Properties if type(win_loss) == int: # if Win-Loss is int, then member.setWinLoss(win_loss) # set the value to the prop else: raise Exception('Win-Loss should be integer!') # inform an error login = util.parseInt(memProp[self.__COL_INDEX_LOGIN]) # get Log-in at index 2 of Member Properties if type(login) == int: # if Log-in is int, then member.setLogin(login) # set the value to the prop else: raise Exception('Log-in should be integer!') # inform an error gender = memProp[self.__COL_INDEX_GENDER] # get Gender at index 3 of Member Properties if gender.lower() in self.__GENDERS: # if Gender is in the possible genders, then member.setGender(gender) # set the value to the prop else: raise Exception('Gender should be "Male" or "Female"!') # inform an error income = util.parseInt(memProp[self.__COL_INDEX_INCOME]) # get Income at index 4 of Member Properties if type(income) == int: # if Income is int, then member.setIncome(income) # set the value to the prop else: raise Exception('Income should be integer!') # inform an error status = memProp[self.__COL_INDEX_STATUS] # get Gender at index 5 of Member Properties member.setStatus(status) # set the value to the prop return member # return the instance else: # else, inform an error raise Exception('Member information is not correct!')
def _Search(self, staff): entityType = entity.member.GetType(self.response.get('Entity'), default=False) if not entityType: return False query = entityType.all() username = self.response.get('username') if username: query.filter('username = '******'name') if name: query.filter('name = ', cgi.escape(name)) _, offset = util.parseInt(self.response.get('offset')) _, limit = util.parseInt(self.response.get('limit')) if limit <= 0: limit = base.DEF_LIMIT if offset < 0: offset = 0 results = query.fetch(limit, offset) content = [ result.toString() for result in results ] self.response.out.write(base.entitysep.join(content)) return True
def post(self): username = myhashlib.check_secure_val(str(self.request.cookies.get('username'))) profile = Profile(id = username, name = self.request.get('name'), email = self.request.get('email'), age = util.parseInt(self.request.get('age')), photo = self.request.get('photo')) profile.put() self.redirect('/')
def submit_points(id): # Get the context ctx = getContext(id) if ctx == None: print("Count not find context " + id) return "", 404 # Check if the job is still running if ctx.status == "stopped": return "", 500 content = request.json modulus = pow(2, ctx.params.dBits) points = [] # Verify all points for i in range(len(content)): a = util.parseInt(content[i]['a']) b = util.parseInt(content[i]['b']) x = util.parseInt(content[i]['x']) y = util.parseInt(content[i]['y']) length = content[i]['length'] # Verify the exponents are within range if a <= 0 or a >= ctx.curve.n or b <= 0 or b >= ctx.curve.n: print("Invalid exponents:") print(str(a)) print(str(b)) return "", 400 # Verify point is valid if x < 0 or x >= ctx.curve.p or y < 0 or y >= ctx.curve.p: print("Invalid point:") print("X: " + str(x)) print("y: " + str(y)) return "", 400 # Check that the x value has the correct number of 0 bits if x % modulus != 0: print("[" + hex(x) + "," + hex(y) + "]") print("Not distinguished point! Rejecting!") return "", 400 # Verify aG = bQ = (x,y) endPoint = ECPoint(x, y) if verifyPoint(ctx.curve, ctx.pointG, ctx.pointQ, a, b, endPoint) == False: print("Invalid point!") print(content[i]) print("") return "", 400 # Append to list dp = {} dp['a'] = a dp['b'] = b dp['x'] = x dp['y'] = y dp['length'] = length points.append(dp) # Connect to database ctx.database.open() # Write list to database collisions = ctx.database.insertMultiple(points) # If there are any collisions, add them to the collisions table if collisions != None: for c in collisions: dp = ctx.database.get(c['x'], c['y']) print("==== FOUND COLLISION ====") print("a1: " + hex(c['a'])) print("b1: " + hex(c['b'])) print("length: " + util.intToString(c['length'])) print("") print("a2: " + hex(dp['a'])) print("b2: " + hex(dp['b'])) print("length: " + util.intToString(dp['length'])) print("") print("x: " + hex(c['x'])) print("y: " + hex(c['y'])) ctx.database.insertCollision(c['a'], c['b'], c['length'], dp['a'], dp['b'], dp['length'], c['x'], c['y']) ctx.database.close() return ""
def do_things(action, args): if action == 'echo': return { 'success': True, 'data': args.get('data', None) } elif action == 'api_users': from serverlib import api return api.get_user_info() elif action == 'authenticate': return authenticate.heavy_authenticate(args.get('user', ''), args.get('password', ''), True) elif action == 'getuser': from serverlib import getuser return getuser.get_user(args.get('user_id_list', None)) else: user_id = util.parseInt(args.get('user_id', 0)) if authenticate.light_authenticate(user_id, args.get('password', '')): if action == 'poll': return poll.do_poll(user_id, args.get('sectors')) elif action == 'build': from serverlib import build return build.do_build( user_id, args.get('client_token'), args.get('last_id'), args.get('sector'), args.get('loc'), args.get('type')) elif action == 'demolish': from serverlib import demolish return demolish.do_demolish( user_id, args.get('last_id'), args.get('sector'), args.get('loc'), args.get('client_token')) elif action == 'radar': from serverlib import neighbors return neighbors.find_neighbors( user_id, args.get('rx', None), args.get('ry', None)) elif action == 'research': from serverlib import research return research.apply_research( user_id, args.get('type', None)) elif action == 'quarrydata': from serverlib import quarrydata return quarrydata.get_quarry_data( user_id, args.get('sector', None), args.get('xy', None)) elif action == 'buildbot': from serverlib import producebot return producebot.produce_bot( user_id, args.get('type', 0)) elif action == 'getbots': from serverlib import producebot return producebot.get_count(user_id) elif action == 'dispatchbots': from serverlib import producebot return producebot.dispatch(user_id) elif action == 'debug_resources': from serverlib import producebot return producebot.DEBUG_resources(user_id) elif action == 'start_research': from serverlib import research return research.apply_research(user_id, args.get('subject', None)) elif action == 'alienkill': from serverlib import serverbattle return serverbattle.award_resources( user_id, args.get('alientype', 'x')) elif action == 'attacksuccess': from serverlib import serverbattle return serverbattle.award_bytes( user_id, args.get('attacked', 0), args.get('numbytes', 0)) else: return { 'success': False, 'message': "Unrecognized command" } else: return { 'success': False, 'message': "Invalid username/password." }
def __createRule(self, ruleProp): # number of rule properties should be 6 if len(ruleProp) == self.__COL_AMT: # create a new object of Rule rule = model.Rule() age = ruleProp[self.__COL_INDEX_AGE] # get Age at index 0 of Rule Properties if age != self.__NO_IMPACT: # if Age is not no-impact, then # split Age conditions by ',' conds = age.split(self.__COND_SEP) # If number of conditions is 2, then if len(conds) == self.__COND_AMT: operator = conds[0] # get operator(GE or LT) at index 0 if operator.lower() in self.__OPERATORS: # if Age operator is in possible operators, then # set the operator to the property rule.setAgeOperator(operator) else: # else, inform an error raise Exception('The Operator of Age is not correct!') val = util.parseInt(conds[1]) # get Age value at index 1 if type(val) == int: # if the Age value is int, then # set the value to the property rule.setAge(val) else: # else, inform an error raise Exception('Age should be integer!') else: # else, inform an error raise Exception('Age info is not correct!') else: # else, set No-Impact to Age prop rule.setAge(self.__NO_IMPACT) win_loss = ruleProp[self.__COL_INDEX_WINLOSS] # get Win-Loss at index 1 of Rule Properties if win_loss != self.__NO_IMPACT: # if Win-Loss is not no-impact, then # split Win-Loss conditions by ',' conds = win_loss.split(self.__COND_SEP) # If number of conditions is 2, then if len(conds) == self.__COND_AMT: operator = conds[0] # get operator(GE or LT) at index 0 if operator.lower() in self.__OPERATORS: # if Win-Liss operator is in possible operators, then # set the operator to the property rule.setWinLossOperator(operator) else: # else, inform an error raise Exception('The Operator of Win-Loss is not correct!') val = util.parseInt(conds[1]) # get Win-Loss value at index 1 if type(val) == int: # if the Win-Loss value is int, then # set the value to the property rule.setWinLoss(val) else: # else, inform an error raise Exception('Win-Loss should be integer!') else: # else, inform an error raise Exception('Win-Loss info is not correct!') else: # else, set No-Impact to Win-Loss prop rule.setWinLoss(self.__NO_IMPACT) login = ruleProp[self.__COL_INDEX_LOGIN] # get Log-in at index 2 of Rule Properties if login != self.__NO_IMPACT: # if Log-in is not no-impact, then # split Log-in conditions by ',' conds = login.split(self.__COND_SEP) # If number of conditions is 2, then if len(conds) == self.__COND_AMT: operator = conds[0] # get operator(GE or LT) at index 0 if operator.lower() in self.__OPERATORS: # if Log-in operator is in possible operators, then rule.setLoginOperator(operator) # set the operator to the property else: # else, inform an error raise Exception('The Operator of Log-in is not correct!') val = util.parseInt(conds[1]) # get Log-in value at index 1 if type(val) == int: # if the Log-in value is int, then rule.setLogin(val) # set the value to the property else: # else, inform an error raise Exception('Log-in should be integer!') else: # else, inform an error raise Exception('Log-in info is not correct!') else: rule.setLogin(self.__NO_IMPACT) # else, set No-Impact to Log-in prop gender = ruleProp[self.__COL_INDEX_GENDER] # get Gender at index 3 of Rule Properties if gender != self.__NO_IMPACT: # if Gender is not no-impact, then # set the equal operator to the property rule.setGenderOperator(self.__EQUAL) if gender.lower() in self.__GENDERS: # if Gender value is in possible genders, then rule.setGender(gender) # set the value to the property else: # else, inform an error raise Exception('Gender should be "Male" or "Female"!') else: # else, set No-Impact to Gender prop rule.setGender(self.__NO_IMPACT) income = ruleProp[self.__COL_INDEX_INCOME] # get Income at index 4 of Rule Properties if income != self.__NO_IMPACT: # if Income is not no-impact, then conds = income.split(self.__COND_SEP) # split xxx conditions by ',' # If number of conditions is 2, then if len(conds) == self.__COND_AMT: operator = conds[0] # get operator(GE or LT) at index 0 if operator.lower() in self.__OPERATORS: # if Income operator is in possible operators, then rule.setIncomeOperator(operator) # set the operator to the property else: # else, inform an error raise Exception('The Operator of Income is not correct!') val = util.parseInt(conds[1]) # get Income value at index 1 if type(val) == int: # if the Income value is int, then rule.setIncome(val) # set the value to the property else: # else, inform an error raise Exception('Income should be integer!') else: # else, inform an error raise Exception('Income info is not correct!') else: # else, set No-Impact to Income prop rule.setIncome(self.__NO_IMPACT) status = ruleProp[self.__COL_INDEX_STATUS] # get Status at index 5 of Rule Properties if status.lower() in self.__STATUS: # if Status operator is in possible statuses, then rule.setStatus(status) # set the value to the property else: # else, inform an error raise Exception('Status should be "Continue" or "Discontinue"!') # return the instance of rule return rule else: raise Exception('Rule information is not correct!')