Overview:
-
The method makefile() of socket class retrieves the file object from a socket and allows reads and writes to the socket using the file I/O semantics.
Example - Server:
# Example Python program that writes to the file object associated with # Create server socket # Bind to an IP & port # Enter into listening state # Accepting client connections clientSocket, clientAddress = svr.accept() fileObject = clientSocket.makefile("wb", buffering=0) |
Output:
Listening for incoming connections: Sent response to the client |
Example - Client:
# Example Python program that reads from # Create a socket and connect to the server # Obtain the file object associated with the socket # Read all that the server has sent if data == b'': print(data.decode()) |
Output:
Hello!
That was the response from your server... |