socket.makefile(mode='r')
- This function creates and returns a file object
f
- This file object
f
reads from and/or writes to the socket - Typically, this refers to the server socket
- This can be used for logging
Example of makefile
>>> import socket
>>> sock = socket(AF_INET, SOCK_STREAM)
>>> serv_addr = ('', 25000) # server socket binds...
>>> sock.bind(serv_address) # ...to port 25000
>>> sock.listen(5) # only allow 5 client connections
>>> # Make file for server socket
>>> server, client_addr = sock.accept()
>>> sf = server.makefile("r+b", bufsize=0)
>>> # Make file for client socket
>>> client = socket.create_connection(serv_addr)
>>> cf = client.makefile('r_b', bufsize=0)
>>> sf.write('hello world')
>>> sf.flush()
>>> sf.close()
>>> server.close()
>>> print(cf.read(99)) # does not hang
References
Previous
Next