如何用Python制作独特的短网址?
如何在Python中创建唯一的URL https://i.sstatic.net/turb6.jpg 或 http://tumblr.com/xzh3bi25y 当使用 python 中的 uuid 时,我得到一个非常大的 uuid。我想要更短的 URL。
How can I make unique URL in Python a la https://i.sstatic.net/turb6.jpg or http://tumblr.com/xzh3bi25y
When using uuid from python I get a very large one. I want something shorter for URLs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
编辑:在这里,我为您编写了一个模块。使用它。 http://code.activestate.com/recipes/576918/
从 1 开始计数保证简短、唯一的 URL。 /1、/2、/3 ...等。
在字母表中添加大写和小写字母将给出与您的问题中类似的 URL。而且您只是以 62 为基数而不是 10 为基数进行计数。
现在唯一的问题是 URL 是连续出现的。要解决这个问题,请阅读我对此问题的回答:
将增量整数范围映射到最大以 26 为基数的六位数字,但不可预测
基本上,该方法是简单地交换增量值中的位,以给出随机性的外观,同时保持确定性并保证你没有任何碰撞。
Edit: Here, I wrote a module for you. Use it. http://code.activestate.com/recipes/576918/
Counting up from 1 will guarantee short, unique URLS. /1, /2, /3 ... etc.
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.
Now the only problem is that the URLs come consecutively. To fix that, read my answer to this question here:
Map incrementing integer range to six-digit base 26 max, but unpredictably
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.
我不确定大多数 URL 缩短器是否使用随机字符串。我的印象是他们将 URL 写入数据库,然后使用新记录的整数 ID 作为短 URL,编码基数为 36 或 62(字母+数字)。
将 int 转换为任意基数的字符串的 Python 代码位于此处。
I'm not sure most URL shorteners use a random string. My impression is they write the URL to a database, then use the integer ID of the new record as the short URL, encoded base 36 or 62 (letters+digits).
Python code to convert an int to a string in arbitrary bases is here.
Python 的 short_url 非常棒。
这是一个示例:
并解码代码:
就是这样:)
希望这会有所帮助。
Python's short_url is awesome.
Here is an example:
And to decode the code:
That's it :)
Hope this will help.
Hashids 是一个很棒的工具。
编辑:
以下是如何使用 Hashids 通过 Python 生成唯一的短 URL:
Hashids is an awesome tool for this.
Edit:
Here's how to use Hashids to generate a unique short URL with Python:
该模块将执行您想要的操作,保证字符串全局唯一(它是 UUID):
http ://pypi.python.org/pypi/shortuuid/0.1
如果您需要更短的内容,您应该能够将其截断到所需的长度,并且仍然得到可以合理避免冲突的内容。
This module will do what you want, guaranteeing that the string is globally unique (it is a UUID):
http://pypi.python.org/pypi/shortuuid/0.1
If you need something shorter, you should be able to truncate it to the desired length and still get something that will reasonably probably avoid clashes.
这个答案来得很晚,但当我计划创建一个 URL 缩短器项目时,我偶然发现了这个问题。现在我已经实现了一个功能齐全的 URL 缩短器(源代码位于 amitt001/pygmy),我在这里添加一个答案对于其他人。
任何 URL 缩短器背后的基本原理都是从长 URL 中获取 int,然后使用 base62(base32 等)编码将此 int 转换为更具可读性的短 URL。
这个 int 是如何生成的?
大多数 URL 缩短器使用一些自动增量数据存储来将 URL 添加到数据存储,并使用自动增量 id 来获取 int 的 Base62 编码。
字符串程序中的 Base62 编码示例:
这只是显示 Base62 编码的部分代码。查看完整的base62编码/解码代码 core/hashdigest.py
此答案中的所有链接从我创建的项目中缩短
This answer comes pretty late but I stumbled upon this question when I was planning to create an URL shortener project. Now that I have implemented a fully functional URL shortener(source code at amitt001/pygmy) I am adding an answer here for others.
The basic principle behind any URL shortener is to get an int from long URL then use base62(base32, etc) encoding to convert this int to a more readable short URL.
How is this int generated?
Most of the URL shortener uses some auto-incrementing datastore to add URL to datastore and use the autoincrement id to get base62 encoding of int.
The sample base62 encoding from string program:
This is just the partial code showing base62 encoding. Check out the complete base62 encoding/decoding code at core/hashdigest.py
All the link in this answer are shortened from the project I created
UUID之所以长,是因为它们包含大量信息,这样可以保证它们是全局唯一的。
如果你想要更短的东西,那么你需要做一些事情,比如生成一个随机字符串,检查它是否在已经生成的字符串的范围内,然后重复直到得到一个未使用的字符串。您还需要注意此处的并发性(如果在插入字符串集之前由单独的进程生成相同的字符串怎么办?)。
如果您需要在 Python 中生成随机字符串的帮助,请参阅这个其他问题 可能有帮助。
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.
这是Python并不重要,但你只需要一个映射到你想要的长度的哈希函数。例如,可以使用 MD5,然后仅获取前 n 个字符。不过,在这种情况下,您必须注意碰撞,因此您可能需要选择在碰撞检测方面更强大的东西(例如使用素数在哈希字符串的空间中循环)。
It doesn't really matter that this is Python, but you just need a hash function that maps to the length you want. For example, maybe use MD5 and then take just the first
n
characters. You'll have to watch out for collisions in that case, though, so you might want to pick something a little more robust in terms of collision detection (like using primes to cycle through the space of hash strings).我不知道你是否可以使用这个,但我们在 Zope 中生成内容对象,这些对象根据当前时间字符串获取唯一的数字 ID,以毫秒为单位(例如,1254298969501)
也许你可以猜到其余的。使用此处描述的配方:
如何将整数转换为Python 中最短的 url 安全字符串?,我们动态编码和解码真实 id,无需存储。例如,13 位整数在基数 62 中减少为 7 个字母数字字符。
为了完成实施,我们注册了一个短(xxx.yy)域名,它对“未找到”URL 进行解码并执行 301 重定向,
如果我重新开始,我会减去“重新开始”时间(以毫秒为单位) )从编码之前的数字id,然后在解码时重新添加它。或者在生成对象时。任何。这样就更短了..
I don't know if you can use this, but we generate content objects in Zope that get unique numeric ids based on current time strings, in millis (eg, 1254298969501)
Maybe you can guess the rest. Using the recipe described here:
How to convert an integer to the shortest url-safe string in Python?, we encode and decode the real id on the fly, with no need for storage. A 13-digit integer is reduced to 7 alphanumeric chars in base 62, for example.
To complete the implementation, we registered a short (xxx.yy) domain name, that decodes and does a 301 redirect for "not found" URLs,
If I was starting over, I would subtract the "starting-over" time (in millis) from the numeric id prior to encoding, then re-add it when decoding. Or else when generating the objects. Whatever. That would be way shorter..
可以生成N个随机字符串:
所以,
全部小写
You can generate a N random string:
so,
all lowercase
试试这个 http://code.google.com/p/tiny4py/ ...仍在开发中,但非常有用!
Try this http://code.google.com/p/tiny4py/ ... It's still under development, but very useful!!
我的目标:生成指定固定长度的唯一标识符,由字符
0-9
和az
组成。例如:这是我的解决方案。 (改编自 此答案,作者:kmkaplan.)
My Goal: Generate a unique identifier of a specified fixed length consisting of the characters
0-9
anda-z
. For example:Here's my solution. (Adapted from this answer by kmkaplan.)