I want to add scheme in urls if not present.
import urlparse
p = urlparse.urlparse(url)
print p
netloc = p.netloc or p.path
path = p.path if p.netloc else ''
scheme = p.scheme or 'http'
p = urlparse.ParseResult(scheme, netloc, path, *p[3:])
url = p.geturl()
print url
The above code works great, in case when I dont have any port number. When port number is there, it show arbitary output. For eg:-
input go.com:8000/3/
output go.com://8000/3/
Same goes for localhost. What approach should I been following in this case?
According to the docs you need to properly introduce netloc to be parsed correctly. So try adding
//at the beginning of the url if it's not an absolute path so like:This way it correctly identifies each part of the url. Also please see the docs: https://docs.python.org/2/library/urlparse.html#urlparse.urlparse