如何使用Python对一个字符串进行编码和解码以便在URL中使用? [英] How does one encode and decode a string with Python for use in a URL?
问题描述
我有这样一个字符串:
字符串A:[12234_1_Hello'World_34433_22acb_4554344_accCC44]
我想加密字符串A以便在干净的URL中使用。像这样:
字符串B:[cYdfkeYss4543423sdfHsaaZ]
Python中是否存在编码API,给定字符串A,它返回字符串B?
python中有一个解码API,给定String B,它返回String A吗?
import base64
import sys
encoded = base64.b64encode(sys.stdin.read())
打印编码
解码= base64.b64解码(编码)
打印解码
您正在寻找什么?您的具体情况如下:
输入:12234_1_Hello'World_34433_22acb_4554344_accCC44
编码:MTIyMzRfMV9IZWxsbydXb3JsZF8zNDQzM18yMmFjYl80NTU0MzQ0X2FjY0NDNDQ =
已解码:12234_1_Hello'World_34433_22acb_4554344_accCC44
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?
One way of doing the encode/decode is to use the package base64, for an example:
import base64
import sys
encoded = base64.b64encode(sys.stdin.read())
print encoded
decoded = base64.b64decode(encoded)
print decoded
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
这篇关于如何使用Python对一个字符串进行编码和解码以便在URL中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!