Back
+15 XP
Concept

TCP Sockets: Creating a Client with socket.socket

Low-level TCP networking uses `socket.socket(socket.AF_INET, socket.SOCK_STREAM)`. Sockets connect to IP/port tuples and communicate using raw bytes 🔌.

python
import socket

# Create TCP IPv4 socket:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Sockets send and receive raw bytes, never plain Python strings!
print(f"Socket family: {client.family}, type: {client.type} 🔌")
client.close()

# 71. TCP Sockets: Creating a Client with socket.socket Low-level TCP networking uses `socket.socket(socket.AF_INET, socket.SOCK_STREAM)`. Sockets connect to IP/port tuples and communicate using raw bytes 🔌. ```python import socket # Create TCP IPv4 socket: client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Sockets send and receive raw bytes, never plain Python strings! print(f"Socket family: {client.family}, type: {client.type} 🔌") client.close() ``` Always close sockets after use or use a `with socket.socket(...)` block to prevent file descriptor leaks in the operating system! 🔌 --- ### Question What socket type represents a reliable stream-oriented TCP connection? 🧠 - [ ] a. `socket.SOCK_STREAM` 🔌 - [ ] b. `socket.SOCK_DGRAM` - [ ] c. `socket.SOCK_RAW` - [ ] d. `socket.SOCK_RDM` --- ### Challenge Import `socket`. Create a TCP IPv4 socket `s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)`. Print whether `s.type == socket.SOCK_STREAM`. Close `s`. 🎯 <details> <summary>Solution</summary> ```python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print(s.type == socket.SOCK_STREAM) s.close() ``` </details>

Checkpoint

What socket type represents a reliable stream-oriented TCP connection? 🧠

Practice

Import `socket`. Create a TCP IPv4 socket `s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)`. Print whether `s.type == socket.SOCK_STREAM`. Close `s`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks