Python string.encode() Examples

The following are 30 code examples for showing how to use string.encode(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module string , or try the search function .

Example 1
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 6 votes vote down vote up
def send_content(self, connection, request_body):
        connection.putheader("Content-Type", "text/xml")

        #optionally encode the request
        if (self.encode_threshold is not None and
            self.encode_threshold < len(request_body) and
            gzip):
            connection.putheader("Content-Encoding", "gzip")
            request_body = gzip_encode(request_body)

        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders(request_body)

    ##
    # Parse response.
    #
    # @param file Stream.
    # @return Response tuple and target method. 
Example 2
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 6 votes vote down vote up
def send_content(self, connection, request_body):
        connection.putheader("Content-Type", "text/xml")

        #optionally encode the request
        if (self.encode_threshold is not None and
            self.encode_threshold < len(request_body) and
            gzip):
            connection.putheader("Content-Encoding", "gzip")
            request_body = gzip_encode(request_body)

        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders(request_body)

    ##
    # Parse response.
    #
    # @param file Stream.
    # @return Response tuple and target method. 
Example 3
Project: aiotuya   Author: frawau   File: provision.py    License: MIT License 6 votes vote down vote up
def sendlinkdata(self):
        delay = 0
        for x in range(30):
            if self.abortbroadcast:
                break

            if delay > 26:
                delay = 6

            for s in self.provisiondata:
                string="\x00"*s
                self.transport.sendto(string.encode(), self.target)
                await aio.sleep(delay/1000.0)

            await aio.sleep(0.2)
            delay += 3

        self.abortbroadcast = False 
Example 4
Project: BinderFilter   Author: dxwu   File: xmlrpclib.py    License: MIT License 6 votes vote down vote up
def send_content(self, connection, request_body):
        connection.putheader("Content-Type", "text/xml")

        #optionally encode the request
        if (self.encode_threshold is not None and
            self.encode_threshold < len(request_body) and
            gzip):
            connection.putheader("Content-Encoding", "gzip")
            request_body = gzip_encode(request_body)

        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders(request_body)

    ##
    # Parse response.
    #
    # @param file Stream.
    # @return Response tuple and target method. 
Example 5
Project: oss-ftp   Author: aliyun   File: xmlrpclib.py    License: MIT License 6 votes vote down vote up
def send_content(self, connection, request_body):
        connection.putheader("Content-Type", "text/xml")

        #optionally encode the request
        if (self.encode_threshold is not None and
            self.encode_threshold < len(request_body) and
            gzip):
            connection.putheader("Content-Encoding", "gzip")
            request_body = gzip_encode(request_body)

        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders(request_body)

    ##
    # Parse response.
    #
    # @param file Stream.
    # @return Response tuple and target method. 
Example 6
Project: pmatic   Author: LarsMichelsen   File: xmlrpclib.py    License: GNU General Public License v2.0 6 votes vote down vote up
def send_content(self, connection, request_body):
        connection.putheader("Content-Type", "text/xml")

        #optionally encode the request
        if (self.encode_threshold is not None and
            self.encode_threshold < len(request_body) and
            gzip):
            connection.putheader("Content-Encoding", "gzip")
            request_body = gzip_encode(request_body)

        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders(request_body)

    ##
    # Parse response.
    #
    # @param file Stream.
    # @return Response tuple and target method. 
Example 7
Project: Splunking-Crime   Author: nccgroup   File: xmlrpclib.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def send_content(self, connection, request_body):
        connection.putheader("Content-Type", "text/xml")

        #optionally encode the request
        if (self.encode_threshold is not None and
            self.encode_threshold < len(request_body) and
            gzip):
            connection.putheader("Content-Encoding", "gzip")
            request_body = gzip_encode(request_body)

        connection.putheader("Content-Length", str(len(request_body)))
        connection.endheaders(request_body)

    ##
    # Parse response.
    #
    # @param file Stream.
    # @return Response tuple and target method. 
Example 8
Project: picoCTF   Author: picoCTF   File: gen_vault.py    License: MIT License 5 votes vote down vote up
def write_file(fname, string):
    with open(fname, 'wb') as out:
        out.write(string.encode('utf-8')) 
Example 9
Project: kbinxml   Author: mon   File: kbinxml.py    License: MIT License 5 votes vote down vote up
def _data_mem_size(self):
        # This is probably better to be done in the parsing/writeout stage...

        data_len = 0
        for e in self.xml_doc.iter(tag=etree.Element):
            t = e.attrib.get('__type')
            if t is None:
                continue

            count = e.attrib.get('__count', 1)
            size = e.attrib.get('__size', 1)
            x = xml_formats[xml_types[t]]
            if x['count'] > 0:
                m = x['count'] * calcsize(x['type']) * count * size
            elif x['name'] == 'bin':
                m = len(e.text) // 2
            else: # string
                # null terminator space
                m = len(e.text.encode(self.encoding)) + 1

            if m <= 4:
                continue

            if x['name'] == 'bin':
                data_len += (m + 1) & ~1
            else:
                data_len += (m + 3) & ~3
        return data_len 
Example 10
Project: kbinxml   Author: mon   File: kbinxml.py    License: MIT License 5 votes vote down vote up
def data_append_string(self, string):
        string = bytes(string.encode(self.encoding) + b'\0')
        self.data_append_auto(string)

    # has its own separate state and other assorted garbage 
Example 11
Project: kbinxml   Author: mon   File: kbinxml.py    License: MIT License 5 votes vote down vote up
def append_node_name(self, name):
        if self.compressed:
            pack_sixbit(name, self.nodeBuf)
        else:
            enc = name.encode(self.encoding)
            self.nodeBuf.append_u8((len(enc) - 1) | 64)
            self.nodeBuf.append_bytes(enc) 
Example 12
Project: kbinxml   Author: mon   File: kbinxml.py    License: MIT License 5 votes vote down vote up
def main():
    if len(sys.argv) != 2:
        print('bin_xml.py file.[xml/bin]')
        exit()

    with open(sys.argv[1], 'rb') as f:
        input = f.read()

    xml = KBinXML(input)
    if KBinXML.is_binary_xml(input):
        stdout.write(xml.to_text().encode('utf-8'))
    else:
        stdout.write(xml.to_binary()) 
Example 13
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def _stringify(string):
        # convert to 7-bit ascii if possible
        try:
            return string.encode("ascii")
        except UnicodeError:
            return string 
Example 14
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def encode(self, out):
            out.write("<value><boolean>%d</boolean></value>\n" % self.value) 
Example 15
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def encode(self, out):
        out.write("<value><dateTime.iso8601>")
        out.write(self.value)
        out.write("</dateTime.iso8601></value>\n") 
Example 16
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def encode(self, out):
        out.write("<value><base64>\n")
        base64.encode(StringIO.StringIO(self.data), out)
        out.write("</base64></value>\n") 
Example 17
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def dump_unicode(self, value, write, escape=escape):
            value = value.encode(self.encoding)
            write("<value><string>")
            write(escape(value))
            write("</string></value>\n") 
Example 18
Project: meddle   Author: glmcdona   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def dump_instance(self, value, write):
        # check for special wrappers
        if value.__class__ in WRAPPERS:
            self.write = write
            value.encode(self)
            del self.write
        else:
            # store instance attributes as a struct (really?)
            self.dump_struct(value.__dict__, write) 
Example 19
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def _stringify(string):
        # convert to 7-bit ascii if possible
        try:
            return string.encode("ascii")
        except UnicodeError:
            return string 
Example 20
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def encode(self, out):
            out.write("<value><boolean>%d</boolean></value>\n" % self.value) 
Example 21
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def encode(self, out):
        out.write("<value><dateTime.iso8601>")
        out.write(self.value)
        out.write("</dateTime.iso8601></value>\n") 
Example 22
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def encode(self, out):
        out.write("<value><base64>\n")
        base64.encode(StringIO.StringIO(self.data), out)
        out.write("</base64></value>\n") 
Example 23
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def dump_unicode(self, value, write, escape=escape):
            write("<value><string>")
            write(escape(value).encode(self.encoding, 'xmlcharrefreplace'))
            write("</string></value>\n") 
Example 24
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def dump_instance(self, value, write):
        # check for special wrappers
        if value.__class__ in WRAPPERS:
            self.write = write
            value.encode(self)
            del self.write
        else:
            # store instance attributes as a struct (really?)
            self.dump_struct(value.__dict__, write) 
Example 25
Project: ironpython2   Author: IronLanguages   File: xmlrpclib.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, uri, transport=None, encoding=None, verbose=0,
                 allow_none=0, use_datetime=0, context=None):
        # establish a "logical" server connection

        if unicode and isinstance(uri, unicode):
            uri = uri.encode('ISO-8859-1')

        # get the url
        import urllib
        type, uri = urllib.splittype(uri)
        if type not in ("http", "https"):
            raise IOError, "unsupported XML-RPC protocol"
        self.__host, self.__handler = urllib.splithost(uri)
        if not self.__handler:
            self.__handler = "/RPC2"

        if transport is None:
            if type == "https":
                transport = SafeTransport(use_datetime=use_datetime, context=context)
            else:
                transport = Transport(use_datetime=use_datetime)
        self.__transport = transport

        self.__encoding = encoding
        self.__verbose = verbose
        self.__allow_none = allow_none 
Example 26
Project: aiotuya   Author: frawau   File: provision.py    License: MIT License 5 votes vote down vote up
def login(self):
        data = {"countryCode": self.region,
                "email": self.email,
                "passwd": md5(self.password.encode()).hexdigest()}
        if self.hmacsecret == None:
            resu = await self._request( 'tuya.m.user.email.password.login',data)
        else:
            resu = await self._request( 'tuya.m.user.email.password.login',data, version="2.0")
        self.sessionid = resu["sid"]
        return resu 
Example 27
Project: aiotuya   Author: frawau   File: provision.py    License: MIT License 5 votes vote down vote up
def register(self):
        data = {"countryCode": self.region,
                "email": self.email,
                "passwd": md5(self.password.encode()).hexdigest()}

        resu = await self._request( 'tuya.m.user.email.register',data)
        self.sessionid = resu["sid"]
        return resu 
Example 28
Project: aiotuya   Author: frawau   File: provision.py    License: MIT License 5 votes vote down vote up
def startbroadcast(self):
        for x in range(144):
            for s in [1, 3, 6, 10]:
                string="\x00"*s
                self.transport.sendto(string.encode(), self.target)
            await aio.sleep(((x % 8) + 33)/1000.0)
            if self.abortbroadcast:
                log.debug("Broadcast aborted")
                break
        log.debug("Broadcast done") 
Example 29
Project: BinderFilter   Author: dxwu   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def _stringify(string):
        # convert to 7-bit ascii if possible
        try:
            return string.encode("ascii")
        except UnicodeError:
            return string 
Example 30
Project: BinderFilter   Author: dxwu   File: xmlrpclib.py    License: MIT License 5 votes vote down vote up
def encode(self, out):
            out.write("<value><boolean>%d</boolean></value>\n" % self.value)