如何使用Python制作唯一的短URL? [英] How to make unique short URL with Python?

查看:124
本文介绍了如何使用Python制作唯一的短URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中将 http://imgur.com/gM19g http://tumblr.com/xzh3bi25y 当从python使用uuid时,我得到了一个很大的.我想要一些简短的网址.

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.而且您只是以62为基础,而不是以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:

地图将整数范围增加到最大为26位的六位数字,但出乎意料地

基本上,该方法是简单地在递增的值中交换位以给出随机性,同时保持确定性并确保您没有任何冲突.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆