def listener(self, sock, *args): '''Asynchronous connection listener. Starts a handler for each connection.''' conn, addr = sock.accept() f = conn.makefile(conn) self.shell = ShoebotCmd(self.bot, stdin=f, stdout=f, intro=INTRO) print(_("Connected")) GObject.io_add_watch(conn, GObject.IO_IN, self.handler) if self.shell.intro: self.shell.stdout.write(str(self.shell.intro) + "\n") self.shell.stdout.flush() return True
class SocketServer(object): def __init__(self, bot, host, port): '''Initialize server and start listening.''' create_listening_socket(host, port, self.listener) self.shell = None self.bot = bot def listener(self, sock, *args): '''Asynchronous connection listener. Starts a handler for each connection.''' conn, addr = sock.accept() f = conn.makefile(conn) self.shell = ShoebotCmd(self.bot, stdin=f, stdout=f, intro=INTRO) print(_("Connected")) GObject.io_add_watch(conn, GObject.IO_IN, self.handler) if self.shell.intro: self.shell.stdout.write(str(self.shell.intro)+"\n") self.shell.stdout.flush() return True def handler(self, conn, *args): ''' Asynchronous connection handler. Processes each line from the socket. ''' # lines from cmd.Cmd self.shell.stdout.write(self.shell.prompt) line = self.shell.stdin.readline() if not len(line): line = 'EOF' return False else: line = line.rstrip('\r\n') line = self.shell.precmd(line) stop = self.shell.onecmd(line) stop = self.shell.postcmd(stop, line) self.shell.stdout.flush() self.shell.postloop() # end lines from cmd.Cmd if stop: self.shell = None conn.close() return not stop
class SocketServer(object): def __init__(self, bot, host, port): '''Initialize server and start listening.''' create_listening_socket(host, port, self.listener) self.shell = None self.bot = bot def listener(self, sock, *args): '''Asynchronous connection listener. Starts a handler for each connection.''' conn, addr = sock.accept() f = conn.makefile(conn) self.shell = ShoebotCmd(self.bot, stdin=f, stdout=f, intro=INTRO) print(_("Connected")) GObject.io_add_watch(conn, GObject.IO_IN, self.handler) if self.shell.intro: self.shell.stdout.write(str(self.shell.intro) + "\n") self.shell.stdout.flush() return True def handler(self, conn, *args): ''' Asynchronous connection handler. Processes each line from the socket. ''' # lines from cmd.Cmd self.shell.stdout.write(self.shell.prompt) line = self.shell.stdin.readline() if not len(line): line = 'EOF' return False else: line = line.rstrip('\r\n') line = self.shell.precmd(line) stop = self.shell.onecmd(line) stop = self.shell.postcmd(stop, line) self.shell.stdout.flush() self.shell.postloop() # end lines from cmd.Cmd if stop: self.shell = None conn.close() return not stop
def listener(self, sock, *args): '''Asynchronous connection listener. Starts a handler for each connection.''' conn, addr = sock.accept() f = conn.makefile(conn) self.shell = ShoebotCmd(self.bot, stdin=f, stdout=f, intro=INTRO) print(_("Connected")) GObject.io_add_watch(conn, GObject.IO_IN, self.handler) if self.shell.intro: self.shell.stdout.write(str(self.shell.intro)+"\n") self.shell.stdout.flush() return True