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
self.alive = False
self.thread_read.join()
python类escape_encode()的实例源码
def _escape_bytes(val):
"""
If val is pure ascii, returns it as a str(), otherwise escapes
into a sequence of escaped bytes:
b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6'
note:
the obvious "v.decode('unicode-escape')" will return
valid utf-8 unicode if it finds them in the string, but we
want to return escaped bytes for any byte, even if they match
a utf-8 string.
"""
# source: http://goo.gl/bGsnwC
import codecs
encoded_bytes, _ = codecs.escape_encode(val)
return encoded_bytes.decode('ascii')
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
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 encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def test_encode_length(self):
# Issue 3739
encoder = codecs.getencoder("unicode_internal")
self.assertEqual(encoder("a")[1], 1)
self.assertEqual(encoder("\xe9\u0142")[1], 2)
self.assertEqual(codecs.escape_encode(br'\x00')[1], 4)
# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
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 encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]