I’ve been having a hard time getting the CMD module to use a socket for stdin. Here is what I have :
class Server(cmd.Cmd):
use_rawinput = False
def __init__(self, port):
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind(("", port))
f = self.sock.makefile(mode='rw')
cmd.Cmd.__init__(self, stdin = f, stdout=sys.stdout)
def do_register(self, username):
print username
When I connect with the client and try the command register user1, I don’t get anything on the server’s console.
The file returned by
makefilewill only work forSOCK_STREAMsockets.SOCK_DGRAMsockets have no notion of a continuous stream of bytes (only individual packets), and therefore cannot usereadorwrite.You should initialize the socket with
socket.SOCK_STREAMinstead ofsocket.SOCK_DGRAM.