def convert(utf8String): try: return utf_8.decode(utf8String)[0] except Exception: LOG_CURRENT_EXCEPTION() LOG_WARNING('Wrong UTF8 string', utf8String) return utf_8.decode('----')[0]
def convert(utf8String): try: return utf_8.decode(utf8String)[0] except Exception as ex: _logger.exception(ex) _logger.warning('Wrong UTF8 string: %r', utf8String) return utf_8.decode('----')[0]
def bceid(request): """ fake bceid login for developer workstation environment """ if request.method == "POST": login_name = request.POST.get('user', '') password = request.POST.get('password', '') # just in case anyone from the general public discovers the dev server # make sure they don't accidentally login and think this is production if password.lower() != 'divorce': return redirect(settings.PROXY_BASE_URL + settings.FORCE_SCRIPT_NAME[:-1] + '/bceid') # convert the login name to a guid hex_name = decode(binascii.hexlify(str.encode(login_name)))[0] fake_guid = hex_name.rjust(32, '0') # save the guid in a session variable request.session['login_name'] = login_name request.session['fake_bceid_guid'] = fake_guid return redirect(settings.PROXY_BASE_URL + settings.FORCE_SCRIPT_NAME[:-1] + '/login') else: return render(request, 'localdev/bceid.html')
def decode(inp: bytes, errors='strict') -> Tuple[str, int]: try: s, con = utf_8.decode(inp, errors) s = apply(s) + "\n" return s, con except Exception as e: sys.excepthook(*sys.exc_info()) raise
def read_string(conn): str_len = read_int(conn) if not str_len: return '' res = to_bytes('') while len(res) < str_len: res = res + conn.recv(str_len - len(res)) res = utf_8.decode(res)[0] if sys.version_info[0] == 2: try: res = ascii.Codec.encode(res)[0] except UnicodeEncodeError: pass return res
def readString(self): """ Reads a string from the input file. A string is composed of a short designating the length in bytes, followed by a *UTF-8* string of the specified length. Returns a unicode string (Python 'unicode') corresponding to the string which was read. """ length = self.readShort() str = self.file.read(length) (unicodestr, lengthConsumed) = utf_8.decode(str) return unicodestr
def wait_for_clients(self): while not self.stopped: conn, addr = self.s.accept() username = conn.recv(1024) username = decode(username)[0] addr = list(addr) addr.insert(2, str(username)) addr = tuple(addr) self.users_data.append(addr) connected_users = [ip + ':' + str(port) + ' - ' + username for (ip, port, username) in self.users_data] print("Connected with ", connected_users[-1]) start_new_thread(self.client_recv, (conn, addr,))
def read_string(conn): """ reads length of text to read, and then the text encoded in UTF-8, and returns the string""" strlen = read_int(conn) if not strlen: return '' res = to_bytes('') while len(res) < strlen: res = res + conn.recv(strlen - len(res)) res = utf_8.decode(res)[0] if sys.version_info[0] == 2 and sys.platform != 'cli': # Py 2.x, we want an ASCII string if possible try: res = ascii.Codec.encode(res)[0] except UnicodeEncodeError: pass return res
def client_recv(self, conn, addr): try: while True: msg = conn.recv(1024) msg = decode(msg)[0] print('client sent: ', msg) if msg == 'send_xml': self.serve_xml(conn, addr, msg) if msg == 'update_whitelist': # TODO pass if msg == 'send_image': self.serve_image(conn, addr, msg) except ConnectionResetError: print('force close', str(addr[1])) finally: self.close_conn(conn, addr)
def signature(params, http_method, secret): ''' Calulate signature for params and http_method according to https://help.aliyun.com/document_detail/25492.html Args: params(dict[string]string): the params to signature http_method(string): HTTP or HTTPS secret(string): the signature secret Returns: (string) the signature for the given input ''' qry = "" for k, v in sorted(params.items()): ek = Pop.percent_encode(k) ev = Pop.percent_encode(v) qry += '&{}={}'.format(ek, ev) to_sign = '{}&%2F&{}'.format(http_method, Pop.percent_encode(qry[1:])) bf = bytearray(utf_8.encode(to_sign)[0]) dig = hmac.digest(utf_8.encode(secret + "&")[0], bf, hashlib.sha1) return utf_8.decode(base64.standard_b64encode(dig))[0]
def pyxl_decode(input, errors='strict'): return utf_8.decode(pyxl_transform_string(input), errors)
def readLineFromMorfeusz(self): readBytes = self.morfeuszProcess.stdout.readline() line = utf_8.decode(readBytes, errors='replace')[0] return line
def process_tx(txn, account): """ process transaction in view of account get basic transaction information, and transaction effects :param txn: :param account: :return: """ tx = txn['tx'] or txn['transaction'] or txn meta = txn['meta'] result = dict() result['date'] = (tx['date'] or tx['Timestamp']) + 0x386D4380 # unix time result['hash'] = tx['hash'] result['type'] = txn_type(tx, account) # 此处同样有疑问 float 和 Number 的用法 result['fee'] = str(float(tx['Fee']) / 1000000.0) result['result'] = meta['TransactionResult'] if meta else 'failed' result['memos'] = [] if result['type'] == 'sent': result['counterparty'] = tx['Destination'] result['amount'] = parse_amount(tx['Amount']) elif result['type'] == 'received': result['counterparty'] = tx['Account'] result['amount'] = parse_amount(tx['Amount']) elif result['type'] == 'trusted': result['counterparty'] = tx['Account'] result['amount'] = reverse_amount(tx['LimitAmount'], tx['Account']) elif result['type'] == 'trusting': result['counterparty'] = tx['LimitAmount']['issuer'] result['amount'] = tx['LimitAmount'] elif result['type'] == 'convert': result['spent'] = parse_amount(tx['SendMax']) result['amount'] = parse_amount(tx['Amount']) elif result['type'] == 'offernew': # result.offertype = tx.Flags & Transaction.flags.OfferCreate.Sell ? 'sell': 'buy' result['gets'] = parse_amount(tx['TakerGets']) result['pays'] = parse_amount(tx['TakerPays']) result['seq'] = tx['Sequence'] elif result['type'] == 'offercancel': result['offerseq'] = tx['Sequence'] elif result['type'] == 'relationset': if account == tx['Target']: result['counterparty'] = tx['Account'] else: result['counterparty'] = tx['Target'] if tx['RelationType'] == 3: result['relationtype'] = 'freeze' else: result['relationtype'] = 'authorize' if account == tx['Target']: result['isactive'] = False else: result['isactive'] = True result['amount'] = parse_amount(tx['LimitAmount']) elif result['type'] == 'configcontract': result['params'] = format_args(tx['Args']) if tx['Method'] == 0: result['method'] = 'deploy' result['payload'] = tx['Payload'] elif tx['Method'] == 1: result['method'] = 'call' result['destination'] = tx['Destination'] # add memo from encodings.utf_8 import decode if tx.__contains__('Memos') and isinstance( tx['Memos'], list) and tx['Memos'].__len__() > 0: for Memo in tx['Memos']: # 此处认定数组Memos中的元素为字典 for key in Memo: try: Memo[key] = decode(base_utils.hex_to_str(Memo[key])) except: Memo[key] = Memo[key] result['memos'].append(Memo) result['effects'] = [] # no effect, return now if not meta or meta['TransactionResult'] != 'tesSUCCESS': return result # process effects for n in meta['AffectedNodes']: node = process_affect_node(n) effect = dict() # now only get offer related effects, need to process other entry type if node['entryType'] == 'offer': # for new and cancelled offers fieldSet = node['fields'] sell = node['fields']['Flags'] and LEDGER_FLAGS['offer']['Sell'] # current account offer if node['fields']['Account'] == account: # 1. offer_partially_funded if node['diffType'] == 'ModifiedNode' or ( node['diffType'] == 'DeletedNode' and node['fieldsPrev']['TakerGets'] and not is_amount_zero( parse_amount(node['fieldsFinal']['TakerGets']))): effect['effect'] = 'offer_partially_funded' effect['counterparty'] = { 'account': tx['Account'], 'seq': tx['Sequence'], 'hash': tx['hash'] } if node['diffType'] != 'DeletedNode': # no need partially funded must remains offers effect['remaining'] = not is_amount_zero( parse_amount(node['fields']['TakerGets'])) else: effect['cancelled'] = True effect['gets'] = parse_amount(fieldSet['TakerGets']) effect['pays'] = parse_amount(fieldSet['TakerPays']) effect['got'] = amount_subtract( parse_amount(node['fieldsPrev']['TakerPays']), parse_amount(node['fields']['TakerPays'])) effect['paid'] = amount_subtract( parse_amount(node['fieldsPrev']['TakerGets']), parse_amount(node['fields']['TakerGets'])) if sell: effect['type'] = 'sold' else: effect['type'] = 'bought' # effect['type'] = 'sold' if sell else effect['type'] = 'bought' else: # offer_funded, offer_created or offer_cancelled offer effect if node['fieldsPrev']['TakerPays']: node['fieldsPrev']['TakerPays'] = 'offer_funded' else: node['fieldsPrev']['TakerPays'] = 'offer_cancelled' if node['diffType'] == 'CreatedNode': effect['effect'] = 'offer_created' else: effect['effect'] = node['fieldsPrev']['TakerPays'] if effect['effect'] == 'offer_funded': fieldSet = node['fieldsPrev'] effect['counterparty'] = { 'account': tx['Account'], 'seq': tx['Sequence'], 'hash': tx['hash'] } effect['got'] = amount_subtract( parse_amount(node['fieldsPrev']['TakerPays']), parse_amount(node['fields']['TakerPays'])) effect['paid'] = amount_subtract( parse_amount(node['fieldsPrev']['TakerGets']), parse_amount(node['fields']['TakerGets'])) if sell: effect['type'] = sell = 'sold' else: effect['type'] = sell = 'bought' # 3. offer_created if effect['effect'] == 'offer_created': effect['gets'] = parse_amount(fieldSet['TakerGets']) effect['pays'] = parse_amount(fieldSet['TakerPays']) if sell: effect['type'] = sell = 'sell' else: effect['type'] = sell = 'buy' # 4. offer_cancelled if effect['effect'] == 'offer_cancelled': effect['hash'] = node['fields']['PreviousTxnID'] # collect data for cancel transaction type if result['type'] == 'offercancel': result['gets'] = parse_amount( fieldSet['TakerGets']) result['pays'] = parse_amount( fieldSet['TakerPays']) effect['gets'] = parse_amount(fieldSet['TakerGets']) effect['pays'] = parse_amount(fieldSet['TakerPays']) if sell: effect['type'] = 'sell' else: effect['type'] = 'buy' effect['seq'] = node['fields']['Sequence'] # 5. offer_bought elif tx['Account'] == account and node['fieldsPrev']: effect['effect'] = 'offer_bought' effect['counterparty'] = { 'account': node['fields']['Account'], 'seq': node['fields']['Sequence'], 'hash': node['PreviousTxnID'] or node['fields']['PreviousTxnID'] } effect['paid'] = amount_subtract( parse_amount(node['fieldsPrev']['TakerPays']), parse_amount(node['fields']['TakerPays'])) effect['got'] = amount_subtract( parse_amount(node['fieldsPrev']['TakerGets']), parse_amount(node['fields']['TakerGets'])) if sell: effect.type = 'bought' else: effect.type = 'sold' # add price if (effect['gets'] and effect['pays']) or (effect['got'] and effect['paid']): created = effect['effect'] == 'offer_created' and effect[ 'type'] == 'buy' funded = effect['effect'] == 'offer_funded' and effect[ 'type'] == 'bought' cancelled = effect['effect'] == 'offer_cancelled' and effect[ 'type'] == 'buy' bought = effect['effect'] == 'offer_bought' and effect[ 'type'] == 'bought' partially_funded = effect[ 'effect'] == 'offer_partially_funded' and effect[ 'type'] == 'bought' effect['price'] = get_price(effect, (created or funded or cancelled or bought or partially_funded)) if result['type'] == 'offereffect' and node[ 'entryType'] == 'AccountRoot': if node['fields']['RegularKey'] == account: effect['effect'] = 'set_regular_key' effect['type'] = 'null' effect['account'] = node['fields']['Account'] effect['regularkey'] = account # add effect if effect: if node['diffType'] == 'DeletedNode' and effect[ 'effect'] != 'offer_bought': effect['deleted'] = True result['effects'].append(effect) # check cross gateway when parse more effect, specially trust related effects, now ignore it return result
def decode(input, errors='strict'): cs, errors = utf_8.decode(input, errors) return transform_string(cs), errors
def mypy_decode(input, errors='strict'): return utf_8.decode(mypy_transform_string(input), errors)
def rot13decode(input, errors='strict'): """ Convert raw bytes to rot-13ed unicode """ decoded, length = utf_8.decode(input, errors) return rot13(decoded), length
def start_processing(self): """loop on created thread which processes communicates with the REPL window""" _debug_write('Started processing thread') try: while True: if self.check_for_exit_socket_loop(): break # we receive a series of 4 byte commands. Each command then # has it's own format which we must parse before continuing to # the next command. self.flush() self.conn.settimeout(10) try: inp = read_bytes(self.conn, 4) self.conn.settimeout(None) cmd = iPythonSocketServer._COMMANDS.get(inp) if inp and cmd is not None: id = "" try: if iPythonSocketServer._COMMANDS_WITH_IDS.get( inp) == True: while True: try: id = read_string(self.conn) break except socket.timeout: pass cmd(self, id) else: cmd(self) except: commandName = utf_8.decode(inp)[0] try: commandName = ascii.Codec.encode( commandName)[0] except UnicodeEncodeError: pass self.replyWithError(commandName, id) else: if inp: print('unknown command', inp) break except socket.timeout: pass except IPythonExitException: _debug_write('IPythonExitException') _debug_write(traceback.format_exc()) pass except socket.error: _debug_write('socket error') _debug_write(traceback.format_exc()) pass except: _debug_write('error in repl loop') _debug_write(traceback.format_exc()) # try and exit gracefully, then interrupt main if necessary time.sleep(2) traceback.print_exc() self.exit_process()
def decode(input, errors='strict'): return utf_8.decode(b'import pyfu ; pyfu.magic(__file__, __name__); del pyfu', errors)
def hy_decode(input, errors='strict'): return utf_8.decode(hy_transform_string(input), errors)
def decode(input, errors="strict"): return utf_8.decode(b"import pyfu ; pyfu.magic(__file__, __name__); del pyfu", errors)
def start_processing(self): """loop on created thread which processes communicates with the REPL window""" _debug_write('Started processing thread') try: while True: if self.check_for_exit_socket_loop(): break # we receive a series of 4 byte commands. Each command then # has it's own format which we must parse before continuing to # the next command. self.flush() self.conn.settimeout(10) try: inp = read_bytes(self.conn, 4) self.conn.settimeout(None) cmd = jediSocketServer._COMMANDS.get(inp) if inp and cmd is not None: id = "" try: if jediSocketServer._COMMANDS_WITH_IDS.get(inp) == True: while True: try: id = read_string(self.conn) break except socket.timeout: pass cmd(self, id) else: cmd(self) except: commandName = utf_8.decode(inp)[0] try: commandName = ascii.Codec.encode(commandName)[ 0] except UnicodeEncodeError: pass self.replyWithError(commandName, id) else: if inp: print ('unknown command', inp) break except socket.timeout: pass except IPythonExitException: _debug_write('IPythonExitException') _debug_write(traceback.format_exc()) pass except socket.error: _debug_write('socket error') _debug_write(traceback.format_exc()) pass except: _debug_write('error in repl loop') _debug_write(traceback.format_exc()) # try and exit gracefully, then interrupt main if necessary time.sleep(2) traceback.print_exc() self.exit_process()
def process_tx(txn, account): """ process transaction in view of account get basic transaction information, and transaction effects :param txn: :param account: :return: """ tx = txn.tx or txn.transaction or txn meta = txn.meta result = dict() result.date = (tx.date or tx.Timestamp) + 0x386D4380 # unix time result.hash = tx.hash result.type = txn_type(tx, account) # 此处同样有疑问 float 和 Number 的用法 result.fee = str(float(tx.Fee) / 1000000.0) result.result = meta.TransactionResult if meta else 'failed' result.memos = [] if result.type == 'sent': result.counterparty = tx.Destination result.amount = parse_amount(tx.Amount) elif result.type == 'received': result.counterparty = tx.Account result.amount = parse_amount(tx.Amount) elif result.type == 'trusted': result.counterparty = tx.Account result.amount = reverse_amount(tx.LimitAmount, tx.Account) elif result.type == 'trusting': result.counterparty = tx.LimitAmount.issuer result.amount = tx.LimitAmount elif result.type == 'convert': result.spent = parse_amount(tx.SendMax) result.amount = parse_amount(tx.Amount) elif result.type == 'offernew': # result.offertype = tx.Flags & Transaction.flags.OfferCreate.Sell ? 'sell': 'buy' result.gets = parse_amount(tx.TakerGets) result.pays = parse_amount(tx.TakerPays) result.seq = tx.Sequence elif result.type == 'offercancel': result.offerseq = tx.Sequence elif result.type == 'relationset': if account == tx.Target: result.counterparty = tx.Account else: result.counterparty = tx.Target if tx.RelationType == 3: result.relationtype = 'freeze' else: result.relationtype = 'authorize' if account == tx.Target: result.isactive = False else: result.isactive = True result.amount = parse_amount(tx.LimitAmount) elif result.type == 'configcontract': result.params = format_args(tx.Args) if tx.Method == 0: result.method = 'deploy' result.payload = tx.Payload elif tx.Method == 1: result.method = 'call' result.destination = tx.Destination # add memo from encodings.utf_8 import decode if isinstance(tx.Memos, list) and tx.Memos.__len__() > 0: for Memo in tx.Memos: # 此处认定数组Memos中的元素为字典 for key in Memo: try: Memo[key] = decode(__hexToString(Memo[key])) except: Memo[key] = Memo[key] result.memos.append(Memo) result.effects = [] # no effect, return now if not meta or meta.TransactionResult != 'tesSUCCESS': return result # process effects for n in meta.AffectedNodes: node = process_affect_node(n) effect = dict() # now only get offer related effects, need to process other entry type if node.entryType == 'offer': # for new and cancelled offers fieldSet = node.fields sell = node.fields.Flags and LEDGER_FLAGS.offer.Sell # current account offer if node.fields.Account == account: # 1. offer_partially_funded if node.diffType == 'ModifiedNode' or ( node.diffType == 'DeletedNode' and node.fieldsPrev.TakerGets and not is_amount_zero( parse_amount(node.fieldsFinal.TakerGets))): effect.effect = 'offer_partially_funded' effect.counterparty = { 'account': tx.Account, 'seq': tx.Sequence, 'hash': tx.hash } if node.diffType != 'DeletedNode': # no need partially funded must remains offers effect.remaining = not is_amount_zero( parse_amount(node.fields.TakerGets)) else: effect.cancelled = True effect.gets = parse_amount(fieldSet.TakerGets) effect.pays = parse_amount(fieldSet.TakerPays) effect.got = Amount_subtract( parse_amount(node.fieldsPrev.TakerPays), parse_amount(node.fields.TakerPays)) effect.paid = Amount_subtract( parse_amount(node.fieldsPrev.TakerGets), parse_amount(node.fields.TakerGets)) effect.type = 'sold' if sell else effect.type = 'bought' else: # offer_funded, offer_created or offer_cancelled offer effect if node.fieldsPrev.TakerPays: node.fieldsPrev.TakerPays = 'offer_funded' else: node.fieldsPrev.TakerPays = 'offer_cancelled' if node.diffType == 'CreatedNode': effect.effect = 'offer_created' else: effect.effect = node.fieldsPrev.TakerPays if effect.effect == 'offer_funded': fieldSet = node.fieldsPrev effect.counterparty = { 'account': tx.Account, 'seq': tx.Sequence, 'hash': tx.hash } effect.got = Amount_subtract( parse_amount(node.fieldsPrev.TakerPays), parse_amount(node.fields.TakerPays)) effect.paid = Amount_subtract( parse_amount(node.fieldsPrev.TakerGets), parse_amount(node.fields.TakerGets)) if sell: effect.type = sell = 'sold' else: effect.type = sell = 'bought' # 3. offer_created if effect.effect == 'offer_created': effect.gets = parse_amount(fieldSet.TakerGets) effect.pays = parse_amount(fieldSet.TakerPays) if sell: effect.type = sell = 'sell' else: effect.type = sell = 'buy' # 4. offer_cancelled if effect.effect == 'offer_cancelled': effect.hash = node.fields.PreviousTxnID # collect data for cancel transaction type if result.type == 'offercancel': result.gets = parse_amount(fieldSet.TakerGets) result.pays = parse_amount(fieldSet.TakerPays) effect.gets = parse_amount(fieldSet.TakerGets) effect.pays = parse_amount(fieldSet.TakerPays) if sell: effect.type = 'sell' else: effect.type = 'buy' effect.seq = node.fields.Sequence # 5. offer_bought elif tx.Account == account and node.fieldsPrev: effect.effect = 'offer_bought' effect.counterparty = { 'account': node.fields.Account, 'seq': node.fields.Sequence, 'hash': node.PreviousTxnID or node.fields.PreviousTxnID } effect.paid = Amount_subtract( parse_amount(node.fieldsPrev.TakerPays), parse_amount(node.fields.TakerPays)) effect.got = Amount_subtract( parse_amount(node.fieldsPrev.TakerGets), parse_amount(node.fields.TakerGets)) if sell: effect.type = 'bought' else: effect.type = 'sold' # add price if (effect.gets and effect.pays) or (effect.got and effect.paid): created = effect.effect == 'offer_created' and effect.type == 'buy' funded = effect.effect == 'offer_funded' and effect.type == 'bought' cancelled = effect.effect == 'offer_cancelled' and effect.type == 'buy' bought = effect.effect == 'offer_bought' and effect.type == 'bought' partially_funded = effect.effect == 'offer_partially_funded' and effect.type == 'bought' effect.price = get_price(effect, (created or funded or cancelled or bought or partially_funded)) if result.type == 'offereffect' and node.entryType == 'AccountRoot': if node.fields.RegularKey == account: effect.effect = 'set_regular_key' effect.type = 'null' effect.account = node.fields.Account effect.regularkey = account # add effect if effect: if node.diffType == 'DeletedNode' and effect.effect != 'offer_bought': effect.deleted = True result.effects.append(effect) # check cross gateway when parse more effect, specially trust related effects, now ignore it return result