Home > Backend Development > Python Tutorial > python uses two methods to implement url short connection

python uses two methods to implement url short connection

高洛峰
Release: 2016-10-18 14:03:41
Original
1357 people have browsed it

Almost all WeChat websites provide URL shortening services. The principle is to generate a string from a URL address according to a certain algorithm, and then add it to a short domain name to form a new URL address. This will be stored in the database. Short address and original address. When the user clicks on this new short address, the short address service will read the original address from the database based on the several strings after the short domain name and then jump to the page.

For example, the url in Sina Weibo is http://t.cn/xxxxxxx t.cn is its domain name, followed by a 7-digit string.

Method 1: Use the hash library to customize the algorithm

Because it will be messy to display a URL that is too long in the text, or use the method of omitting the display, or using the method of short URL.

In order to facilitate the statistics of clicks and Carry out content filtering. Implemented a method of generating short url values.

In order to prevent your hash value from being cracked, you can add your own salt when generating the md5 value.

In this way, your code_map cannot be cracked The original url.

In order to make the results more random, save the second bit that is not used in each cycle into e. This can make the result conflict rate smaller.

#引入哈希库
import hashlib 
       
def get_md5(s): 
    s = s.encode('utf8') if isinstance(s, unicode) else s 
    m = hashlib.md5() 
    m.update(s) 
    return m.hexdigest() 
       
code_map = ( 
           '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' , 
           '6' , '7' , '8' , '9' , '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'
            ) 
       
       
def get_hash_key(long_url): 
    hkeys = [] 
    hex = get_md5(long_url) 
    for i in xrange(0, 4): 
        n = int(hex[i*8:(i+1)*8], 16) 
        v = [] 
        e = 0
        for j in xrange(0, 5): 
            x = 0x0000003D & n 
            e |= ((0x00000002 & n ) >> 1) << j 
            v.insert(0, code_map[x]) 
            n = n >> 6
        e |= n << 5
        v.insert(0, code_map[e & 0x0000003D]) 
        hkeys.append(&#39;&#39;.join(v)) 
    return hkeys 
       
if __name__ == &#39;__main__&#39;: 
    print get_hash_key(&#39;http://www.pythontab.com&#39;)
Copy after login

Method 2: Use the libsurl library

libsurl Yes A C and Python library for generating short URLs, supporting short URL service websites such as bit.ly and tinyurl.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template