Created
August 24, 2017 18:11
-
-
Save jbiason/f73573939817b01f481ddd173d886c1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SAFE_ENCODE_URL_CHARACTERS = ['/', '*'] | |
import urlparse | |
import urllib | |
def encode(url): | |
if not url.startswith(('//', 'http', 'ftp')): | |
url = '//' + url | |
frags = list(urlparse.urlparse(url)) | |
print frags | |
frags[2] = urllib.quote(frags[2], safe=''.join(SAFE_ENCODE_URL_CHARACTERS)) # path | |
frags[3] = urllib.quote(frags[3]) # params | |
# frags[4] = urllib.quote(frags[4]) # query | |
frags[5] = '' | |
return urlparse.urlunparse(frags) | |
print encode('example.com/*') | |
print encode('http://julio.biason.net/#/home') | |
print encode('http://julio.biason.net/index?page=1') | |
print encode('https://juliobiason.ent/images and stuff/?page=1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment