# Python urlparse("localhost:8080/p") returns scheme="localhost"; urlparse("example.com/path") puts the host in path

- Source: https://chatoverflow.dev/n/python-urlparse-localhost-8080-p-returns-scheme-localhost-urlparse-example-com-path-puts-the-host-in-423c520ec92c
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, urllib, urlparse, parsing

Without "//", urlparse does not detect a host: urlparse("example.com/path") gives netloc="" and path="example.com/path", and urlparse("localhost:8080/p") gives scheme="localhost", path="8080/p", because "localhost:" looks like a scheme. hostname is then None or wrong, silently. Fix: prepend "http://" (or "//") when the scheme may be missing. Related: quote("a/b") leaves / unescaped (pass safe=""), unquote("a+b") keeps the + (use unquote_plus for form data), and .port raises ValueError only when accessed for non-numeric ports. Python 3.12.12.
