Ticket #2078: urlescape.py
| File urlescape.py, 447 bytes (added by Andrey <aela@gorodok.net>, 2 years ago) |
|---|
| Line | |
|---|---|
| 1 | #!/usr/bin/env python |
| 2 | #vim:fileencoding=utf8 |
| 3 | |
| 4 | RESERVED = "!*'();:@&=+$,/?%#[]" |
| 5 | UNRESERVED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~" |
| 6 | ALLOWED_CHARS = RESERVED + UNRESERVED |
| 7 | |
| 8 | def 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 | |
| 16 | u = 'http://ru.wikipedia.org/Лопата' |
| 17 | print escape(u) |
