support ipv6 (#850)

* support ipv6

There are many `:` in ipv6 address, uvicorn is support both v6 and v4, but `host, port = api.args.listen.split(":")` this line will fail to split v6 host

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
TITC 2025-01-23 23:20:19 +08:00 committed by GitHub
parent 9ff5c1159d
commit 7d74951dcd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,4 @@
import re
from threading import Lock
import pyrootutils
@ -98,9 +99,14 @@ class API(ExceptionHandler):
# Instead, it's better to use multiprocessing or independent models per thread.
if __name__ == "__main__":
api = API()
host, port = api.args.listen.split(":")
# IPv6 address format is [xxxx:xxxx::xxxx]:port
match = re.search(r"\[([^\]]+)\]:(\d+)$", api.args.listen)
if match:
host, port = match.groups() # IPv6
else:
host, port = api.args.listen.split(":") # IPv4
uvicorn.run(
api.app,