def on_status(self, data): global count count += 1 if count % 100 == 0: logging.debug('%s tweets gathered.' % str(count)) tweetID = data.id_str tweet = {} tweet['id'] = encode(data.id_str) tweet['created_at'] = dumps(data.created_at, default=json_serial) tweet['text'] = encode(data.text) tweet['username'] = encode(data.user.screen_name) tweet['url'] = encode(data.user.url) tweet['location'] = encode(data.user.location) json_tweet = json.dumps(tweet) redis.lpush(args.query, json_tweet) i = redis.llen(args.query) if args.mode == 'f': if i % 50 == 0: os.system('python writer.py -q %s' % args.query) if args.mode == 'd': if i % 50 == 0: os.system('python upload.py -q %s' % args.query)
def genqrcode(): try: uid = request.cookies.get("uid") shown = request.form.get("shown") hidden = request.form.get("hidden") nickname = request.form.get("nickname") option = request.form.get("option") if shown == None: # and hidden == None and nickname == None: shown = request.cookies.get("shown") hidden = request.cookies.get("hidden") nickname = request.cookies.get("nickname") option = request.cookies.get("option") newid = insert_db(uid, shown, hidden, nickname, option) util.encode(uid, newid) img_path = "data/" + uid + "/" + newid + ".png" img_url = url_for("static", filename=img_path) redirect_dst = redirect("/share/?uid=" + uid + "&pid=" + newid + "&nn=" + nickname) resp = make_response(redirect_dst) # resp = make_response(render_template("gen_qrcode.html", url = img_url)) resp.set_cookie("shown", shown) resp.set_cookie("hidden", hidden) resp.set_cookie("nickname", nickname) resp.set_cookie("option", option) return resp except Exception, e: return "抱歉,您请求的页面不存在"
def serialize(self, jsonify=True): seed=encode(self.seed) pub=encode(self.pub.save_pkcs1('DER')) sig=encode(self.sig) if jsonify: return json.dumps([seed, pub, sig]) else: return [seed, pub, sig]
def getSuperOrSubclasses(word, isSuperClass): superclasses = [] criteria = {} if isSuperClass: criteria['start'] = word else: criteria['end'] = word criteria['rel'] = "/r/IsA" for assertion in query(criteria): if isSuperClass and assertion['end'].startswith('/c/en'): superclasses.append(util.encode(assertion['end'])) if (not isSuperClass) and assertion['start'].startswith('/c/en'): superclasses.append(util.encode(assertion['start'])) return superclasses
def _handle_exit_event(self, socket, player_id): # send confirm to the exited client socket.sendall(encode(Event.EXIT)) message_to_other_clients = Event.EXIT + str(player_id) + Event.DELIM self._notify_other_sockets_except( socket, encode(message_to_other_clients)) for connection in self.connected_sockets: if connection[0] == socket: self.connected_sockets.remove(connection) break print("removed socket")
def render(self, name, value, attrs): aquestion = question() label = '%s %s ?' % (settings.QUESTION, aquestion) value = super(MathWidget, self).render(name, value, attrs) hidden = '<input type="hidden" value="%s" name="math_captcha_question"/>' % encode( aquestion) return value.replace('<input', '%s %s = <input' % (hidden, label))
def get(self): user_messages = self.notifications.get('user_messages') working_model = models.Workings(self.db) report_model = models.Reports(self.db) filter_by_date = self.get_argument('filterByDate', 'today') s_date, e_date = util.str_to_date_range(filter_by_date) if self.get_argument('id', None) and ("ROLE_ADMIN" in self.current_user['roles']): user_model = models.Users(self.db) user = yield user_model.get({'_id': self.get_argument('id')}) view = "admin" else: user = self.current_user view = "user" user_id = user['_id'] query = {'user_id': user['_id'], 'end': None} working = yield working_model.get(query) if working: today = khayyam.jalali_datetime.JalaliDatetime().now(khayyam.teh_tz).todatetime().day start_day = working['start'].day if today > start_day: end = datetime.datetime.combine(working['start'].date(), datetime.time(23, 59, 59)) working_model.end(working['_id'], end, True) self.redirect(self.reverse_url('user')) return do = 'stop' wid = util.encode(str(working['_id']), 'url') else: do = 'start' wid = '' reports = yield report_model.get_all_fb_user_fb_date(user_id, s_date, e_date) workings = yield working_model.get_all_fb_user_fb_date(user_id, s_date, e_date) self.render('user.html', do=do, wid=wid, user=user, workings=workings, reports=reports, view=view)
def math_clean(form): """ Cleans a form, validating answer to math question in the process. The given ``form`` must be an instance of either ``MathCaptchaModelForm`` or ``MathCaptchaForm``. Answer keys are communicated in the ``math_captcha_question`` field which is evaluated to give the correct answer after being validated against the ``SECRET_KEY`` """ try: value = form.cleaned_data['math_captcha_field'] test_secret, question = decode( form.cleaned_data['math_captcha_question']) assert len(test_secret) == 40 and question except (TypeError, AssertionError): # problem decoding, junky data raise forms.ValidationError('Invalid token') except KeyError: return if encode(question) != form.cleaned_data['math_captcha_question']: # security problem, hack attempt raise forms.ValidationError('Invalid token') if eval(question) != value: raise forms.ValidationError('Wrong answer, try again')
def encodedText(self): """Returns the text of the document encoded in the correct encoding. Useful to save to a file. """ return util.encode(self.toPlainText(), self.encoding())
def run(self): self.socket.listen(MAX_CONNECTION) print('waiting for a connection') while True: self._serve_players() try: connection_socket, client_address = self.socket.accept() except error: continue print("connection established with ", client_address[0]) if len(self.connected_sockets) < self.max_players: connection_socket.setblocking(0) player_id = self._next_available_id() self.players_position[str(player_id)] = (22, 0) self.connected_sockets.append( (connection_socket, str(player_id))) else: try: connection_socket.sendall(encode(Event.SERVER_FULL)) print("SERVER_FULL sent successful...") except error as e: print("Failed to send SERVER_FULL. exception raised: ", e)
def fetch(doi, delay=0.3): before = time.time() # conn = httplib.HTTPConnection('api.springer.com') conn = httplib.HTTPConnection('springer.api.mashery.com') path = '/metadata/json?%s' % urllib.urlencode({'q':'doi:'+doi, 'api_key':keys['metadata']}) conn.request('GET', path) response = conn.getresponse() status = response.status data = response.read() conn.close() after = time.time() time.sleep(max(0, delay + before - after)) if status == 200: meta = util.encode(json.loads(data)) # !!! metadata encoding? if meta['records']: return meta else: # sometimes get an empty response rather than a 404 raise db.NotFound('fetch:empty', doi) elif status == 404: raise db.NotFound('fetch:404', doi) else: raise CommError(data, 'http://api.springer.com' + path, code=status)
def saveCopyAs(self): import ly.lex doc = self.currentDocument() if not self.currentView().textCursor().hasSelection(): import documentinfo mode = documentinfo.mode(doc) data = doc.encodedText() caption = app.caption(_("dialog title", "Save Copy")) else: import fileinfo text = self.currentView().textCursor().selection().toPlainText() mode = fileinfo.textmode(text) data = util.encode(text) caption = app.caption(_("dialog title", "Save Selection")) filetypes = app.filetypes(ly.lex.extensions[mode]) dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir() filename = QFileDialog.getSaveFileName(self, caption, dirname, filetypes) if not filename: return # cancelled try: with open(filename, "w") as f: f.write(data) except (IOError, OSError) as err: QMessageBox.warning(self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}\n\n{error}").format( url=filename, error=err.strerror))
def send_filters_one(agent, log): """ Send filters to each agent that needs it. @type agent: Agent @param agent: Agent to send filters to @type log: logger @param log: Logger object for logging """ global agents, filters for filt in filters: log.debug("Agent -- " + agent.__str__()) if agent and filters[filt].isUsed(agent.getName()): try: # Encode the body body = util.encode( json.dumps({ 'code': filt, 'filter': filters[filt].getInfo() }), agent.getKey()) util.send_post(agent.getAddress() + '/filters', body, log) # Send the body except: log.warning("Agent at " + agent.getName() + " is no longer alive.") agent.setStatus(False)
def render(self, name, value, attrs): aquestion = question() value = super(MathWidget, self).render(name, value, attrs) hidden = '<input type="hidden" value="%s" name="math_captcha_question"/>' % encode( aquestion) return mark_safe( value.replace('<input', '%s %s = <input' % (hidden, aquestion)))
def saveCopyAs(self): import ly.lex doc = self.currentDocument() if not self.currentView().textCursor().hasSelection(): import documentinfo mode = documentinfo.mode(doc) data = doc.encodedText() caption = app.caption(_("dialog title", "Save Copy")) else: import fileinfo text = self.currentView().textCursor().selection().toPlainText() mode = fileinfo.textmode(text) data = util.encode(util.platform_newlines(text)) caption = app.caption(_("dialog title", "Save Selection")) filetypes = app.filetypes(ly.lex.extensions[mode]) dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir() filename = QFileDialog.getSaveFileName(self, caption, dirname, filetypes)[0] if not filename: return # cancelled try: with open(filename, "wb") as f: f.write(data) except IOError as e: msg = _("{message}\n\n{strerror} ({errno})").format( message = _("Could not write to: {url}").format(url=filename), strerror = e.strerror, errno = e.errno) QMessageBox.critical(self, app.caption(_("Error")), msg)
def sync(self): print('Connecting...') self.sleep = 0.1 while self.ack < 3: time.sleep(self.sleep) self.sleep = min(self.sleep * 2, 4) self.request_update_socket() self.request_save_public_key() self.request_update_receiver_public_key() self.request_save_encrypted_socket() self.request_update_encrypted_socket() _data = {"op": "syn", "data": {"ack": self.ack}} if not self.receiver[0]: continue self.__probe(util.encode(_data), self.receiver) _rpkm = self.receiver_public_key_md5 _log = 'send ack = {} from {}:{} to {}:{}' log = _log.format( self.ack, self.socket[0], self.socket[1], self.receiver[0], self.receiver[1] ) logging.info(log) print(log) _rpkm = self.receiver_public_key_md5 log = 'the connection to {} has been established '.format(_rpkm) logging.info(log)
def serialize(self, saveSig=False, jsonify=False): if self.args: if type(self.args)==unicode: msg=[encode(self.pub.save_pkcs1('DER')), self.time, self.cmd, self.coin.serialize(jsonify=False), self.args] else: msg=[encode(self.pub.save_pkcs1('DER')), self.time, self.cmd, self.coin.serialize(jsonify=False), encode(self.args.save_pkcs1('DER'))] else: msg=[encode(self.pub.save_pkcs1('DER')), self.time, self.cmd, self.coin.serialize(jsonify=False)] if saveSig: msg=[encode(self.sig)]+msg if jsonify: return json.dumps(msg) else: return msg
def create(self) -> (np.ndarray, np.ndarray, np.ndarray): pointer = int( np.ceil(self.max_int / 2) if self.max_int % 2 != 0 else self.max_int / 2) init_mem = np.zeros((self.batch_size, self.max_int), dtype=np.int32) init_mem[:, 0] = pointer for idx in range(self.batch_size): init_mem[idx, 1:(1 + self.sequence_size)] = np.random.permutation( self.sequence_size) init_mem[:, pointer:(pointer + self.sequence_size)] = \ np.random.randint(1, self.max_int, size=(self.batch_size, self.sequence_size), dtype=np.int32) out_mem = init_mem.copy() permutations = encode(out_mem[:, 1:(1 + self.sequence_size)]) for idx in range(self.batch_size): out_mem[idx, 1:(1 + self.sequence_size)] = np.tensordot( out_mem[idx, pointer:(pointer + self.sequence_size)], permutations[idx], axes=(0, 1)) cost_mask = np.zeros((self.batch_size, self.max_int), dtype=np.int8) cost_mask[:, 1:(1 + self.sequence_size)] = 1 return init_mem, out_mem, cost_mask
def send_filters(log, tag=None): """ Send filters to each agent that needs it. @type log: logger @param log: Logger object for logging @type tag: string @param tag: None if send all filters, otherwise the tag of the specific filter to send """ global agents, filters lock.acquire() try: for filt in filters: if tag: filt = tag for x in range(0, len(agents)): log.debug("Agent -- " + agents[x].__str__()) if agents[x] and filters[filt].isUsed(agents[x].getName()): # try: dump = {'code': filt, 'filter': filters[filt].getInfo()} body = util.encode(json.dumps(dump), agents[x].getKey()) # Encode the body util.send_post(agents[x].getAddress() + '/filters', body, log) # Send the body # except: # log.warning("Agent at " + agents[x].getAddress() + " is no longer alive.") # agents[x].setStatus(False) if tag: break finally: lock.release()
def send_post(path, args, log): """ Send a given body to each agent at the given path. Encodes each body with the agent's corresponding secret key before sending @type path: string @param path: The path to send the body to @type args: JSON parsable object (dict, list, string, etc) @param args: Body to be encoded and sent @type log: logger @param log: Logger object for logging """ global agents lock.acquire() try: for x in range(0, len(agents)): log.debug("Agent -- " + agents[x].__str__()) if agents[x]: try: body = util.encode(args, agents[x].getKey()) # Encode the body util.send_post(agents[x].getAddress() + path, body, log) # Send the body except: log.warning("Agent at " + agents[x].getName() + " is no longer alive.") agents[x].setStatus(False) finally: lock.release()
def __call__(self, *args, **kwargs): in_mem, out_mem, error_mask = self.create() return encode(in_mem), \ out_mem, \ error_mask, \ self.init_regs(np.zeros((self.batch_size, self.num_regs, self.max_int), dtype=np.float32)), \ self.timesteps
def math_clean(form): """ Cleans a form, validating answer to math question in the process. The given ``form`` must be an instance of either ``MathCaptchaModelForm`` or ``MathCaptchaForm``. Answer keys are communicated in the ``math_captcha_question`` field which is evaluated to give the correct answer after being validated against the ``SECRET_KEY`` """ try: value = form.cleaned_data['math_captcha_field'] test_secret, question = decode( form.cleaned_data['math_captcha_question']) assert len(test_secret) == 40 and question except (TypeError, AssertionError): # problem decoding, junky data form._errors['math_captcha_field'] = form.error_class(["Invalid token"]) del form.cleaned_data['math_captcha_field'] except KeyError: return if encode(question) != form.cleaned_data['math_captcha_question']: # security problem, hack attempt form._errors['math_captcha_field'] = form.error_class(["Invalid token"]) del form.cleaned_data['math_captcha_field'] if eval(question) != value: form._errors['math_captcha_field'] = form.error_class(["Wrong answer, try again"]) del form.cleaned_data['math_captcha_field']
def saveCopyAs(self): import ly.lex doc = self.currentDocument() if not self.currentView().textCursor().hasSelection(): import documentinfo mode = documentinfo.mode(doc) data = doc.encodedText() caption = app.caption(_("dialog title", "Save Copy")) else: import fileinfo text = self.currentView().textCursor().selection().toPlainText() mode = fileinfo.textmode(text) data = util.encode(text) caption = app.caption(_("dialog title", "Save Selection")) filetypes = app.filetypes(ly.lex.extensions[mode]) dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir() filename = QFileDialog.getSaveFileName(self, caption, dirname, filetypes) if not filename: return # cancelled try: with open(filename, "w") as f: f.write(data) except IOError as e: msg = _("{message}\n\n{strerror} ({errno})").format( message = _("Could not write to: {url}").format(url=filename), strerror = e.strerror, errno = e.errno) QMessageBox.critical(self, app.caption(_("Error")), msg)
def saveCopyAs(self): import ly.lex doc = self.currentDocument() if not self.currentView().textCursor().hasSelection(): import documentinfo mode = documentinfo.mode(doc) data = doc.encodedText() caption = app.caption(_("dialog title", "Save Copy")) else: import fileinfo text = self.currentView().textCursor().selection().toPlainText() mode = fileinfo.textmode(text) data = util.encode(text) caption = app.caption(_("dialog title", "Save Selection")) filetypes = app.filetypes(ly.lex.extensions[mode]) dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir() filename = QFileDialog.getSaveFileName(self, caption, dirname, filetypes) if not filename: return # cancelled try: with open(filename, "w") as f: f.write(data) except (IOError, OSError) as err: QMessageBox.warning( self, app.caption(_("Error")), _("Can't write to destination:\n\n{url}\n\n{error}").format( url=filename, error=err.strerror))
def request_save_public_key(self): pkm = self.public_key_md5 pk = self.public_key _data = {"op": "spk", "data": {"pkm": pkm, "pk": pk}} self.udp_client.sendto(util.encode(_data), self.server) log = 'request saving public key' logging.info(log)
def export(self): return encode(json.dumps({ "roles": [str(i) for i in self.roles], "channel": str(self.post_channel) if self.post_channel else None, "options": self.options.as_num(), "prefix": self.prefix, "offset": self.offset + 1 }))
def set_item_dict( pfx, item_dict ) : for (k,v) in item_dict.items() : try: raw_data = util.encode( v ) mc = memcache.Client( etc.memcached_addr, debug=0 ) mc.set( pfx+'_item_'+str(k), raw_data, etc.item_timeout ) except Exception as e : log.exp( e )
def replace_ss( ss_data ) : try: raw_data = util.encode( ss_data ) mc = memcache.Client( etc.memcached_addr, debug=0 ) if mc.replace( 'ss_'+ss_data.ss_id, raw_data, etc.session_timeout ) : return True return False except Exception as e: log.exp(e) return False
def encodedText(self): """Returns the text of the document encoded in the correct encoding. The line separator is '\\n' on Unix/Linux/Mac OS X, '\\r\\n' on Windows. Useful to save to a file. """ text = util.platform_newlines(self.toPlainText()) return util.encode(text, self.encoding())
def set_item( pfx, key, item ) : try: raw_data = util.encode( item ) r = redis.Redis( connection_pool=pool ) if r.set( pfx+'_item_'+str(key), raw_data ) : return True return False except Exception as e : log.exp( e ) return False
def set_node( pfx, key, node ) : try: raw_data = util.encode( node ) mc = memcache.Client( etc.memcached_addr, debug=0 ) if mc.set( pfx+'_node_'+str(key), raw_data, etc.node_timeout ) : return True return False except Exception as e : log.exp( e ) return False
def request_update_encrypted_socket(self): try: pkm = self.receiver_public_key_md5 _data = {"op": "ges", "data": {"pkm": pkm}} self.udp_client.sendto(util.encode(_data), self.server) log = 'request receiver encrypted socket' logging.info(log) except: log = 'an error occurred during decryption' logging.info(log)
def recognize_stream(self, font=Settings.font, accuracy=Settings.accuracy, detection_delay=Settings.detection_delay, font_size=Settings.font_size, path_to_video=Settings.path_to_video): detector = FaceDetector() # initialize parameters from Config.py mysql = MysqlConnector(host=Config.host, user=Config.user, password=Config.password, database=Config.database_name) database_users = mysql.select_all_users() users = encode(database_users=database_users) v_cap = cv2.VideoCapture(path_to_video) boxes = None img_embedding = [] recognized_users = [] counter = 0 while True: success, img = v_cap.read() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = Image.fromarray(img) boxes, _ = detector.mtcnn.detect(img) detect = detector.mtcnn(img) if counter == 0: if detect is not None: img_embedding = detector.resnet(detect) recognized_users = recognize_users( face_embedding=img_embedding, users=users, accuracy=accuracy) image = draw_bounding_box(boxes=boxes, recognized_users=recognized_users, pil_image=img, font_path=font, font_size=font_size) counter += 1 if counter == detection_delay: counter = 0 # display and close window cv2.imshow("Video", convert_to_cv(image)) if cv2.waitKey(1) & 0xFF == ord('f'): print("Program has been closed") sys.exit(1)
def post(self): upload_files = self.get_uploads('photo') # 'file' is file upload field in the form # if the user uploaded a file if upload_files: blob_info = upload_files[0] imageKey = blob_info.key() # Check valid image file image_types = ('image/bmp', 'image/jpeg', 'image/png') if blob_info.content_type not in image_types: blob_info.delete() self.redirect('/pet/error?msg=invalid%20file%20type') return else: # resize image to prevent storing too much data pass else: imageKey = None petName = self.request.get('petName') # provide a default pet group name if one is not provided # for some absolutely bazare reason, the default value is not working.. groupName = self.request.get('groupName', default_value = 'My Pet Family') # force default value if not groupName: groupName = 'My Pet Family' groupCode = self.getNextPetGroupCode() newGroup = PetGroup(name=groupName, shortCodeInteger=groupCode) import util shortCode = util.encode(groupCode) if not petName: self.writeError("Sorry, please give your pet a name!") return newGroup.put() groupId = newGroup.key().id() newPet = Pet(name=petName, photoKey=imageKey, petGroup=groupId) newPet.put() # output json self.writeJSON( {'pet' : {'name': petName, 'id': str(newPet.key().id()), 'parentGroup': groupId, 'photoUrl': self.request.host_url+'/pet/img/'+str(imageKey) }, 'group': {'name': groupName, 'id': groupId, 'shortCode': shortCode, } })
def get_public_key(data, addr, udp_server): global MD5_PK try: pkm = data['pkm'] pk = MD5_PK.get(pkm) or 'NULL' data = {'op': 'gpk', 'data': {'pkm': pkm, 'pk': pk}} udp_server.sendto(util.encode(data), addr) logging.info(data) except: source = '{}:{}'.format(addr[0], addr[1]) logging.warning('unknown args from {}'.format(source))
def get_encrypted_socket(data, addr, udp_server): global MD5_SOCKET try: pkm = data['pkm'] es = MD5_SOCKET.get(pkm) or 'NULL' data = {'op': 'ges', 'data': {'pkm': pkm, 'es': es}} udp_server.sendto(util.encode(data), addr) logging.info(data) except: source = '{}:{}'.format(addr[0], addr[1]) logging.warning('unknown args from {}'.format(source))
def save_public_key(data, addr, udp_server): global MD5_PK try: public_key_md5 = data['pkm'] public_key = data['pk'] MD5_PK[public_key_md5] = public_key data = {'op': 'spk', 'data': {'pkm': public_key_md5}} udp_server.sendto(util.encode(data), addr) logging.info(data) except: source = '{}:{}'.format(addr[0], addr[1]) logging.warning('unknown args from {}'.format(source))
def fetchVideoInfo(self, forceUpdate=False, foreign=False): """ Populate the *new* variables with information. """ # Try populating values from the NFO first self.nfoInfo = self._getNfoInfo() self.imdbUrl = self._getImdbUrlFromNfo() if (self.nfoInfo): self.title = util.encode(self.nfoInfo.findtext("//movie/title")) self.year = self.nfoInfo.findtext("//movie/year") self.country = util.encode(self.nfoInfo.findtext("//movie/country")) self.aka = util.encode(self.nfoInfo.findtext("//movie/aka")) self.imdbUpdate = util.encode(self.nfoInfo.findtext("//movie/imdbupdate")) self.trailerUrl = self.nfoInfo.findtext("//movie/trailerurl") if (self.year): self.year = int(self.year) # If not all required values, get them from IMDB if (not self.nfoInfo) or (forceUpdate): self.imdbUrl = self.imdbUrl or self._getImdbUrlFromSearch(foreign) self.imdbInfo = self._getImdbInfoFromUrl(self.imdbUrl) self.imdbUpdate = time.strftime("%Y-%m-%d %H:%M:%S") if (self.imdbInfo): self._newInfoFound = True self.title = self.title or util.encode(self.imdbInfo['title']) self.year = self.year or self.imdbInfo['year'] self.country = self.country or util.encode(self.imdbInfo['country'][0]) self.aka = self._getAka(self.imdbInfo) # Update New DirName and FileNames self.updateNewDirName(self.aka if foreign else None) self.updateNewFilePrefix(self.aka if foreign else None) self.updateNewFileNames()
def get(name, key): ensure(name) discodbs = dbs[name] partition = util.default_partition(key, len(discodbs), None) results = discodbs[partition].get(key) if results == None: raise NotFound('db:' + name, key) else: results = list(results) if len(results) == 1: return util.encode(pickle.loads(results[0])) else: raise MultipleValues(name, key, results)
def wait_for_valid_rfid(): while True: try: temp = getpass.getpass("Blip me! ") if temp: rfid = util.encode(temp) break else: print("Tagg id empty try again") except ValueError: print("Blip not recognised") print("") return rfid
def set_item_dict( pfx, item_dict ) : r = redis.Redis( connection_pool=pool ) pipe = r.pipeline() #原子pipe #pipe = r.pipeline(transaction=False) #非原子pipe for (k,v) in item_dict.items() : try: raw_data = util.encode( v ) pipe.set( pfx+'_item_'+str(k), raw_data ) except Exception as e : log.exp( e ) try: pipe.execute() except Exception as e : log.exp( e )
def load_lazy(input_file, dict): for line in open(input_file): if line.strip() == "": continue components = line.strip().split('\t') if len(components) not in [2, 8]: continue title = components[0] articles = components[1:] title = encode(title, dict["w2i"]) if len(articles) > 1: articles = [ encode(article, dict["w2i"]) for article in articles ] elif len(articles) > 0: articles = encode(articles[0], dict["w2i"]) yield articles, title
def request_save_encrypted_socket(self): try: plain_socket = '{}:{}'.format(self.socket[0], str(self.socket[1])) _rpl = self.receiver_public_key pkm = self.public_key_md5 es = self.__encrypt(plain_socket, _rpl) _data = {"op": "ses", "data": {"pkm": pkm, "es": es}} self.udp_client.sendto(util.encode(_data), self.server) log = 'request saving encrypted socket' logging.info(log) except: log = 'an error occurred during encryption' self.request_update_receiver_public_key() logging.info(log)
def encode(self): """ Encode/encrypt auth dict structure. :return: encoded string """ if not self.verify(): return None try: s = json.dumps(self.auth_dict) return encode(APP.config['SECRET_KEY'], s) except Exception as err: LOGGER.error('Error encoding auth: %s' % str(err)) raise err
def init_one(agent, log): """ Initialize one newly recognized agent. Runs on a thread and sends all records. @type agent: Agent @param agent: New agent to initialize @type log: logger @param log: Logger object for logging """ # Attempt to connect three times before giving up on the agent. tries = 3 while tries > 0: try: util.send_post(agent.getAddress() + '/init', util.encode('[]', agent.getKey()), log) break except: # If could not connect, decrement remaining tries and sleep 2 seconds tries -= 1 sleep(2) continue # Send all the records to the new agent and then reload its DNSMasq log.info("Filters with " + agent.getAddress()) send_filters_one(agent, log) log.info("NS with " + agent.getAddress()) send_records_one(agent, 'ns', ns_file, log) log.info("A with " + agent.getAddress()) send_records_one(agent, 'a', a_file, log) log.info("PTR with " + agent.getAddress()) send_records_one(agent, 'ptr', ptr_file, log) log.info("Reloading " + agent.getAddress()) util.send_post(agent.getAddress() + '/reload', util.encode('[]', agent.getKey()), log) log.info("Done with " + agent.getAddress())
def send_records_one(agent, rtype, fl, log): """ Send all of the records of one type from the local file to the given agent. @type agent: Agent @param agent: New agent to initialize @type rtype: string @param rtype: Defines the records type ('ns', 'a', or 'ptr') @type fl: string @param fl: Address of the file @type log: logger @param log: Logger object for logging """ ret = [] for r in communicator.sendFromFile(fl): ret.append(r) if len(ret) > 1000: # Send 1000 records at a time body = util.encode(json.dumps(ret), agent.getKey()) util.send_post(agent.getAddress() + '/init/' + rtype, body, log) ret = [] # Send remaining records body = util.encode(json.dumps(ret), agent.getKey()) util.send_post(agent.getAddress() + '/init/' + rtype, body, log)
def handler(con): try: f = con.makefile('rw') while not closed: cmd, args = readCmdLine(f) if not cmd: con.close() return logger.info(u'处理命令 %s %s', cmd, args) if hasattr(cmdHandler, cmd): m = getattr(cmdHandler, cmd) try: message = m(*args) if not message: f.write('OK\n') else: f.write('VALUE %s\n' % EOFflag) f.write(encode(message)) f.write('\n') f.write(EOFflag) f.write('\n') except Exception as e: logger.exception(u'处理命令异常 %s %s', cmd, args) f.write('ERROR %s\n' % inline(encode(e))) else: f.write('ERROR unknow cmd %s\n' % cmd) f.flush() except socket.error as e: if e.errno == 32: # Broken pipe # 连接断开 pass else: raise finally: con.close()
def recognize_image(self, image_path, font=Settings.font, accuracy=Settings.accuracy, font_size=Settings.font_size): try: tested_image = Image.open(image_path) detector = FaceDetector() # tested image path -> sample-images/multi/3.jpg # invalid picture path -> sample-images/invalid/1.jpg mtcnn = detector.mtcnn(tested_image) if mtcnn is not None: tested_image_embedding = detector.resnet(mtcnn) # initialize parameters from config.txt mysql = MysqlConnector(host=Config.host, user=Config.user, password=Config.password, database=Config.database_name) database_users = mysql.select_all_users() users = encode(database_users=database_users) recognized_users = recognize_users( face_embedding=tested_image_embedding, users=users, accuracy=accuracy) boxes, _ = detector.mtcnn.detect(tested_image) tested_image = draw_bounding_box( boxes=boxes, recognized_users=recognized_users, pil_image=tested_image, font_path=font, font_size=font_size) else: print("no face was detected") cv2.imshow("recognized Image", convert_to_cv(pil_image=tested_image)) cv2.waitKey(0) except FileNotFoundError: print(" No such file or directory: ", end=image_path)
def register(uid, pw, biom_file): uid = int(uid) biom = util.load_biometric(biom_file) tp = numpy.random.rand(4) ct = compute_cancellable_template(biom, tp) k = str(uuid.uuid1()) kcw = util.encode(k) ltk = kcw ^ ct pwd = compute_one_way_hash(str(uid) + k + pw) auth_params = register_user(uid, pwd) f = compute_one_way_hash(str(uid) + str(pwd) + str(ct)) smart_card = (auth_params, tp, ltk, compute_one_way_hash(k), f) store_user_data(biom_file, smart_card, uid) print("Successfully registered user: ", uid) return 0
def _handle_player_action_events(self, socket, player_id, events): if events[3] == "exit": self._handle_exit_event(socket, player_id) else: events_count = len(events) // 4 for event_index in range(events_count): self.players_position[player_id] = ( events[event_index * 4 + 1], events[event_index * 4 + 2]) message = Event.PLAYER + player_id + Event.DELIM + \ Event.ACTION + events[event_index * 4 + 3] + Event.DELIM self._notify_other_sockets_except( socket, encode(message))
def timeout(): elapsed_time = time.time( ) - start_time if time.time() - start_time < 60 else 60 status = False if (self.player_agent.attr['hp'] <= 0 or self.player_user.attr['hp'] > self.player_agent.attr['hp']): status = True elif (self.player_agent.score < self.player_user.score): status = True result = { 'score': game.player_user.score, 'result': status, 'elapsedTime': elapsed_time * 1000 } message = util.encode(result) # publish the message game.redisClient.publish('worker' + sys.argv[2] + 'simulation', message) game.timeout = True
def get(self): name = self.request.get(GROUP_NAME) if not name: self.writeError('Sorry, you must have a group name') else: import util sci = getNextPetGroupCode() newPetGroup = PetGroup(name = name, shortCodeInteger = sci) newPetGroup.put() shortcode = util.encode(sci) self.writeJSON( { GROUP_NAME : newPetGroup.name, 'id' : newPetGroup.key().id(), 'shortcode' : shortcode } )
def move_to_include_file(cursor, parent_widget=None): """Opens a dialog to save the cursor's selection to a file. The cursor's selection is then replaced with an \\include statement. This function does its best to supply a good default filename and use it correctly in a relative \\include statement. Of course it only works well if the document already has a filename. """ doc = cursor.document() text = cursor.selection().toPlainText() mode = fileinfo.textmode(text) caption = app.caption(_("dialog title", "Move to include file")) filetypes = app.filetypes(ly.lex.extensions[mode]) name, ext = os.path.splitext(os.path.basename(doc.url().path())) if not ext or mode == "lilypond": ext = ".ily" version = documentinfo.docinfo(doc).version_string() if version: text = '\\version "{0}"\n\n{1}'.format(version, text) docname = name + "-include" + ext dirname = os.path.dirname(doc.url().toLocalFile()) or app.basedir() filename = os.path.join(dirname, docname) filename = QFileDialog.getSaveFileName(parent_widget, caption, filename, filetypes)[0] if not filename: return # cancelled data = util.encode(util.platform_newlines(text)) try: with open(filename, "wb") as f: f.write(data) except IOError as e: msg = _("{message}\n\n{strerror} ({errno})").format( message = _("Could not write to: {url}").format(url=filename), strerror = e.strerror, errno = e.errno) QMessageBox.critical(self, app.caption(_("Error")), msg) return filename = os.path.relpath(filename, dirname) command = '\\include "{0}"\n'.format(filename) cursor.insertText(command)
def math_clean(form): """ Cleans a form, validating answer to math question in the process. The given ``form`` must be an instance of either ``MathCaptchaModelForm`` or ``MathCaptchaForm``. Answer keys are communicated in the ``math_captcha_question`` field which is evaluated to give the correct answer after being validated against the ``SECRET_KEY`` """ try: value = form.cleaned_data['math_captcha_field'] test_secret, question = decode(form.cleaned_data['math_captcha_question']) assert len(test_secret) == 40 and question except (TypeError, AssertionError): # problem decoding, junky data raise forms.ValidationError('Invalid token') except KeyError: return if encode(question) != form.cleaned_data['math_captcha_question']: # security problem, hack attempt raise forms.ValidationError(u'Будь ласка, введіть правильну відповіль цифрою') if eval(question) != value: raise forms.ValidationError(u'Будь ласка, введіть правильну відповідь цифрою')
def test_bad_operation(self): form = MathCaptchaForm({'math_captcha_field':'0', 'math_captcha_question':encode(' - 1')}) self.assert_(form.as_p().find('errorlist') > -1)
import sys import time import random import struct from keys import loadKeys from coins import Coins from util import encode dir=sys.argv[1] pub, priv = loadKeys(dir) print(encode(pub.save_pkcs1('DER')))
def jsonify(data, null = 404): if null and data == None: abort(null) else: return Response(util.encode(data), mimetype = 'application/json')
def test_negative_value(self): form = MathCaptchaForm({'math_captcha_field':'-1', 'math_captcha_question':encode('0 - 1')}) self.assert_(form.as_p().find('errorlist') > -1)
def test_success(self): form = MathCaptchaForm({'math_captcha_field':'0', 'math_captcha_question':encode('1 - 1')}) self.assert_(form.as_p().find('errorlist') == -1)