Exemplo n.º 1
0
def main():
    args = argv()

    if args.replay:
        replay(args.replay)
    else:
        # Create a TCP/IP socket
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Bind the socket to the port
        server_address = ('', int(args.port))
        print(colors.Green + "Server Starting on: ", server_address)
        server.bind(server_address)

        # Listen for incoming connections
        server.listen(5)

        while True:
            # Wait for a connection
            print(colors.Blue + 'waiting for a connection' + colors.White)
            connection, client_address = server.accept()
            try:
                print(colors.Green + 'connection from ', client_address[0],
                      'on Port ', client_address[1])

                # Receive the data in small chunks and retransmit it
                while True:
                    data = connection.recv(1024)

                    if data:
                        p = Parser(data)
                        tlv_blob = p.parse()
                        p.print_parsed_data(tlv_blob, client_address)
                        JSON.write_json(tlv_blob, client_address)

                        # print('sending data back to the client')
                        connection.sendall(data)
                    else:
                        print(colors.Red + 'no more data from', client_address)
                        break

            finally:
                # Clean up the connection
                connection.close()