python类encode()的实例源码

xmlrpclib.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, uri, transport=None, encoding=None, verbose=0,
                 allow_none=0, use_datetime=0, context=None):
        # establish a "logical" server connection

        if 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
wxapi.py 文件源码 项目:Server 作者: malaonline 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def wx_signature(data):
    string = '&'.join(['%s=%s' % (key.lower(), data[key]) for key in sorted(data) if data[key] is not None and data[key] is not ''])
    return hashlib.sha1(string.encode('utf-8')).hexdigest()
wxapi.py 文件源码 项目:Server 作者: malaonline 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def wx_sign_for_pay(params):
    content = '&'.join(['%s=%s' % (key, params[key]) for key in sorted(params) if params[key] is not None and params[key] is not ''])
    content += '&key=' + settings.WEIXIN_KEY
    return hashlib.md5(content.encode('utf-8')).hexdigest().upper()
xmlrpclib.py 文件源码 项目:edx-video-pipeline 作者: edx 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def _stringify(string):
        # convert to 7-bit ascii if possible
        try:
            return string.encode("ascii")
        except UnicodeError:
            return string
xmlrpclib.py 文件源码 项目:edx-video-pipeline 作者: edx 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def encode(self, out):
            out.write("<value><boolean>%d</boolean></value>\n" % self.value)
tools.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(
            self,
            payload,
            filename=None,
            content_id=None,
            content_type=None,
                encoding='utf-8'):
            if isinstance(payload, str):
                if filename is None:
                    filename = os.path.basename(payload)
                payload = read_file(payload, 'rb')
            else:
                if filename is None:
                    raise Exception('Missing attachment name')
                payload = payload.read()
            filename = filename.encode(encoding)
            if content_type is None:
                content_type = contenttype(filename)
            self.my_filename = filename
            self.my_payload = payload
            MIMEBase.MIMEBase.__init__(self, *content_type.split('/', 1))
            self.set_payload(payload)
            self['Content-Disposition'] = 'attachment; filename="%s"' % filename
            if content_id is not None:
                self['Content-Id'] = '<%s>' % content_id.encode(encoding)
            Encoders.encode_base64(self)
tools.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def jwt_b64e(string):
        if isinstance(string, unicode):
            string = string.encode('utf-8', 'strict')
        return base64.urlsafe_b64encode(string).strip(b'=')
tools.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 45 收藏 0 点赞 0 评论 0
def jwt_b64d(string):
        """base64 decodes a single bytestring (and is tolerant to getting
        called with a unicode string).
        The result is also a bytestring.
        """
        if isinstance(string, unicode):
            string = string.encode('ascii', 'ignore')
        return base64.urlsafe_b64decode(string + '=' * (-len(string) % 4))
tools.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def generate_token(self, payload):
        secret = self.secret_key
        if self.salt:
            if callable(self.salt):
                secret = "%s$%s" % (secret, self.salt(payload))
            else:
                secret = "%s$%s" % (secret, self.salt)
            if isinstance(secret, unicode):
                secret = secret.encode('ascii', 'ignore')
        b64h = self.cached_b64h
        b64p = self.jwt_b64e(serializers.json(payload))
        jbody = b64h + '.' + b64p
        mauth = hmac.new(key=secret, msg=jbody, digestmod=self.digestmod)
        jsign = self.jwt_b64e(mauth.digest())
        return jbody + '.' + jsign
tools.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def load_token(self, token):
        if isinstance(token, unicode):
            token = token.encode('utf-8', 'strict')
        body, sig = token.rsplit('.', 1)
        b64h, b64b = body.split('.', 1)
        if b64h != self.cached_b64h:
            # header not the same
            raise HTTP(400, u'Invalid JWT Header')
        secret = self.secret_key
        tokend = serializers.loads_json(self.jwt_b64d(b64b))
        if self.salt:
            if callable(self.salt):
                secret = "%s$%s" % (secret, self.salt(tokend))
            else:
                secret = "%s$%s" % (secret, self.salt)
            if isinstance(secret, unicode):
                secret = secret.encode('ascii', 'ignore')
        if not self.verify_signature(body, sig, secret):
            # signature verification failed
            raise HTTP(400, u'Token signature is invalid')
        if self.verify_expiration:
            now = time.mktime(datetime.datetime.utcnow().timetuple())
            if tokend['exp'] + self.leeway < now:
                raise HTTP(400, u'Token is expired')
        if callable(self.before_authorization):
            self.before_authorization(tokend)
        return tokend
searchEngine.py 文件源码 项目:Simple-Search-Engine 作者: HoweZZH 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def getHash(self, string):
        '''return a unique and stable hash value from hashing the website url'''
        m = hashlib.sha1(string.encode('utf-8'))
        return m.hexdigest()
searchEngine.py 文件源码 项目:Simple-Search-Engine 作者: HoweZZH 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def getHash(url):
    '''return a unique and stable hash value from hashing the website url'''
    m = hashlib.sha1(url.encode('utf-8'))
    return m.hexdigest()
sign.py 文件源码 项目:GOKU 作者: bingweichen 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def sign(self):
        string = '&'.join(['%s=%s' % (key.lower(), self.ret[key]) for key in
                           sorted(self.ret)])
        self.ret['signature'] = hashlib.sha1(string.encode('utf-8')).hexdigest()
        return self.ret
tools.py 文件源码 项目:Problematica-public 作者: TechMaz 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(
            self,
            payload,
            filename=None,
            content_id=None,
            content_type=None,
                encoding='utf-8'):
            if isinstance(payload, str):
                if filename is None:
                    filename = os.path.basename(payload)
                payload = read_file(payload, 'rb')
            else:
                if filename is None:
                    raise Exception('Missing attachment name')
                payload = payload.read()
            filename = filename.encode(encoding)
            if content_type is None:
                content_type = contenttype(filename)
            self.my_filename = filename
            self.my_payload = payload
            MIMEBase.MIMEBase.__init__(self, *content_type.split('/', 1))
            self.set_payload(payload)
            self['Content-Disposition'] = 'attachment; filename="%s"' % filename
            if content_id is not None:
                self['Content-Id'] = '<%s>' % content_id.encode(encoding)
            Encoders.encode_base64(self)
tools.py 文件源码 项目:Problematica-public 作者: TechMaz 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def jwt_b64e(string):
        if isinstance(string, unicode):
            string = string.encode('utf-8', 'strict')
        return base64.urlsafe_b64encode(string).strip(b'=')
tools.py 文件源码 项目:Problematica-public 作者: TechMaz 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def jwt_b64d(string):
        """base64 decodes a single bytestring (and is tolerant to getting
        called with a unicode string).
        The result is also a bytestring.
        """
        if isinstance(string, unicode):
            string = string.encode('ascii', 'ignore')
        return base64.urlsafe_b64decode(string + '=' * (-len(string) % 4))
tools.py 文件源码 项目:Problematica-public 作者: TechMaz 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def generate_token(self, payload):
        secret = self.secret_key
        if self.salt:
            if callable(self.salt):
                secret = "%s$%s" % (secret, self.salt(payload))
            else:
                secret = "%s$%s" % (secret, self.salt)
            if isinstance(secret, unicode):
                secret = secret.encode('ascii', 'ignore')
        b64h = self.cached_b64h
        b64p = self.jwt_b64e(serializers.json(payload))
        jbody = b64h + '.' + b64p
        mauth = hmac.new(key=secret, msg=jbody, digestmod=self.digestmod)
        jsign = self.jwt_b64e(mauth.digest())
        return jbody + '.' + jsign
tools.py 文件源码 项目:Problematica-public 作者: TechMaz 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def load_token(self, token):
        if isinstance(token, unicode):
            token = token.encode('utf-8', 'strict')
        body, sig = token.rsplit('.', 1)
        b64h, b64b = body.split('.', 1)
        if b64h != self.cached_b64h:
            # header not the same
            raise HTTP(400, u'Invalid JWT Header')
        secret = self.secret_key
        tokend = serializers.loads_json(self.jwt_b64d(b64b))
        if self.salt:
            if callable(self.salt):
                secret = "%s$%s" % (secret, self.salt(tokend))
            else:
                secret = "%s$%s" % (secret, self.salt)
            if isinstance(secret, unicode):
                secret = secret.encode('ascii', 'ignore')
        if not self.verify_signature(body, sig, secret):
            # signature verification failed
            raise HTTP(400, u'Token signature is invalid')
        if self.verify_expiration:
            now = time.mktime(datetime.datetime.utcnow().timetuple())
            if tokend['exp'] + self.leeway < now:
                raise HTTP(400, u'Token is expired')
        if callable(self.before_authorization):
            self.before_authorization(tokend)
        return tokend
xmlrpclib.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def _stringify(string):
        # convert to 7-bit ascii if possible
        try:
            return string.encode("ascii")
        except UnicodeError:
            return string
xmlrpclib.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def encode(self, out):
            out.write("<value><boolean>%d</boolean></value>\n" % self.value)


问题


面经


文章

微信
公众号

扫码关注公众号