def dataReceived(self, data): self._buffer += data self.resetTimeout() self._buffer_escaped = codecs.escape_encode(self._buffer)[0] if self._busyReceiving: return try: self._busyReceiving = True logdata = { 'msg': 'Probe', 'probe_action': 'Probe Data Recieved', 'DATA': self._buffer_escaped } self.factory.log(logdata, transport=self.transport) if self._buffer_escaped in self.factory.probes: logdata = { 'msg': 'Probe', 'probe_action': 'Probe Response Sent', 'DATA': codecs.escape_encode( self.factory.probes[self._buffer_escaped])[0] } self.factory.log(logdata, transport=self.transport) self.transport.write(self.factory.probes[self._buffer_escaped]) finally: self._busyReceiving = False
def test_escape_encode(self): #sanity checks value, length = codecs.escape_encode("abba") self.assertEqual(value, "abba") self.assertEqual(length, 4) value, length = codecs.escape_encode("ab\a\b\t\n\r\f\vba") self.assertEqual(value, 'ab\\x07\\x08\\t\\n\\r\\x0c\\x0bba') if is_cli: #http://ironpython.codeplex.com/workitem/27899 self.assertEqual(length, 26) else: self.assertEqual(length, 11) value, length = codecs.escape_encode("\\a") self.assertEqual(value, "\\\\a") if is_cli: #http://ironpython.codeplex.com/workitem/27899 self.assertEqual(length, 3) else: self.assertEqual(length, 2) l = [] for i in xrange(256): l.append(chr(i)) value, length = codecs.escape_encode(''.join(l)) self.assertEqual(value, '\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f !"#$%&\\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff') if is_cli: #http://ironpython.codeplex.com/workitem/27899 self.assertEqual(length, 735) else: self.assertEqual(length, 256)
async def get_storage_data(self, mode: Optional[str] = 'full') -> str: number_of_records = [0] data = {} keys_list: list = [] def add_db_key(record_tuple): key = bytes(record_tuple[0][2]) keys_list.append(key) number_of_records[0] = number_of_records[0] + 1 if number_of_records[0] > 3000: return False scan = self.client.scan(self.namespace, self.aero_set) scan.foreach(add_db_key) if number_of_records[0] > 3000: return json.dumps('database too big (> 3000 records)') keys_list.sort() for item in keys_list: item_value = await self.get_value(item) item_name = codecs.escape_encode(item)[0].decode( 'ascii') # type: ignore if mode == 'short': # Mode 'short' - exclude blockchain packages, blocks, and merkle nodes. if item_name.startswith(SHORT_MODE_EXCLUDE): continue item_value_str = None if item_value is None else \ codecs.escape_encode(item_value)[0].decode('ascii') # type: ignore data[item_name] = item_value_str return json.dumps(data, indent=4, sort_keys=True)
def reader(self): """loop forever and copy serial->socket""" while self.alive: try: if controlEvent.isSet() == False: break data = self.serial.read(1) # read one, blocking n = self.serial.inWaiting() # look if there is more if n: data = data + self.serial.read(n) # and get as much as possible if data: # the spy shows what's on the serial port, so log it before converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() # if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string # data = net_newline.join(data.split(ser_newline)) # escape outgoing data when needed (Telnet IAC (0xff) character) self._write_lock.acquire() try: # Only send data to socket if it is in active state if controlEvent.isSet() == True: self.socket.sendall(data) # send it over TCP except Exception, msg: sys.stderr.write("reader Socket ERROR IOError: %s\n" % msg) finally: self._write_lock.release()
def _escape_strings(val): """If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes: b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6' and escapes unicode objects into a sequence of escaped unicode ids, e.g.: '4\\nV\\U00043efa\\x0eMXWB\\x1e\\u3028\\u15fd\\xcd\\U0007d944' note: the obvious "v.decode('unicode-escape')" will return valid utf-8 unicode if it finds them in bytes, but we want to return escaped bytes for any byte, even if they match a utf-8 string. """ if isinstance(val, bytes): if val: # source: http://goo.gl/bGsnwC encoded_bytes, _ = codecs.escape_encode(val) return encoded_bytes.decode('ascii') else: # empty bytes crashes codecs.escape_encode (#1087) return '' else: return val.encode('unicode_escape').decode('ascii')
def writer(self): """loop forever and copy socket->serial""" while self.alive: try: if controlEvent.isSet() == False: self.alive = False self.thread_read.join() break data = self.socket.recv(1024) if not data: break # if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string # data = ser_newline.join(data.split(net_newline)) # Only send data to serial if it is in active state if controlEvent.isSet() == True: self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.timeout: continue except socket.error, msg: sys.stderr.write("writer socket.error: %s\n" % msg) # probably got disconnected break except IOError, msg: sys.stderr.write("writer IOError: %s\n" % msg)
def escape_string_with_python_escapes( string_to_escape: Optional[str]) -> Optional[str]: if not string_to_escape: return None escaped_bytes, _ = codecs.escape_encode( string_to_escape.encode()) # type: ignore # C-Api level return "'" + escaped_bytes.decode("utf-8") + "'" # type: ignore
def reader(self): """loop forever and copy serial->socket""" #sys.stdout.write("got some data....") while self.alive: data = self.serial.read(1) # read one, blocking n = self.serial.inWaiting() # look if there is more if n: data = data + self.serial.read( n) # and get as much as possible if data: # append the currently-read data onto the historic data array self._historic_data_lock.acquire() self.historic_data += data self._historic_data_lock.release() # the spy shows what's on the serial port, so log it before converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string data = net_newline.join(data.split(ser_newline)) for client in self.client_queue: client.send_data(data) self.alive = False
def reader(self): """loop forever and copy serial->socket""" while self.alive: try: if controlEvent.isSet() == False: break data = self.serial.read(1) # read one, blocking n = self.serial.inWaiting() # look if there is more if n: data = data + self.serial.read( n) # and get as much as possible if data: # the spy shows what's on the serial port, so log it before converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() #if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string #data = net_newline.join(data.split(ser_newline)) # escape outgoing data when needed (Telnet IAC (0xff) character) self._write_lock.acquire() try: # Only send data to socket if it is in active state if controlEvent.isSet() == True: self.socket.sendall(data) # send it over TCP except Exception, msg: sys.stderr.write('reader Socket ERROR IOError: %s\n' % msg) finally: self._write_lock.release()
def _escape_strings(val): """If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes: b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6' and escapes unicode objects into a sequence of escaped unicode ids, e.g.: '4\\nV\\U00043efa\\x0eMXWB\\x1e\\u3028\\u15fd\\xcd\\U0007d944' note: the obvious "v.decode('unicode-escape')" will return valid utf-8 unicode if it finds them in bytes, but we want to return escaped bytes for any byte, even if they match a utf-8 string. """ if isinstance(val, bytes): if val: # source: http://goo.gl/bGsnwC encoded_bytes, _ = codecs.escape_encode(val) return encoded_bytes.decode("ascii") else: # empty bytes crashes codecs.escape_encode (#1087) return "" else: return val.encode("unicode_escape").decode("ascii")
def reader(self): """loop forever and copy serial->socket""" #sys.stdout.write("got some data....") while self.alive: data = self.serial.read(1) # read one, blocking n = self.serial.inWaiting() # look if there is more if n: data = data + self.serial.read(n) # and get as much as possible if data: # append the currently-read data onto the historic data array self._historic_data_lock.acquire() self.historic_data += data self._historic_data_lock.release() # the spy shows what's on the serial port, so log it before converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string data = net_newline.join(data.split(ser_newline)) for client in self.client_queue: client.send_data(data) self.alive = False
def reader(self): """loop forever and copy serial->socket""" while self.alive: try: data = self.serial.read(1) # read one, blocking n = self.serial.inWaiting() # look if there is more if n: data = data + self.serial.read( n) # and get as much as possible if data: # the spy shows what's on the serial port, so log it before converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string data = net_newline.join(data.split(ser_newline)) # escape outgoing data when needed (Telnet IAC (0xff) character) self._write_lock.acquire() #print "data got from imu:", data try: #self.socket.sendall(data) # send it over UDP self.udp_sock.sendto(data, self.UDP_addr) finally: self._write_lock.release() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break except OSError, msg: sys.stderr.write('OSError: %s\n' % msg) continue
def _FormatMessage(item, lang): """Format a single <message> element.""" message = item.ws_at_start + item.Translate(lang) + item.ws_at_end # Output message with non-ascii chars escaped as octal numbers C's grammar # allows escaped hexadecimal numbers to be infinite, but octal is always of # the form \OOO. Python 3 doesn't support string-escape, so we have to jump # through some hoops here via codecs.escape_encode. # This basically does: # - message - the starting string # - message.encode(...) - convert to bytes # - codecs.escape_encode(...) - convert non-ASCII bytes to \x## escapes # - (...).decode() - convert bytes back to a string message = codecs.escape_encode(message.encode('utf-8'))[0].decode('utf-8') # an escaped char is (\xHH)+ but only if the initial # backslash is not escaped. not_a_backslash = r"(^|[^\\])" # beginning of line or a non-backslash char escaped_backslashes = not_a_backslash + r"(\\\\)*" hex_digits = r"((\\x)[0-9a-f]{2})+" two_digit_hex_num = re.compile(r"(?P<escaped_backslashes>%s)(?P<hex>%s)" % (escaped_backslashes, hex_digits)) message = two_digit_hex_num.sub(_HexToOct, message) # unescape \ (convert \\ back to \) message = message.replace('\\\\', '\\') message = message.replace('"', '\\"') message = util.LINEBREAKS.sub(r'\\n', message) name_attr = item.GetTextualIds()[0] return '\n case %s:\n return "%s";' % (name_attr, message)
def reader(self): """loop forever and copy serial->socket""" while self.alive: try: data = self.serial.read(1) # read one, blocking n = self.serial.inWaiting() # look if there is more if n: data = data + self.serial.read(n) # and get as much as possible if data: # the spy shows what's on the serial port, so log it before converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string data = net_newline.join(data.split(ser_newline)) # escape outgoing data when needed (Telnet IAC (0xff) character) self._write_lock.acquire() try: self.socket.sendall(data) # send it over TCP finally: self._write_lock.release() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break
def string_to_query(self, value): value = value.strip() if value[0] == '{' and value[-1] == '}': consecutive = [''] value = value[1:-1].strip() hexdigs = 'ABCDEFabcdef0123456789' value = value.replace(" ", "") vals = re.split('\[.*?\]', value) for value in vals: value = value.strip() consecutive.append('') for c in [value[i:i+2] for i in range(0, len(value), 2)]: if len(c) == 2 and c != '??' and (c[0] == '?' or c[1] == '?'): consecutive[-1] += c elif len(c) == 2 and c[0] in hexdigs and c[1] in hexdigs: consecutive[-1] += c else: consecutive.append('') qs = ' & '.join('{' + c + '}' for c in consecutive if len(c) >= 6) if not qs: qs = '""' return '(' + qs + ')' elif value[0] == "\"" and value[-1] == "\"": # this will encode non-ascii characters into \x entities, so UrsaDB could parse them inner_val = codecs.escape_encode(value[1:-1].encode('utf-8'))[0] inner_val = inner_val.decode('ascii').replace('\\\\', '\\').replace('\\\'', '\'') return "\"" + inner_val + "\"" else: assert False
def writer(self): """loop forever and copy socket->serial""" while self.alive: try: if controlEvent.isSet() == False: self.alive = False self.thread_read.join() break data = self.socket.recv(1024) if not data: break #if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string #data = ser_newline.join(data.split(net_newline)) # Only send data to serial if it is in active state if controlEvent.isSet() == True: self.serial.write( data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.timeout: continue except socket.error, msg: sys.stderr.write('writer socket.error: %s\n' % msg) # probably got disconnected break except IOError, msg: sys.stderr.write('writer IOError: %s\n' % msg)
def _bytes_to_ascii(val): if val: # source: http://goo.gl/bGsnwC encoded_bytes, _ = codecs.escape_encode(val) return encoded_bytes.decode("ascii") else: # empty bytes crashes codecs.escape_encode (#1087) return ""
def writer(self): """loop forever and copy socket->serial""" remainder = "" while self.alive: try: data = self.socket.recv(1024) print "test" if not data: break data = remainder + data if remainder: print "prepending remainder " + str( remainder) + " to " + str(data) remainder = "" if not (data.endswith('\n')): pos = data.rfind('\n') if pos != -1: remainder = data[pos + 1:] data = data[0:pos + 1] else: remainder = data data = "" else: remainder = "" print "data is", repr(data) if data.endswith('\n'): print "writing!" + data # these commands are special if data.find('shutdown') != -1: system('shutdown -h now &') elif data.find('adhocwireless') != -1: system('~pi/switch_to_adhoc.pl &') break elif data.find('setvideomode') != -1: newmode = data[data.find('setvideomode') + len('setvideomode'):] newmode = newmode[:newmode.find('\n')] print '~pi/switch_video_mode.sh "' + newmode + '" &' system('~pi/switch_video_mode.sh "' + newmode + '" &') print "done" break elif data.find('normalwireless') != -1: system('~pi/switch_to_normal.sh &') break print data, len(data) self.serial.write( data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break
def myesc(s): if sys.version_info >= (3, 0): # workaround https://bugs.python.org/issue25270 if not s: return s b, n = codecs.escape_encode(s.encode('utf-8')) return b.decode('ascii') else: return s.encode('utf-8').encode('string_escape')
def convert(self, source_txt): text = "" try: import zlib import codecs text = zlib.compress(source_txt.encode()) return codecs.escape_encode(text)[0].decode() except: sublime.error_message("Zip failed.")
def _compile_thread(self, frame): next = iter(frame).next try: while True: entry = next() if callable(entry): if entry == self.instruction_output_text: label = self.create_label() self.doctree.write('\t.word %s, %s\n' % ( self.create_asm_label('__write_text'), self.create_asm_label(label))) self._compile_remember('__write_text') # output the text separately frame = NativeFrame(label) frame.chapter = self.doctree.chapter_name self.target_namespace[label] = frame self._compile_remember(label) text = next() frame.append(self.instruction_output_text) frame.append('\t.asciiz "%s"\n' % (codecs.escape_encode(text)[0],)) elif entry == self.instruction_literal: value = next() if isinstance(value, Frame): self.doctree.write('\t.word %s, %s\n' % ( self.create_asm_label('LIT'), self.create_asm_label(value.name),)) else: self.doctree.write('\t.word %s, %-6s ; 0x%04x\n' % ( self.create_asm_label('LIT'), value, value & 0xffff)) self._compile_remember('LIT') elif entry == self.instruction_seek: # branch needs special case as offset needs to be recalculated offset = next() self.doctree.write('\t.word %s, %s\n' % (self.create_asm_label('BRANCH'), offset*2)) self._compile_remember('BRANCH') elif entry == self.instruction_branch_if_false: # branch needs special case as offset needs to be recalculated offset = next() self.doctree.write('\t.word %s, %s\n' % (self.create_asm_label('BRANCH0'), offset*2)) self._compile_remember('BRANCH0') elif hasattr(entry, 'rpn_name'): # for built-ins just take the name of the function self.doctree.write('\t.word %s\n' % self.create_asm_label(entry.rpn_name.upper())) self._compile_remember(entry.rpn_name) elif isinstance(entry, Frame): self.doctree.write('\t.word %s\n' % self.create_asm_label(entry.name)) self._compile_remember(entry.name) else: raise ValueError('Cross compilation undefined for %r' % entry) else: self.doctree.write('\t.word %r\n' % (entry,)) #~ raise ValueError('Cross compilation undefined for %r' % entry) except StopIteration: pass
def writer(self): """loop forever and copy socket->serial""" remainder = "" while self.alive: try: data = self.socket.recv(1024) print "test" if not data: break data = remainder + data if remainder: print "prepending remainder " + str(remainder) + " to " + str(data) remainder = "" if not(data.endswith('\n')): pos = data.rfind('\n') if pos != -1: remainder = data[pos+1:] data = data[0:pos+1] else: remainder = data data = "" else: remainder = "" print "data is", repr(data) if data.endswith('\n'): print "writing!" + data # these commands are special if data.find('shutdown') != -1: system('shutdown -h now &') elif data.find('adhocwireless') != -1: system('~pi/switch_to_adhoc.pl &') break elif data.find('setvideomode') != -1: newmode = data[data.find('setvideomode')+len('setvideomode'):] newmode = newmode[:newmode.find('\n')] print '~pi/switch_video_mode.sh "' + newmode + '" &' system('~pi/switch_video_mode.sh "' + newmode + '" &') print "done" break elif data.find('normalwireless') != -1: system('~pi/switch_to_normal.sh &') break print data, len(data) self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break
def OnTextPopupMenuItemFind(self, event): self.TextFindStart = None data = wx.FindReplaceData() start, end = self.textView.getSelection() if start is not None and end is not None: size = min(100, end - start + 1) data.SetFindString(codecs.escape_encode(self.textView.model.bin.read(start, size))[0]) dlg = wx.FindReplaceDialog(self, data, "Find", wx.FR_NOUPDOWN|wx.FR_NOWHOLEWORD) dlg.data = data # save a reference to it... dlg.Show(True) event.Skip()
def connectionMade(self): logdata = {'msg': 'Probe', 'probe_action': 'Connection Established'} self.factory.log(logdata, transport=self.transport) if 'Null Probe' in self.factory.probes: self.transport.write(self.factory.probes['Null Probe']) logdata = { 'msg': 'Probe', 'probe_action': 'Probe Response Sent', 'DATA': codecs.escape_encode(self.factory.probes['Null Probe'])[0] } self.factory.log(logdata, transport=self.transport)
def unicodeChar(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for u in range(0, 65535): unicodeChar = int2bytes(u) additionalInfo = 'UNICODE [Code: {} - Char: {}]'.format(u, codecs.escape_encode(unicodeChar)) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiChar=asciiChar),additionalInfo) result.append(HTTPRequestHolderObjTemp) return result
def asciiChar(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for c in range(0, 256): asciiChar = chr(c) additionalInfo = 'ASCII [Code: {} - Char: {}]'.format(c, codecs.escape_encode(asciiChar)) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiChar=asciiChar),additionalInfo) result.append(HTTPRequestHolderObjTemp) return result
def writer(self): """loop forever and copy socket->serial""" while self.alive: try: data = self.socket.recv(1024) if not data: break if self.net_newline: splitted_data = data.split(net_newline) else: sys.stderr.write('ERROR: net newline is not set') break data = map(lambda x: x.replace(net_newline, "")) commands = {"forward": "$KE,REL,1,1"+ser_newline + "$KE,REL,2,1"+ser_newline + "$KE,REL,3,1"+ser_newline + "$KE,REL,4,1"+ser_newline, "back": "$KE,REL,1,0"+ser_newline + "$KE,REL,2,0"+ser_newline + "$KE,REL,3,0"+ser_newline + "$KE,REL,4,0"+ser_newline, "left": "$KE,REL,1,1"+ser_newline + "$KE,REL,2,0"+ser_newline + "$KE,REL,3,1"+ser_newline + "$KE,REL,4,0"+ser_newline, "right": "$KE,REL,1,0"+ser_newline + "$KE,REL,2,1"+ser_newline + "$KE,REL,3,0"+ser_newline + "$KE,REL,4,1"+ser_newline, "stop": "$KE,REL,1,0"+ser_newline + "$KE,REL,2,0"+ser_newline + "$KE,REL,3,0"+ser_newline + "$KE,REL,4,0"+ser_newline, } for cmd in data: self.serial.write(commands[cmd]); #if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string # data = ser_newline.join(data.split(net_newline)) #self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break
def build(self, pre=None, shortest=False): """Build the ``Quote`` instance :param list pre: The prerequisites list :param bool shortest: Whether or not the shortest reference-chain (most minimal) version of the field should be generated. """ res = super(Q, self).build(pre, shortest=shortest) if self.escape: return self._repr_escape(res) elif self.html_js_escape: res, _ = codecs.escape_encode(res) return (b"'" + res.replace(b"<", b"\\x3c").replace(b">", b"\\x3e") + b"'") else: return b"".join([self.quote, res, self.quote])
def cross_compile(self, forth, compiler, doctree): label = self.create_label() asmlabel = self.create_asm_label(label) self.doctree.write("\t.word %s\n" % self.create_asm_label(label)) # output the text separately frame = MPS430_Native(label) # This will not work! # The interpreter executes native frames to compile them using ." to # output text data. The simple parser can not handle double quotes around # text as originally coded. frame.append('." %s .asciiz "%s""' % (asmlabel, codecs.escape_encode(self.value)[0])) forth.target_namespace[label] = frame compiler.compile_remember(label)
def encrypt_fn(cipher, data): # data_dec = codecs.escape_decode(data)[0] if debbug: print_current_fn() # print "data:", data # print "data len:", len(data) # print "data_dec:", data_dec aux = ">" * (32 - len(data)) enc_data = cipher.encrypt(data + aux) # AES decrypt if debbug: print "data", data print "enc_data:", enc_data # print "serv:", serv return codecs.escape_encode(enc_data)[0]
def test_escape_encode(self): tests = [ (b'', (b'', 0)), (b'foobar', (b'foobar', 6)), (b'spam\0eggs', (b'spam\\x00eggs', 9)), (b'a\'b', (b"a\\'b", 3)), (b'b\\c', (b'b\\\\c', 3)), (b'c\nd', (b'c\\nd', 3)), (b'd\re', (b'd\\re', 3)), (b'f\x7fg', (b'f\\x7fg', 3)), ] for data, output in tests: self.assertEqual(codecs.escape_encode(data), output) self.assertRaises(TypeError, codecs.escape_encode, 'spam') self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
def cross_compile(self, forth, compiler, doctree): label = self.create_label() asmlabel=self.create_asm_label(label) self.doctree.write('\t.word %s\n' % self.create_asm_label(label)) # output the text separately frame = MPS430_Native(label,) # This will not work! # The interpreter executes native frames to compile them using ." to # output text data. The simple parser can not handle double quotes around # text as originally coded. frame.append('." %s .asciiz "%s"\"' \ % (asmlabel,codecs.escape_encode(self.value)[0],)) forth.target_namespace[label] = frame compiler.compile_remember(label)
def asciiChar(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for c in range(0, 256): asciiChar = chr(c) additionalInfo = 'ASCII [Code: {} - Char: {}]'.format( c, codecs.escape_encode(asciiChar)) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder( Template(initHTTPReq).safe_substitute(asciiChar=asciiChar), additionalInfo) result.append(HTTPRequestHolderObjTemp) return result
def unicodeChar(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for u in range(0, 65535): unicodeChar = int2bytes(u) additionalInfo = 'UNICODE [Code: {} - Char: {}]'.format( u, codecs.escape_encode(unicodeChar)) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder( Template(initHTTPReq).safe_substitute(asciiChar=asciiChar), additionalInfo) result.append(HTTPRequestHolderObjTemp) return result
def press_button(self, button): logger.info('Received button press ({0}, {1})'.format(self.name, button)) if button in self.buttons: # See what communication mode is used for this device and send the # command accordingly. if self.mode == MODE_RS232: if not self.connection: if self.reopen_connection() != 0: return 1 logger.debug('Sending {0} command to device {1} ({2}, {3}, {4}, {5}, {6})'. format(codecs.escape_encode(self.buttons[button])[0], self.name, self.terminal, self.baud, self.bytesize, self.stopbits, self.parity)) try: self.connection.write(self.buttons[button]) except serial.SerialException as e: logger.error('Unable to write to rs232 connection for device {0}'.format(self.name)) logger.error('{0}'.format(e)) return 1 return 0 elif self.mode == MODE_HTTP: logger.debug('Sending {0} to device {1}'.format(self.buttons[button], self.name)) try: f = urllib2.urlopen(url=self.buttons[button], timeout=self.timeout) logger.debug('----- Received Response -----') logger.debug(f.read()) except urllib2.URLError as e: # We could have sent the request, but just not received the response back # before the timeout. We just log an error on the server side but return # success to the client, as the action may have taken place. This is really # a server side error anyway, and there is nothing that the client can do # about it. logger.error('Error accessing URL:') logger.error(e.reason); return 0 else: logger.warning('Unknown communication mode in press_button ({0}, {1})'.format(self.name, self.mode)) return 1 else: logger.warning('Received request for non-existent button ({0}, {1})'.format(self.name, button)) return 1
def query_llmnr(self, query, length): # Configure the socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32) sock.settimeout(2) sock.bind(('0.0.0.0', 0)) # Configure the destination address and packet data mcast_addr = '224.0.0.252' mcast_port = 5355 if query == "random": query = ''.join(random.choice(string.lowercase) for i in range(16)) llmnr_packet_data = "\x31\x81\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00" + chr( length) + query + "\x00\x00\x01\x00\x01" # Send the LLMNR query sock.sendto(llmnr_packet_data, (mcast_addr, mcast_port)) event = self.gen_event() event['dst_port'] = 5355 # Check if a response was received while 1: try: resp = sock.recvfrom(1024) # If a response was received, parse the results into an event if resp: event["logdata"] = { "msg": "Poisoned Response Received - LLMNR" } event['src_ip'] = str(resp[1][0]) event['src_port'] = str(resp[1][1]) event['logdata']["responder_ip"] = str(resp[1][0]) event['logdata']["query"] = query event['logdata']["response"] = str(resp[0][13:(13 + length)]) event['logdata']["DATA"] = codecs.escape_encode(resp[0]) event['logdata']["protocol"] = "llmnr" sock.close() self.events.append(event) break # If no response, wait for the socket to timeout and close it except socket.timeout: sock.close() break
def writer(self): """loop forever and copy socket->serial""" while self.alive: try: data = self.socket.recv(1024) if not data: break if self.net_newline: splitted_data = data.split(net_newline) else: sys.stderr.write('ERROR: net newline is not set') break data = map(lambda x: x.replace(net_newline, "")) commands = { "forward": "$KE,REL,1,1" + ser_newline + "$KE,REL,2,1" + ser_newline + "$KE,REL,3,1" + ser_newline + "$KE,REL,4,1" + ser_newline, "back": "$KE,REL,1,0" + ser_newline + "$KE,REL,2,0" + ser_newline + "$KE,REL,3,0" + ser_newline + "$KE,REL,4,0" + ser_newline, "left": "$KE,REL,1,1" + ser_newline + "$KE,REL,2,0" + ser_newline + "$KE,REL,3,1" + ser_newline + "$KE,REL,4,0" + ser_newline, "right": "$KE,REL,1,0" + ser_newline + "$KE,REL,2,1" + ser_newline + "$KE,REL,3,0" + ser_newline + "$KE,REL,4,1" + ser_newline, "stop": "$KE,REL,1,0" + ser_newline + "$KE,REL,2,0" + ser_newline + "$KE,REL,3,0" + ser_newline + "$KE,REL,4,0" + ser_newline, } for cmd in data: self.serial.write(commands[cmd]) #if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string # data = ser_newline.join(data.split(net_newline)) #self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break
def _generate_value_str(self, value, indent=0): tabs = '\t' * indent if isinstance(value, construct.ListContainer): return '- ' + tabs + ('\n' + '- ' + tabs).join( self._generate_value_str(value_, indent=indent + 1).lstrip() for value_ in value) elif isinstance(value, construct.Container): # NOTE: must use items() instead of iteritems() to keep order. return tabs + ('\n' + tabs).join('{}: \n{}'.format( name, self._generate_value_str(value_, indent=indent + 1)) for name, value_ in value.items()) elif isinstance(value, bytes): # Escape unprintable bytes with "\x" notation. # (using codecs necessary to get this to work in both python 2 and 3) return tabs + codecs.escape_encode(value)[0].decode('utf-8') else: return tabs + '{}'.format(value)
def _generate_value_str(self, value, indent=0): tabs = '\t' * indent if isinstance(value, construct.ListContainer): return '- ' + tabs + ('\n' + '- ' + tabs).join( self._generate_value_str(value_, indent=indent+1).lstrip() for value_ in value) elif isinstance(value, construct.Container): # NOTE: must use items() instead of iteritems() to keep order. return tabs + ('\n' + tabs).join( '{}: \n{}'.format(name, self._generate_value_str(value_, indent=indent+1)) for name, value_ in value.items() if not name.startswith('_')) elif isinstance(value, bytes): # Escape unprintable bytes with "\x" notation. # (using codecs necessary to get this to work in both python 2 and 3) return tabs + codecs.escape_encode(value)[0].decode('utf-8') else: return tabs + '{}'.format(value)
def writer(self): """loop forever and copy socket->serial""" remainder = "" while self.alive: try: data = self.socket.recv(1024) if not data: break data = remainder + data if remainder: print "prepending remainder " + str(remainder) + " to " + str(data) remainder = "" if not (data.endswith("\n")): pos = data.rfind("\n") if pos != -1: remainder = data[pos + 1 :] data = data[0 : pos + 1] else: remainder = data data = "" else: remainder = "" if data.endswith("\n"): print "writing!" + data # these commands are special if data.find("shutdown") != -1: system("shutdown -h now &") elif data.find("adhocwireless") != -1: system("~pi/switch_to_adhoc.pl &") break elif data.find("normalwireless") != -1: system("~pi/switch_to_normal.sh &") break self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write("ERROR: %s\n" % msg) # probably got disconnected break
def writer(self): """loop forever and copy socket->serial""" while self.alive: try: data = self.socket.recv(1024) if not data: break if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string data = ser_newline.join(data.split(net_newline)) self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break
def commands(self) -> str: """ It opens the template and renders it via jinja2 """ with open(os.path.join(os.getcwd(), self.src), 'r') as content_file: template = Template(content_file.read()) template_output = template.render( config=config, host=self.host, executor=self ) # Adds line endings and escaping quotes text = codecs.escape_encode(bytes(template_output, "utf-8"))[0].decode("utf-8").replace("'", "\'") pipe = '| ' if self.sudo: pipe += 'sudo ' pipe += f'tee {self.dest}' return 'printf \'' + text + f'\' {pipe}'
def text(self): if self.m_tableWidget.rowCount() == 0: return "" root = et.Element("revs") for row in range(self.m_tableWidget.rowCount()): attrs = dict() revItem = self.m_tableWidget.item(row, 1) if revItem is None: return "" attrs["rev"] = revItem.text() dateItem = self.m_tableWidget.item(row, 2) if dateItem is None: return "" attrs["date"] = dateItem.text() typeItem = self.m_tableWidget.item(row, 0) if typeItem is None: return "" elem = et.SubElement(root, typeItem.text(), **attrs) xml = codecs.escape_encode(et.tostring(root))[0].decode("utf-8") return xml
def encode_string(text): return codecs.escape_encode(text.encode('UTF-8'))[0].decode().replace('\\x', '=').upper()
'import_descriptor_loader', ] # NOTE: MessageField is missing because message fields cannot have # a default value at this time. # TODO(rafek): Support default message values. # # Map to functions that convert default values of fields of a given type # to a string. The function must return a value that is compatible with # FieldDescriptor.default_value and therefore a unicode string. _DEFAULT_TO_STRING_MAP = { messages.IntegerField: unicode, messages.FloatField: unicode, messages.BooleanField: lambda value: value and u'true' or u'false', messages.BytesField: lambda value: codecs.escape_encode(value)[0], messages.StringField: lambda value: value, messages.EnumField: lambda value: unicode(value.number), } class EnumValueDescriptor(messages.Message): """Enum value descriptor. Fields: name: Name of enumeration value. number: Number of enumeration value. """ # TODO(rafek): Why are these listed as optional in descriptor.proto. # Harmonize?
def get_status(self, command): logger.info('Received status request ({0}, {1})'.format(self.name, command)) if command in self.status_cmds: # See what communication mode is used for this device and send the # command accordingly. if self.mode == MODE_RS232: if not self.connection: if self.reopen_connection() != 0: return '' logger.debug('Sending {0} command to device {1} ({2}, {3}, {4}, {5}, {6})'. format(codecs.escape_encode(self.status_cmds[command].command)[0], self.name, self.terminal, self.baud, self.bytesize, self.stopbits, self.parity)) try: self.connection.write(self.status_cmds[command].command) except serial.SerialException as e: logger.error('Unable to write to rs232 connection for device {0}'.format(self.name)) logger.error('{0}'.format(e)) return '' try: if (self.status_cmds[command].response_size == 0): self.connection.flushInput() # Use serial io for universal readline support response = self.sio.readline() else: response = self.connection.read(size=self.status_cmds[command].response_size) except serial.SerialException as e: logger.error('Unable to read response from device {0}'.format(self.name)) logger.error('{0}'.format(e)) return '' logger.debug('----- Received Response -----') logger.debug('0x{0}'.format(binascii.hexlify(response))) # Parse the result to find out what value we should return. retval = self.status_cmds[command].get_result(response) logger.debug('Returning value ({0})'.format(retval)) return retval elif self.mode == MODE_HTTP: logger.debug('Sending {0} to device {1}'.format(self.status_cmds[command], self.name)) try: f = urllib2.urlopen(url=self.status_cmds[command], timeout=self.timeout) # NGK TODO - responses from http are going to be very different from rs232. # Need to determine how we want to set up response config and deal with # parsing for http. logger.debug('----- Received Response -----') logger.debug(f.read()) except urllib2.URLError as e: # We could have sent the request, but just not received the response back # before the timeout. We just log an error on the server side and return # an empty response to the client. logger.error('Error accessing URL:') logger.error(e.reason); # NGK TODO - return actual response here after implementing http response handling. return '' else: logger.warning('Unknown communication mode in get_status ({0}, {1})'.format(self.name, self.mode)) return '' else: logger.warning('Received request for non-existent status command ({0}, {1})'.format(self.name, command)) return ''
def encode(self, input, final = False): return codecs.escape_encode(input, self.errors)[0]
def convert_bytes(hex): bb = binascii.a2b_hex(hex) encoded, _ = codecs.escape_encode(bb) return encoded.decode('utf-8').replace('"', '\\"')
def create_node(index, label): label, _ = escape_encode(label.encode('utf-8')) return '\t{NAME} [label="{LABEL}"];\n'.format(NAME=get_name(index), LABEL=label)
def __init__(self, address_space, tag = None, **kwargs): scan.ScannerCheck.__init__(self, address_space, **kwargs) import codecs self.tag = codecs.escape_encode(bytes(tag, sys.getdefaultencoding()))[0]