コード例 #1
0
class EscapeServer(asyncio.Protocol):
    def connection_made(self, transport):
        # peername = transport.get_extra_info('peername')
        # print('Connection from {}'.format(peername))
        self.transport = transport
        self.room = EscapeRoom()
        self.room.start()

    def data_received(self, raw_data):
        try:
            message = raw_data.decode('utf-8')[:-1]
            # print(message)
        except UnicodeDecodeError as e:
            self.transport._write(str(e).encode('utf-8'))
        else:
            self.handle(message)

    def handle(self, message):
        out = self.room.command(message)
        if self.room.status() == 'dead':
            out = out + '\n' + 'You died!'
            self.transport.write(str.encode(out))
            self.transport.close()
        elif self.room.status() == 'escaped':
            out = out + '\n' + 'You escaped!'
            self.transport.write(str.encode(out))
            self.transport.close()

        # print("Send: %r" % out)
        self.transport.write(str.encode(out))

        if message == 'quit':
            # print("Close the client socket")
            self.transport.close()
コード例 #2
0
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        print('Connection from {}'.format(peername))

        self.transport = transport

        self.room = EscapeRoom()
        self.room.start()
コード例 #3
0
class EscapeServerClientProtocol(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        print('Connection from {}'.format(peername))

        self.transport = transport

        self.room = EscapeRoom()
        self.room.start()

    def data_received(self, data):
        string = data.decode()
        string = string[:-1]
        output = self.room.command(string)
        self.transport.write(output.encode())

        status = self.room.status()
        if status == "dead":
            self.transport.write(("\r\n" + status).encode())
        elif status == "escaped":
            if string == "open door":
                self.transport.write(("\r\n" + status).encode())
コード例 #4
0
def multithread(conn):
    while True:
        # create an escape room
        escape_room = EscapeRoom()
        escape_room.start()
        # while the escape room status is locked
        # read the data from conn
        while escape_room.status() == "locked":
            data = conn.recv(1024)
            output = escape_room.command(
                data.decode())  # encode converts from bytes to string
            # send the output.encode() to conn (encode converts from string to bytes)
            conn.send(output.encode())
            #data = conn.recv(1024)
            #output = escape_room.command(data.decode())
            #conn.send(output.encode())
            if escape_room.status() == "escaped" or escape_room.status(
            ) == "dead":
                conn.send(("\n" + escape_room.status()).encode())
                conn.close()
コード例 #5
0
def new_client(conn, addr):
    while True:
        room = EscapeRoom()
        room.start()
        while room.status() == 'locked':
            cmd = str(conn.recv(1024), "utf-8")
            # print("command received:", cmd, " from: ", addr)

            out = room.command(cmd)
            if room.status() == 'dead':
                out = out + '\n' + 'You died!'
            elif room.status() == 'escaped':
                out = out + '\n' + 'You escaped!'
            conn.send(str.encode(out))

            if cmd == 'quit':
                # print("closing connection from: ", addr)
                conn.close()  # close connection
                # s.close()  # close socket
                sys.exit()  # close cmd
        conn.close()
        sys.exit()
コード例 #6
0
def handle_echo(reader, writer):
    room = EscapeRoom()
    room.start()
    while room.status() == 'locked':
        data = yield from reader.read(1024)
        message = data.decode()
        # addr = writer.get_extra_info('peername')
        # print("Received %r from %r" % (message, addr))

        out = room.command(message)
        if room.status() == 'dead':
            out = out + '\n' + 'You died!'
        elif room.status() == 'escaped':
            out = out + '\n' + 'You escaped!'

        print("Send: %r" % out)
        writer.write(str.encode(out))
        yield from writer.drain()

        if message == 'quit':
            # print("Close the client socket")
            writer.close()
コード例 #7
0
def main(PORT):
	HOST='localhost'
	s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	room=[]
	i=-1
	s.bind((HOST, PORT))
	inputs=[s]
	s.listen(5)
	while True:
		read_ready, _, _ = select.select(inputs,[],[])
		for server in read_ready:
			if s == server:			
				conn, addr = server.accept()
				inputs.append(conn)	
			else:
				room.append(EscapeRoom())
				i=i+1     # create an escape room
				room[i].start()
				if room[i].status()=="locked":      # while the escape room status is locked
					data = server.recv(1024)              # read the data from conn
					datastr=data.decode()
					datastr=datastr.replace('\r\n','')
					output = room[i].command(datastr) # encode converts from bytes to string
					#print(output)
					try:					
						if output:
							server.sendall(output.encode())               # send the output.encode() to conn (encode converts from string to bytes)
						else:
							server.sendall("invalid input".encode())
					except BrokenPipeError:	
						break	
				if room[i].status()=="escaped":
					server.sendall("Congratulations! You escaped!".encode())
				elif room[i].status()=="died":
					server.sendall("Oh no! The clock starts ringing!!! After a few seconds, the room fills with a deadly gas... Sorry. You died.".encode())
				break
コード例 #8
0
 def connection_made(self, transport):
     global room
     room = []
     self.transport = transport
     room.append(EscapeRoom())
     room[-1].start()
コード例 #9
0
    s.bind((host, port))
    s.listen(1)

except socket.error as msg:
    print("Socket binding error: ", str(msg) + '\n' + "Retry....")

# establish connection with client (socket must be listening)

# send commands to client
while True:  # for keeping connection open
    conn, address = s.accept()
    print("Connection has been established ! " + "IP" + address[0] + " Port" +
          str(address[1]))

    room = EscapeRoom()
    room.start()
    while room.status() == 'locked':
        cmd = str(conn.recv(1024), "utf-8")
        print("command received:", cmd)
        if cmd == 'quit':
            conn.close()  # close connection
            s.close()  # close socket
            sys.exit()  # close cmd

        out = room.command(cmd)
        if room.status() == 'dead':
            out = out + '\n' + 'You died!'
        elif room.status() == 'escaped':
            out = out + '\n' + 'You escaped!'
        conn.send(str.encode(out))