python/cpython · error · ValueError
host and port was not specified and no sock specified
Error message
host and port was not specified and no sock specified
What it means
Raised by create_connection when neither host/port nor sock was provided. The method needs either an address to resolve-and-dial or an already-connected socket to adopt; with both absent there is nothing to connect, so it raises ValueError immediately.
Source
Thrown at Lib/asyncio/base_events.py:1192
raise exceptions[0]
elif exceptions:
# If they all have the same str(), raise one.
model = str(exceptions[0])
if all(str(exc) == model for exc in exceptions):
raise exceptions[0]
# Raise a combined exception so the user can see all
# the various error messages.
raise OSError('Multiple exceptions: {}'.format(
', '.join(str(exc) for exc in exceptions)))
else:
# No exceptions were collected, raise a timeout error
raise TimeoutError('create_connection failed')
finally:
exceptions = None
else:
if sock is None:
raise ValueError(
'host and port was not specified and no sock specified')
if sock.type != socket.SOCK_STREAM:
# We allow AF_INET, AF_INET6, AF_UNIX as long as they
# are SOCK_STREAM.
# We support passing AF_UNIX sockets even though we have
# a dedicated API for that: create_unix_connection.
# Disallowing AF_UNIX in this method, breaks backwards
# compatibility.
raise ValueError(
f'A Stream Socket was expected, got {sock!r}')
transport, protocol = await self._create_connection_transport(
sock, protocol_factory, ssl, server_hostname,
ssl_handshake_timeout=ssl_handshake_timeout,
ssl_shutdown_timeout=ssl_shutdown_timeout)
if self._debug:
# Get the socket from the transport because SSL transport closes
# the old socket and creates a new SSL socketView on GitHub (pinned to bc6749cc3b)
Solutions
- Validate configuration before connecting: assert host and port are set (and port is an int).
- Pass sock=... if your intent is to adopt an existing connected socket.
- Fail fast at config load with a clear message instead of deep inside asyncio.
Example fix
// before
host = cfg.get('host') # missing -> None
await loop.create_connection(proto, host, cfg.get('port'))
// after
host, port = cfg['host'], int(cfg['port'])
if not host or not port:
raise ConfigError('host and port are required')
await loop.create_connection(proto, host, port) Defensive patterns
Strategy: validation
Validate before calling
if not host and sock is None:
raise ConfigError('create_connection requires host/port or sock') Prevention
- Validate required connection config at load time, not inside the network layer.
- Type connection targets as a union (host/port pair vs socket) so 'neither' is unrepresentable.
- Add assertions in tests that config parsing always yields a usable target.
When it happens
Trigger: loop.create_connection(proto) or loop.create_connection(proto, None, None) — host and port default to None and no sock was passed. Common when host/port come from config and both resolve to None/empty.
Common situations: Config-driven clients where required host/port keys are missing and code passes the values through unchecked; parsers that strip values and yield None; test scaffolding that forgot to populate the target.
Related errors
- server_hostname is only meaningful with ssl
- host/port and sock can not be specified at the same time
- A Stream Socket was expected, got {sock!r}
- Semaphore initial value must be >= 0
- parties must be >= 1
AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14).
Data as JSON: /api/errors/37efea5fa995dcab.
Report an issue: GitHub.