-2

Hello so im having trouble with keeping my application open and repeating my input function and getting the input from the user itself. heres my code

import socket
import time
import sys

HOSTNAME = socket.gethostname()
CREDITS = "Developed by SnoDev"

print("Welcome " + HOSTNAME + "!")
time.sleep(1)
def cmd():
   input("Command Bar: ")
cmd()

i want to get input from the function cmd() but i dont know how to, can anyone help?

SnoDev
  • 5
  • 1

2 Answers2

0

Here is something. I added a few commands like DIR or ping. Also, recursion is not good. While loop is better.

import socket
import time
import os
import platform    # For getting the operating system name
import subprocess  # For executing a shell command

def ping(host,num="4"):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    # Option for the number of packets as a function of
    param = '-n' if platform.system().lower()=='windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, num, host]

    return subprocess.call(command) == 0

HOSTNAME = socket.gethostname()
CREDITS = "Developed by SnoDev"

print("Welcome " + HOSTNAME + "!")
time.sleep(1)
while True:
    var1=input("Command Bar: ")
    if "ping" in var1.lower():
        var2=var1.lower().split()
        
        if len(var2)==1:
            print("Usage: ping [-number]\n\nOptions:\n\t-number\t\tPing the host for specified number of times.\n")
        elif len(var2)==3:
            print(ping(var2[1],var2[2][1:]))
            #print(ping(var2[1],var2[2]))
        else:
            print(ping(var2[1]))
        
    elif var1.lower()=="exit":
        break
    elif "dir" in var1.lower():
        var3=var1.lower().split()
        if len(var3)==1:
            print("Usage: DIR [-path]\n\nOptions:\n\t-path\t\tList all folders and files inside the specified directory.\n")
        else:
            x=os.listdir(var3[1])
            print("\nDirectories and files found: \n")

            for i in x:
                print(i)
            print("\n")
        
    elif var1=="":
        pass
    
    else:
        print(f"Error: {var1} is not recognized as a command.\n")
        

-1

Use recursion , you can break it whenever you want

import socket
import time
import sys

HOSTNAME = socket.gethostname()
CREDITS = "Developed by SnoDev"

print("Welcome " + HOSTNAME + "!")
time.sleep(1)

def do_something_with_input(res):
   pass

def cmd():
   res = input("Command Bar: ")

   do_something_with_input(res)
   
   return cmd()

cmd()
Wrench
  • 478
  • 2
  • 13
  • alright i have that. im just trying to find where the input is? heres my code – SnoDev Jun 17 '21 at 06:11
  • ` import socket import time import sys HOSTNAME = socket.gethostname() CREDITS = "Developed by SnoDev" print("Welcome " + HOSTNAME + "!") time.sleep(1) def do_something_with_input(res): pass def cmd(): res = input("Command Bar: ") do_something_with_input(res) return cmd() cmd() if do_something_with_input() >= "hello": print("hi") ` – SnoDev Jun 17 '21 at 06:12
  • recursion is not a good idea, while loops should be used instead. RecursionError – xcodz-dot Jun 17 '21 at 06:40