1

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()
Doritos
  • 403
  • 3
  • 16
  • 2
    Note usre if it's related but I'd be wary of doing `from cmd2 import *` if you're only using one or two things (e.g. `Cmd`) in case you're polluting your namespace unexpectedly. – match Oct 31 '18 at 12:32
  • I fixed it, but issue is still present. – Doritos Oct 31 '18 at 12:39
  • There are some other bugs - where is `recv_data` defined? Make sure the posted code is actually what you are running! – match Oct 31 '18 at 12:44
  • I added the function, but it is not needed to replicate the issue. Without the function it will print a error saying it can't find the function blah blah. But once you go back to the server and enter any key, all of it gets removed and it goes back to a blank prompt (Server>). Run this with "listen -i localhost -p 5555" and connect with netcat, send some data and the server prints it, then enter any key on server and it is gone. – Doritos Oct 31 '18 at 12:49
  • If recv_data doesn't exist, you get no output on the server since there's no method handling the received data. Please post a full example, including the `recv_data` method or we can't debug the issue! – match Oct 31 '18 at 12:55
  • As mentioned above, I have added the function. And the server will still print a error to the servers stdout. Which will be removed when you enter any key on the server. – Doritos Oct 31 '18 at 12:57
  • I'm not getting the problem you describe - when I send texct with nc it appears on the server console - if I then type anything (e.g. 'help') it (and the resulting output) appear on the line below, with the original output left intact. This is python 3.6.6 with cmd2 pulled in via pip, if that makes a difference? – match Nov 01 '18 at 10:09

0 Answers0