Python codecs 模块,escape_encode() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用codecs.escape_encode()

项目:btc-fpga-miner    作者:marsohod4you    | 项目源码 | 文件源码
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()
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
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')
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:swjtu-pyscraper    作者:Desgard    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
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')
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Sci-Finder    作者:snverse    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Sci-Finder    作者:snverse    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:RPoint    作者:george17-meet    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:habilitacion    作者:GabrielBD    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:flasky    作者:RoseOu    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:threatdetectionservice    作者:flyballlabs    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Indushell    作者:SecarmaLabs    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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
项目:CaScale    作者:Thatsillogical    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:chihu    作者:yelongyu    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:pyetje    作者:rorlika    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:respeaker_virtualenv    作者:respeaker    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:infinite-lorem-ipsum    作者:patjm1992    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Price-Comparator    作者:Thejas-1    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:chalktalk_docs    作者:loremIpsum1771    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
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')
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:python-group-proj    作者:Sharcee    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:covar_me_app    作者:CovarMe    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Callandtext    作者:iaora    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:NeuroMobile    作者:AndrewADykman    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_encode_length(self):
        with support.check_warnings(('unicode_internal codec has been '
                                     'deprecated', DeprecationWarning)):
            # 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
项目:httpninja    作者:irsdl    | 项目源码 | 文件源码
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
项目:httpninja    作者:irsdl    | 项目源码 | 文件源码
def asciiHex(HTTPRequestHolderObjs):
    result = []
    for HTTPRequestHolderObj in HTTPRequestHolderObjs:
        initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest
        initAdditionalInfo = HTTPRequestHolderObj.additionalInfo
        for c in range(0, 256):
            asciiChar = chr(c)
            asciiHex = "{:02x}".format(c)
            additionalInfo = 'ASCII [Code: {} - Char: {} - Hex: {}]'.format(c, codecs.escape_encode(asciiChar), asciiHex)
            if initAdditionalInfo != '':
                additionalInfo = initAdditionalInfo + ' - ' + additionalInfo
            HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiHex=asciiHex),additionalInfo)
            result.append(HTTPRequestHolderObjTemp)
    return result
项目:httpninja    作者:irsdl    | 项目源码 | 文件源码
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
项目:WebAct    作者:CreatCodeBuild    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:PornGuys    作者:followloda    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:godot-python    作者:touilleMan    | 项目源码 | 文件源码
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')
项目:godot-python    作者:touilleMan    | 项目源码 | 文件源码
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')
项目:remoteControlPPT    作者:htwenning    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:ASE-Fall2016    作者:Dai0526    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:TodoListMaker    作者:prakharchoudhary    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_encode_length(self):
        with support.check_warnings(('unicode_internal codec has been '
                                     'deprecated', DeprecationWarning)):
            # 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
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:Sudoku-Solver    作者:ayush1997    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:youtube-trending-music    作者:ishan-nitj    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:compatify    作者:hatooku    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]
项目:ropi    作者:ThumbGen    | 项目源码 | 文件源码
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0]