from urlparse import urlparse import urllib def toGSBL(str_url, character_code) scheme, netloc, path, params, query, fragments = urlparse(unicode(str_url, character_code, 'ignore')) netloc = netloc.encode('idna') path = urllib.quote_plus(path.encode('utf-8'), '') if len(path) > 0 else '/' query = urllib.quote_plus(query.encode('utf-8'), '') if len(query) > 0 else '' return scheme + '://' + netloc