How to make unique short URL with Python?

后端 未结 12 1290
渐次进展
渐次进展 2020-12-02 07:02

How can I make unique URL in Python a la http://imgur.com/gM19g or http://tumblr.com/xzh3bi25y When using uuid from python I get a very large one. I want something shorter f

12条回答
  •  天涯浪人
    2020-12-02 07:59

    Hashids is an awesome tool for this.

    Edit:

    Here's how to use Hashids to generate a unique short URL with Python:

    from hashids import Hashids
    
    pk = 123 # Your object's id
    domain = 'imgur.com' # Your domain
    
    hashids = Hashids(salt='this is my salt', min_length=6)
    link_id = hashids.encode(pk)
    url = 'http://{domain}/{link_id}'.format(domain=domain, link_id=link_id)
    

提交回复
热议问题