How to make unique short URL with Python?

后端 未结 12 1259
渐次进展
渐次进展 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 08:05

    The reason UUIDs are long is because they contain lots of information so that they can be guaranteed to be globally unique.

    If you want something shorter, then you'll need to do something like generate a random string, checking whether it is in the universe of already generated strings, and repeating until you get an unused string. You'll also need to watch out for concurrency here (what if the same string gets generated by a separate process before you inserted into the set of strings?).

    If you need some help generating random strings in Python, this other question might help.

提交回复
热议问题