What Python module is the core of socket programming?
The socket module.
What is the necessary first line of code to use sockets in a script?
import socket
What function is used to create a new socket object?
socket.socket()
What argument in socket.socket() specifies the IPv4 address family?
socket.AF_INET (Address Family: Internet)
What argument specifies the connection-oriented TCP protocol?
socket.SOCK_STREAM (Stream Socket)
What argument specifies the connectionless UDP protocol?
socket.SOCK_DGRAM (Datagram Socket)
What is the very first mandatory step for a server socket?
s.bind((host, port)) (Assigning its address and port)
For s.bind(), what host value listens on all available network interfaces?
'0.0.0.0' or an empty string ('')
For s.bind(), what host value listens only on the local machine (localhost)?
'127.0.0.1'
What method puts the server socket into passive listening mode?
s.listen(backlog)
What does the backlog argument in s.listen() control?
The maximum number of queued (unaccepted) client connections.
What method blocks and waits for a new client connection on the server?
conn, addr = s.accept()
What two objects does s.accept() return?
A new connection socket (conn) and the client’s address tuple (addr).
What method does a client use to initiate a connection to a server?
s.connect((host, port))
What method sends data over a connected socket?
conn.send(data) (or s.send())
What method receives data, and what argument specifies the buffer size?
conn.recv(bufsize) (The bufsize is the max number of bytes to receive)
What format must data be in before being sent with s.send()?
Bytes (b'...'). You must use .encode().
What must you do with the data received from s.recv() to read it as text?
Use the .decode() method (e.g., data.decode('utf-8')).
What method is used by both client and server to properly release the socket resource?
s.close()
What Python keyword is highly recommended for automatically managing the closing of a socket?
The with statement (used for reliable cleanup).