Python
Sockets & Networking
31 flashcards · answers and spaced-repetition review in the KnowCard app
You need a network protocol that guarantees every byte arrives, in order, with no loss. Which socket type do you create, and what do you give up vs the alternative?
On a TCP server you call s.accept(). Which socket do you then use to call recv/send with the client?
What is the correct order of TCP server calls before you can talk to a client, and what does each step do?
What does this client snippet do wrong?
```python
s = socket.create_connection(
(ip, 50000))
s.send("hello")
```
A peer sends exactly 1000 bytes in one send. Your TCP code does data = conn.recv(1024). Is data guaranteed to be all 1000 bytes?
In a TCP receive loop, how do you detect that the peer closed the connection?
```python
while data := conn.recv(1024):
...
```
Why wrap a socket in with socket.socket(...) as s: instead of just assigning it?
What's the difference between binding a server to "" (or 0.0.0.0) vs binding to "localhost" / 127.0.0.1?
What does the address argument to bind, connect, and sendto look like for IPv4 sockets?
What's the practical difference between send and sendall on a TCP socket?
What does recv_into(buffer) do that plain recv(bufsize) doesn't?
How do you make a socket use IPv6 instead of IPv4, and how can you check at runtime whether the OS actually supports IPv6?
The socket module gives you raw TCP/UDP. When you want to speak a higher-level protocol instead, which standard-library modules handle HTTP, FTP, and email?
You're choosing a port for a custom server. Why avoid low numbers like 80 or 21, and what range is safe to grab?
VoIP and video streaming deliberately run over UDP even though packets can be lost or arrive out of order. Why is that the right trade-off?
A UDP server runs data, addr = s.recvfrom(1024). What are the two things it returns, and what does the 1024 control?
You have many sockets and don't want one idle recv to freeze the whole program. What is a socket's default mode, and how do reads/sends behave once you change it?
On a nonblocking socket, connect() raises OSError ("Operation now in progress") while the connection is still pending. How does connect_ex() behave differently?
Besides the destination address, what two optional arguments does socket.create_connection(address, [timeout, source_address]) accept?
On a connected socket, how do you get the remote peer's (IP, port) versus your own socket's local address?
You don't want recv to block forever if the peer goes silent. How do you cap the wait, and what happens when the limit is exceeded?
You send a multi-byte binary integer over TCP from a little-endian PC to a big-endian machine and it arrives garbled. TCP delivered the bytes in order — so what went wrong, and what's the fix?
Serving many clients in one process by looping over nonblocking sockets and checking each one is called what — and what's its cost? Which module avoids it?
Using selectors, you register a socket with selector.register(sock, selectors.EVENT_READ, callback). In the serve loop, what does selector.select() return and how do you reach the callback and the socket?
To write a multi-client server with socketserver, which base class do you subclass, which method must you override, and how do you reach the client inside it?
In a socketserver TCP handler, self.request is the client socket. What is self.request instead when the server is UDP?
socketserver.TCPServer serves only one client at a time. Which socketserver classes give you concurrent clients, and how do the two families differ?
FTP runs over two separate channels. What are they, and why does passive mode let a client behind a router/firewall still transfer files?
ftplib's FTP object has both quit() and close(). What's the difference, and which is the polite one?
ftplib offers exactly four transfer methods: retrbinary, retrlines, storbinary, storlines. What two decisions do these four names encode?
Why might ftp.size(filename) fail or be unavailable against some FTP servers even though the file exists?
Start learning today
Free to start — download the app or use it in your browser.
