Python implements a URL short connection in two ways

Source: Internet
Author: User
Tags md5

Almost all of the meager offers serve to shorten the Web site, the principle is that a URL address in accordance with a certain algorithm generated a string, and then added to a short domain name behind the edge into a new URL address, the database will store this short address and the original address, when the user clicks on this new short address, The short address service reads the original address from the database based on a few strings following the short domain name and then jumps the page.

For example, Sina's meager URL is http://t.cn/xxxxxxx t.cn is its domain name, followed by a 7-bit calculated string.

Today we use Python to implement this feature in two ways.

Method One: Using the Hachiku custom algorithm

Because a URL that is too long in the text is messy, either by omitting the display or by using a short URL.

In order to facilitate statistical clicks and content filtering at the same time, a method to generate a short URL value is implemented.

To prevent your hash value from being cracked, you can add your own salt when generating a MD5 value.

This way, even if your code_map can't hack the original URL.

To make the results more random, save the second bit that is not used for Each loop 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): HK Eys = [] hex = GET_MD5 (Long_url) for I in Xrange (0, 4): n = Int (hex[i*8: (i+1) *8], V = [] e = 0 for J in xrange (0, 5): x = 0x0000003d & n e |= (0x00000002 &am P n) >> 1) <&Lt J V.insert (0, code_map[x]) n = n >> 6 e |= n << 5 v.insert (0, code  
    _map[e & 0x0000003d]) hkeys.append ('. Join (v)) return Hkeys if __name__ = ' __main__ ': Print Get_hash_key (' http://pythontab.com ')

Method Two: Use Libsurl library

Libsurl is a C and Python library for generating short URLs that support short URL service sites such as bit.ly and TinyURL.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.