How to encode and decode a string with Python for use in a url?

I have a line like this:

String A: [ 12234_1_Hello'World_34433_22acb_4554344_accCC44 ] 

I would like to encrypt String A to be used in a pure URL. something like that:

 String B: [ cYdfkeYss4543423sdfHsaaZ ] 

Is there an API for coding in python, given String A, does it return String B? Is there a decoding API in python, given String B, it returns String A?

+4
source share
8 answers

One way to perform encoding / decoding is to use base64 as an example:

 import base64 import sys encoded = base64.b64encode(sys.stdin.read()) print encoded decoded = base64.b64decode(encoded) print decoded 

Is this what you were looking for? In your particular case, you will receive:

input: 12234_1_Hello'World_34433_22acb_4554344_accCC44

encoded: MTIyMzRfMV9IZWxsbydXb3JsZF8zNDQzM18yMmFjYl80NTU0MzQ0X2FjY0NDNDQ =

Decoded: 12234_1_Hello'World_34433_22acb_4554344_accCC44

+9
source

Please note that there is a huge difference between encoding and encryption.

if you want to send sensitive data, then do not use the encoding mentioned above;)

+10
source

Are you after encryption, compression or just urlencoding? The string may be passed after urlencoding, but that will not make it smaller, as in your example. Compression can compress it, but you still have to urlencode the result.

Do you really need to hide the string data from the viewer (for example, sensitive data should not be visible to someone reading the URL over the shoulder)?

+5
source

To make it really short -> just insert a row into the database. Store something like a list of tuples (id auto_increment, url) . Then you can base64 encode the identifier to get the "proxy url". Decode it by decoding the identifier and looking at the correct URL in the database. Or, if you don't mind the identifiers looking consistent, just use numbers.

+5
source

Do you want to encrypt a string or encode it to remove invalid characters for URLs? If the latter, you can use urllib.quote :

 >>> quoted = urllib.quote("12234_1_Hello'World_34433_22acb_4554344_accCC44") >>> quoted '12234_1_Hello%27World_34433_22acb_4554344_accCC44' >>> urllib.unquote(quoted) "12234_1_Hello'World_34433_22acb_4554344_accCC44" 
+4
source

The base64 module provides encoding and decoding for strings and from different bases since python 2.4.

In your example, you will do the following:

 import base64 string_b = base64.b64encode(string_a) string_a = base64.b64decode(string_b) 

For the full API: http://docs.python.org/library/base64.html

+2
source

It is difficult to reduce the size of the string and save arbitrary content.

You must limit the data to what you can usefully compress.

Your alternative is as follows:

  • Save "all arguments to URL" in the database row.

  • Assign a GUID to this collection of arguments.

  • Then specify this abbreviated GUID.

+2
source

Another method, which would also shorten the string, would be to compute the hash md5 / sha1 of the string (if you want) (<concretized with the seed):

 import hashlib >>> hashlib.sha1("12234_1_Hello'World_34433_22acb_4554344_accCC44").hexdigest() 'e1153227558aadc00a2e90b5013fdd6b0804fdfb' 

In theory, you should get a rowset with very few collisions and a fixed length. The hashlib library has an array of different hash functions that you can use this way with different release sizes.

Edit: You also said that you need a reversible string, so this will not work. Afaik, however, many web platforms that use pure URLs, for example, you seem to want to use hash functions to calculate the abbreviated URL, and then save that URL along with a page of other data to allow reverse search.

+1
source

All Articles