socket.create_connection
Prev: socket.close | Up: socket | Next: socket.create_server
Rel: socket.AddressFamily, socket.close, socket.create_server, socket.dup
Ref: docs.python.org — socket.create_connection
Tags: #public #python3 #stdlib #socket
Import
import socket
from socket import create_connection
Signature & Overview
socket.create_connection(address, timeout=<object object at 0x1040b4980>, source_address=None, *, all_errors=False)
Connect to address and return the socket object.
Usage
import socket
# High-level TCP client helper (tries IPv6 and IPv4 automatically):
# with socket.create_connection(("example.com", 443), timeout=10) as sock: ...
assert callable(socket.create_connection)
Gotchas
- Always pass
timeout=: Without a timeout, connecting to an unreachable host can block for minutes. - Handles dual-stack DNS automatically: Prefer
create_connection()over manually callingsocket()+connect().
REPL Inspect
from socket import create_connection
help(create_connection)
Linked from
- tuts/python3/stdlib_module/socket
- tuts/python3/stdlib_module/socket/AddressFamily
- tuts/python3/stdlib_module/socket/close
- tuts/python3/stdlib_module/socket/create_server
- tuts/python3/stdlib_module/socket/dup
- tuts/python3/stdlib_module/socket/error
- tuts/python3/stdlib_module/socket/fromfd
- tuts/python3/stdlib_module/socket/gaierror
- tuts/python3/stdlib_module/socket/getaddrinfo
- tuts/python3/stdlib_module/socket/getdefaulttimeout
- tuts/python3/stdlib_module/socket/getfqdn
- tuts/python3/stdlib_module/socket/gethostbyaddr
- tuts/python3/stdlib_module/socket/gethostbyname
- tuts/python3/stdlib_module/socket/gethostbyname_ex
- tuts/python3/stdlib_module/socket/gethostname
- tuts/python3/stdlib_module/socket/getnameinfo
- tuts/python3/stdlib_module/socket/getprotobyname
- tuts/python3/stdlib_module/socket/getservbyname
- tuts/python3/stdlib_module/socket/getservbyport
- tuts/python3/stdlib_module/socket/has_dualstack_ipv6
- tuts/python3/stdlib_module/socket/herror
- tuts/python3/stdlib_module/socket/htonl
- tuts/python3/stdlib_module/socket/htons
- tuts/python3/stdlib_module/socket/if_indextoname
- tuts/python3/stdlib_module/socket/if_nameindex
- tuts/python3/stdlib_module/socket/if_nametoindex
- tuts/python3/stdlib_module/socket/inet_aton
- tuts/python3/stdlib_module/socket/inet_ntoa
- tuts/python3/stdlib_module/socket/inet_ntop
- tuts/python3/stdlib_module/socket/inet_pton
- tuts/python3/stdlib_module/socket/ntohl
- tuts/python3/stdlib_module/socket/ntohs
- tuts/python3/stdlib_module/socket/recv_fds
- tuts/python3/stdlib_module/socket/send_fds
- tuts/python3/stdlib_module/socket/setdefaulttimeout
- tuts/python3/stdlib_module/socket/sethostname
- tuts/python3/stdlib_module/socket/socket
- tuts/python3/stdlib_module/socket/SocketKind
- tuts/python3/stdlib_module/socket/socketpair
- tuts/python3/stdlib_module/socket/SocketType
- tuts/python3/stdlib_module/socket/timeout
Related notes
- tuts/python3/stdlib_module/socket/AddressFamily Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/close Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/create_server Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/dup Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/socket Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/error links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/fromfd links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/gaierror links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/getaddrinfo links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/getdefaulttimeout links to this page; shared tags: #socket, #stdlib