def escape ( s ) : if not isinstance ( s , bytes ) : s = s . encode ( 'utf-8' ) return quote ( s , safe = '~' )
python urllib quote plus
Escape a URL including any / .
def escape(s):
    """Escape a URL including any /."""
    if not isinstance(s, bytes):
        s = s.encode('utf-8')
    return quote(s, safe='~')