MCPcopy
hub / github.com/redis/redis-py / parse_url

Function parse_url

redis/connection.py:2264–2318  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

2262
2263
2264def parse_url(url):
2265 if not (
2266 url.startswith("redis://")
2267 or url.startswith("rediss://")
2268 or url.startswith("unix://")
2269 ):
2270 raise ValueError(
2271 "Redis URL must specify one of the following "
2272 "schemes (redis://, rediss://, unix://)"
2273 )
2274
2275 url = urlparse(url)
2276 kwargs = {}
2277
2278 for name, value in parse_qs(url.query).items():
2279 if value and len(value) > 0:
2280 value = unquote(value[0])
2281 parser = URL_QUERY_ARGUMENT_PARSERS.get(name)
2282 if parser:
2283 try:
2284 kwargs[name] = parser(value)
2285 except (TypeError, ValueError):
2286 raise ValueError(f"Invalid value for '{name}' in connection URL.")
2287 else:
2288 kwargs[name] = value
2289
2290 if url.username:
2291 kwargs["username"] = unquote(url.username)
2292 if url.password:
2293 kwargs["password"] = unquote(url.password)
2294
2295 # We only support redis://, rediss:// and unix:// schemes.
2296 if url.scheme == "unix":
2297 if url.path:
2298 kwargs["path"] = unquote(url.path)
2299 kwargs["connection_class"] = UnixDomainSocketConnection
2300
2301 else: # implied: url.scheme in ("redis", "rediss"):
2302 if url.hostname:
2303 kwargs["host"] = unquote(url.hostname)
2304 if url.port:
2305 kwargs["port"] = int(url.port)
2306
2307 # If there's a path argument, use it as the db argument if a
2308 # querystring value wasn't specified
2309 if url.path and "db" not in kwargs:
2310 try:
2311 kwargs["db"] = int(unquote(url.path).replace("/", ""))
2312 except (AttributeError, ValueError):
2313 pass
2314
2315 if url.scheme == "rediss":
2316 kwargs["connection_class"] = SSLConnection
2317
2318 return kwargs
2319
2320
2321_CP = TypeVar("_CP", bound="ConnectionPool")

Callers 14

test_pool_auto_closeFunction · 0.90
test_redis_from_poolFunction · 0.90
_get_clientFunction · 0.90
test_pool_auto_closeFunction · 0.90
test_redis_from_poolFunction · 0.90
client_factoryFunction · 0.90
connect_argsFunction · 0.90

Calls 1

getMethod · 0.45

Tested by 12

test_pool_auto_closeFunction · 0.72
test_redis_from_poolFunction · 0.72
_get_clientFunction · 0.72
test_pool_auto_closeFunction · 0.72
test_redis_from_poolFunction · 0.72
client_factoryFunction · 0.72
connect_argsFunction · 0.72