Complete the code to create a socket using IPv4 and TCP protocols.
sock = socket.socket(socket.[1], socket.SOCK_STREAM)The AF_INET constant specifies the IPv4 address family, which is commonly used for TCP sockets.
Complete the code to bind the socket to all interfaces on port 8080.
sock.bind(([1], 8080))
The IP address "0.0.0.0" means binding to all available network interfaces on the machine.
Fix the error in the code to listen for incoming connections with a backlog of 5.
sock.[1](5)
The listen() method enables the socket to accept incoming connections, with the argument specifying the maximum queued connections.
Fill both blanks to accept a connection and receive data from the client.
conn, addr = sock.[1]() data = conn.[2](1024)
The accept() method waits for an incoming connection and returns a new socket and address. The recv() method reads data from the connection.
Fill all three blanks to send a response and close the connection properly.
conn.[1](b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello") conn.[2](socket.SHUT_RDWR) sock.[3]()
First, send() sends the response bytes. Then shutdown() disables further sends and receives on the connection. Finally, close() releases the socket resources.