Skip to content

Instantly share code, notes, and snippets.

@jbiason
Created August 24, 2017 18:11
Show Gist options
  • Save jbiason/f73573939817b01f481ddd173d886c1a to your computer and use it in GitHub Desktop.
Save jbiason/f73573939817b01f481ddd173d886c1a to your computer and use it in GitHub Desktop.
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