Ticket #2078: urlescape.py

File urlescape.py, 447 bytes (added by Andrey <aela@…>, 18 years ago)

A simple code for percent-encoding of strings

Line 
1#!/usr/bin/env python
2#vim:fileencoding=utf8
3
4RESERVED = "!*'();:@&=+$,/?%#[]"
5UNRESERVED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
6ALLOWED_CHARS = RESERVED + UNRESERVED
7
8def escape(s):
9 def escape_char(c):
10 if c in ALLOWED_CHARS:
11 return c
12 else:
13 return '%%%0x' % ord(c)
14 return ''.join(escape_char(c) for c in s)
15
16u = 'http://ru.wikipedia.org/Лопата'
17print escape(u)
Back to Top