如何使用 Python 对字符串进行编码和解码以便在 URL 中使用?
我有一个像这样的字符串:
String A: [ 12234_1_Hello'World_34433_22acb_4554344_accCC44 ]
我想加密字符串 A 以在干净的 URL 中使用。 像这样的事情:
String B: [ cYdfkeYss4543423sdfHsaaZ ]
python中有编码API,给定字符串A,它返回字符串B吗? python中有解码API,给定字符串B,它返回字符串A吗?
I have a string like this:
String A: [ 12234_1_Hello'World_34433_22acb_4554344_accCC44 ]
I would like to encrypt String A to be used in a clean URL. something like this:
String B: [ cYdfkeYss4543423sdfHsaaZ ]
Is there a encode API in python, given String A, it returns String B?
Is there a decode API in python, given String B, it returns String A?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请注意,编码和加密之间存在巨大差异。
如果您想发送敏感数据,请不要使用上面提到的编码;)
note that theres a huge difference between encoding and encryption.
if you want to send sensitive data, then dont use the encoding mentioned above ;)
进行编码/解码的一种方法是使用包 base64,例如:
这是您正在寻找的吗? 根据您的具体情况,您将得到:
输入: 12234_1_Hello'World_34433_22acb_4554344_accCC44
编码: MTIyMzRfMV9IZWxsbydXb3JsZF8zNDQzM18yMmFjYl80NTU0MzQ0X2FjY0NDNDQ=
解码: 12234_1_Hello'World_344 33_22acb_4554344_accCC44
One way of doing the encode/decode is to use the package base64, for an example:
Is it what you were looking for? With your particular case you get:
input: 12234_1_Hello'World_34433_22acb_4554344_accCC44
encoded: MTIyMzRfMV9IZWxsbydXb3JsZF8zNDQzM18yMmFjYl80NTU0MzQ0X2FjY0NDNDQ=
decoded: 12234_1_Hello'World_34433_22acb_4554344_accCC44
您是否希望对字符串进行加密或编码以删除网址中的非法字符?
如果是后者,您可以使用
urllib.quote
:Are you looking to encrypt the string or encode it to remove illegal characters for urls?
If the latter, you can use
urllib.quote
:您想要加密、压缩还是只是 urlencoding? 该字符串可以在 urlencoding 之后传递,但这不会使它像您的示例中那样变小。 压缩可能会缩小它,但您仍然需要对结果进行 urlencode。
您是否真的需要向查看者隐藏字符串数据(例如,敏感数据,不应被越过您的 URL 读取的人看到)?
Are you after encryption, compression, or just urlencoding? The string can be passed after urlencoding, but that will not make it smaller as in your example. Compression might shrink it, but you would still need to urlencode the result.
Do you actually need to hide the string data from the viewer (e.g. sensitive data, should not be viewable by someone reading the URL over your shoulder)?
为了让它变得非常简短-> 只需将一行插入数据库即可。 存储诸如
(id auto_increment, url)
元组列表之类的内容。 然后您可以对 id 进行 Base64 编码以获得“代理 url”。 通过解码 id 并在数据库中查找正确的 url 来对其进行解码。 或者,如果您不介意标识符看起来是连续的,则只需使用数字即可。To make it really short -> just insert a row into the database. Store something like a list of
(id auto_increment, url)
tuples. Then you canbase64
encode the id to get a "proxy url". Decode it by decoding the id and looking up the proper url in the database. Or if you don't mind the identifiers looking sequential, just use the numbers.从 python 2.4 开始,base64 模块为来自不同基数的字符串提供编码和解码。
在您的示例中,您将执行以下操作:
对于完整的 API:
http://docs.python.org/library/base64.html
The base64 module provides encoding and decoding for a string to and from different bases, since python 2.4.
In you example, you would do the following:
For full API:
http://docs.python.org/library/base64.html
减小字符串的大小并保留任意内容是很困难的。
您必须将数据限制为可以有效压缩的数据。
您的替代方法是执行以下操作。
将“URL 中的所有参数”保存在数据库行中。
为该参数集合分配一个 GUID 密钥。
然后提供缩短的 GUID 密钥。
It's hard to reduce the size of a string and preserve arbitrary content.
You have to restrict the data to something you can usefully compress.
Your alternative is to do the following.
Save "all the arguments in the URL" in a database row.
Assign a GUID key to this collection of arguments.
Then provide that shortened GUID key.
另一种缩短字符串的方法是计算字符串的 md5/sha1 哈希(如果您愿意,可以与种子连接):
理论上,您应该得到一组冲突很少且长度固定的字符串。
hashlib
库有一系列不同的哈希函数,您可以使用可以以这种方式使用,具有不同的输出大小。编辑:你还说你需要一个可逆的字符串,所以这不起作用。 然而,据我所知,许多像您一样使用干净 URL 的网络平台似乎想要实现使用哈希函数来计算缩短的 URL,然后将该 URL 与页面的其他数据一起存储以提供反向查找功能。
Another method that would also shorten the string would be to calculate the md5/sha1 hash of the string (concatenated with a seed if you wished):
In theory you should get a set of strings with very few collisions and with a fixed length. The
hashlib
library has an array of different hash functions you can use in this manner, with different output sizes.Edit: You also said that you needed a reversible string, so this wouldn't work for that. Afaik, however, many web platforms that use clean URLs like you seem to want to implement use hash functions to calculate a shortened URL and then store that URL along with the page's other data to provide the reverse lookup capability.