如何使用 Python 创建唯一的短 URL? [英] How to make unique short URL with Python?
问题描述
如何在 Python 中创建唯一的 URL,例如 http://imgur.com/gM19g 或
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 for URLs.
推荐答案
编辑:在这里,我为您编写了一个模块.用它.http://code.activestate.com/recipes/576918/
Edit: Here, I wrote a module for you. Use it. http://code.activestate.com/recipes/576918/
从 1 开始计数将保证 URL 短且唯一./1、/2、/3 ...等
Counting up from 1 will guarantee short, unique URLS. /1, /2, /3 ... etc.
在您的字母表中添加大写和小写字母将提供与您的问题中类似的 URL.而且你只是以 base-62 而不是 base-10 计算.
Adding uppercase and lowercase letters to your alphabet will give URLs like those in your question. And you're just counting in base-62 instead of base-10.
现在唯一的问题是 URL 是连续出现的.要解决这个问题,请在此处阅读我对这个问题的回答:
Now the only problem is that the URLs come consecutively. To fix that, read my answer to this question here:
基本上,该方法是简单地在递增值中交换位,以提供随机性的外观,同时保持确定性并保证不会发生任何冲突.
Basically the approach is to simply swap bits around in the incrementing value to give the appearance of randomness while maintaining determinism and guaranteeing that you don't have any collisions.
这篇关于如何使用 Python 创建唯一的短 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!