CLI clearing screen every time I push a key

58 Views Asked by At

I upgraded from cmd to cmd2 and I am running into a issue. My application is a server and once it receives a connection it prints the following to the screen.

Connected with 127.0.0.1:56653

Now I am using netcat for testing as the client for connection and sending to the server. It will print everything I send to it, but then soon as I enter any key on the server, it is all removed as if I cleared the screen.

import cmd2
import argparse
import sys
import socket
from _thread import *

def recv_data(conn, addr):
    while True:
        data = conn.recv(1024)
        print(data)

def accept_clients(sock):
    while True:
        conn, addr = sock.accept()

        print("\nConnected with %s:%s\n" % (addr[0], str(addr[1])))
        start_new_thread(recv_data, (conn,addr))

def start_socket(ip, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("Socket created")

    try:
        port = int(port)
    except ValueError:
        print("Invalid port number.")
        return

    try:
        sock.bind((ip, int(port)))
    except socket.error as msg:
        print("Bind failed. Error Code : %s" % (msg))
        return

    print("Socket bind complete")
    sock.listen(5)
    print("Socket now listening")

    start_new_thread(accept_clients, (sock,))


class CLI(cmd2.Cmd):
    def __init__(self):
        cmd2.Cmd.__init__(self)
        self.prompt = "Server> "


    def do_listen(self, arg):
        if not arg:
            arg = '-h'

        parser = argparse.ArgumentParser(description="Content goes here for information.", epilog="Examples and stuff go here.")
        parser.add_argument('-i', action='store', required=True, dest='ip', help='IP address to listen for connections.')
        parser.add_argument('-p', action='store', required=True, dest='port', help='Port to listen for connections.')
        results = parser.parse_args(arg.split())

        start_socket(results.ip, results.port)


    def do_quit(self, arg):
        sys.exit(1)
    def help_quit(self):
        print("Terminates the application.\n")

cli = CLI()
cli.cmdloop()
0

There are 0 best solutions below