You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following the syntax specifications in RFC 1808, urlparse recognizes a netloc only if it is properly introduced by ‘//’. Otherwise the input is presumed to be a relative URL and thus to start with a path component.
To get your expected result, you need to prefix with // for urlparse to recognize the netloc:
Since your input doesn't start with a scheme or '//', urlparse should treat your input as a "relative URL". However, your input also contains :, which doesn't make sense in a relative path, so I guess the result you see is actually undefined behavior?
Reacted by Shantanu, Jason Zhang and Konrad Sikorski
urllib.parse.urlparse follows the contents of RFC1808 (Relative URL) religiously. However, RFC1808 is a companion to RFC1738 (URL), and because of that, RFC1808 skipped the description of net_loc entirely (Section 2.1 of RFC1808), since it's describe in Section 3.1 of RFC1738. As a result, RFC1808 skips the description of port in the net_loc entirely, and doesn't consider the case where the net_loc is presented by itself, without the // prefix, which is undefined behaviour as this case is not defined by RFC1808 or RFC1738.
I get the impression that nowadays, most people do not know that according to RFC1738, a net_loc must be prefixed by //, and using a netloc by itself (like 192.168.1.132:16992 presented in the original issue) is very commonplace, and we should just support it in urlparse.
The scheme argument gives the default addressing scheme, to be used only if the URL does not specify one. It should be the same type (text or bytes) as urlstring, except that the default value '' is always allowed, and is automatically converted to b'' if appropriate.
Since urlparse technically discovered a scheme, therefore it ignores the scheme argument.
Activity
planetA commentedon Oct 7, 2022
Adding scheme does not seem to help:
Jason-Y-Z commentedon Oct 7, 2022
I might be horribly wrong, but from the example given -
I think it's treating the IP address of your string as the scheme because it's the first part of the URL string (before the colon).
planetA commentedon Oct 7, 2022
According to RFC3986 scheme should start with an alpha character.
kwsp commentedon Oct 8, 2022
Quoting from the documentation of urlparse
To get your expected result, you need to prefix with
//forurlparseto recognize the netloc:Since your input doesn't start with a scheme or
'//',urlparseshould treat your input as a "relative URL". However, your input also contains:, which doesn't make sense in a relative path, so I guess the result you see is actually undefined behavior?Kcchouette commentedon Oct 23, 2022
Hello
I have the same problem in recent version of python
Same if I specify the scheme in the function:
kwsp commentedon Oct 23, 2022
I think the issue boils down to this:
urllib.parse.urlparsefollows the contents of RFC1808 (Relative URL) religiously. However, RFC1808 is a companion to RFC1738 (URL), and because of that, RFC1808 skipped the description ofnet_locentirely (Section 2.1 of RFC1808), since it's describe in Section 3.1 of RFC1738. As a result, RFC1808 skips the description ofportin thenet_locentirely, and doesn't consider the case where thenet_locis presented by itself, without the//prefix, which is undefined behaviour as this case is not defined by RFC1808 or RFC1738.I get the impression that nowadays, most people do not know that according to RFC1738, a
net_locmust be prefixed by//, and using a netloc by itself (like192.168.1.132:16992presented in the original issue) is very commonplace, and we should just support it inurlparse.I will work on a patch for this
kwsp commentedon Oct 24, 2022
On second thought and reading of the discussion on #38644, this is actually documented behaviour, as documented in the second paragraph of https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse.
The only bug here then is as @planetA mentioned:
I can look into a fix for this - maybe raise an error if the parsed
schemedoesn't start with an alpha character?This is technically documented behaviour too:
Since
urlparsetechnically discovered a scheme, therefore it ignores theschemeargument.arhadthedev commentedon Mar 4, 2023
ping
kwsp commentedon Mar 9, 2023
@arhadthedev Sorry I forgot about this.
Taking a look again, the behaviour and changes I described above has already been fixed in this issue (#99418) and PR (#99421).
We can close this issue now.