short url的一个python兑现


求如何使用python来实现short url?

perl 程序开发 Perl语言基础

土豆用完了 11 years, 3 months ago

给你一个参考:

import hashlib
    def shorturl(url):
        base32 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z', '0', '1', '2', '3', '4', '5']

        m = hashlib.md5()
        m.update(url)
        hexStr = m.hexdigest()
        hexStrLen = len(hexStr)
        subHexLen = hexStrLen / 8

        output = []
        for i in range(0,subHexLen):
            subHex = '0x'+hexStr[i*8:(i+1)*8]
            res = 0x3FFFFFFF & int(subHex,16)

            out = ''
            for j in range(6):
                val = 0x0000001F & res
                out += (base32[val])
                res = res $amp;>amp;$gt; 5
            output.append(out)
    return output
小胖家的小情人 answered 9 years, 9 months ago

Your Answer