I am trying to write a Python script that will submit my id and filename and retrieve some info based on the filename. The "Cmd" module should prompt me for both an id and filename. However, so far this is the script that I have written. I have to manually type in my id in the script and once I run this script, it prompts me for a filename. Is there any way in which I can make this script to prompt me for both id and filename (and what if the filename contains spaces).
import requests
from cmd import Cmd
class Term(Cmd):
prompt = "promtp >>> "
def default(self, args):
id = "someid"
resp = requests.post('http://10.10.10.10/login',
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={"username": name, "filename": f"{args}"})
def do_quit(self, args):
return 1
term = Term()
term.cmdloop()
The part where the filename contains space is a bit tricky.
>Solution :
When typing your name in the prompt, also type you id, like this:
promtp >>> your_name your_id
The args in the default method is actually a string, so you should change for singular, like arg.
Then, you have to split the string in arg so you can get the two words passed at the command line. Like this:
def default(self, arg):
args = arg.split()
name = args[0]
id = args[1]