535. Encode and Decode TinyURL

来源:互联网 发布:制作电子报刊的软件 编辑:程序博客网 时间:2024/05/21 04:40
import base64
class Codec:


    def encode(self, longUrl):
        """Encodes a URL to a shortened URL.
        
        :type longUrl: str
        :rtype: str
        """
        bytesString = longUrl.encode(encoding="utf-8")
        encodestr = base64.b64encode(bytesString)
        return 'http://tinyurl.com/' + encodestr


    def decode(self, shortUrl):
        """Decodes a shortened URL to its original URL.
        
        :type shortUrl: str
        :rtype: str
        """
        decodestr = base64.b64decode(shortUrl.split('http://tinyurl.com/')[1])

        return decodestr

编解码一个url地址,这里用base64加密

        
原创粉丝点击