Inconsistent url parsing between urllib.parse and urllib.request with ":@" in netloc
Open
Nobody has claimed this yet.
stdlib
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 36k
- PR merge metrics
- PR metrics pending
Description
Bug report
With "https://host:@1/path", urlib.parse.urlsplit parses "host" as the user and "1" as the hostname with no port.
If I use this url with urllib.request.urlopen, it parses "@1" as being the port.
Python 3.9.6 (default, Jun 28 2021, 08:57:49)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib.parse import urlsplit
>>> s = urlsplit("https://host:@1/path")
>>> s.hostname
'1'
>>> s.port
>>> s.username
'host'
>>> import urllib.request
>>> urllib.request.urlopen(urllib.request.Request("https://host:@1/path"))
Traceback (most recent call last):
File "/usr/lib/python3.9/http/client.py", line 872, in _get_hostport
ValueError: invalid literal for int() with base 10: '@1'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/urllib/request.py", line 214, in urlopen
File "/usr/lib/python3.9/urllib/request.py", line 517, in open
File "/usr/lib/python3.9/urllib/request.py", line 534, in _open
File "/usr/lib/python3.9/urllib/request.py", line 494, in _call_chain
File "/usr/lib/python3.9/urllib/request.py", line 1389, in https_open
File "/usr/lib/python3.9/urllib/request.py", line 1315, in do_open
File "/usr/lib/python3.9/http/client.py", line 1384, in __init__
File "/usr/lib/python3.9/http/client.py", line 834, in __init__
File "/usr/lib/python3.9/http/client.py", line 877, in _get_hostport
http.client.InvalidURL: nonnumeric port: '@1'
Your environment
- CPython versions tested on: 3.9.6
- Operating system and architecture: Linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with urllib.parse.urlsplit and urllib.request.urlopen, then trace the http.client host/port handling shown in the traceback. Compare how each path interprets https://host:@1/path and identify the expected consistent result; done means both paths agree without the reported parsing mismatch, with relevant tests passing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100