Python Forum
Call CLI app with input? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Call CLI app with input? (/thread-44491.html)



Call CLI app with input? - Winfried - May-23-2025

Hello,

Does someone know how to call a CLI application in Windows with an input field?

As show, I tried calling it with a full string, an array, a byte-encoded input, to no avail.

Thank you.

import os
import wx
import subprocess
from subprocess import PIPE

#b'Password: Error reading password!\n'
CMD = "app.exe input.txt"

#b'\'"app.exe input.txt"\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n'
CMD = ["app.exe input.txt"]

#b'Password: Error reading password!\n'
CMD = ["app.exe", "input.txt"]

class MyPanel(wx.Panel):
	def __init__(self, parent):
		super().__init__(parent)

		self.my_text = wx.TextCtrl(self, style=wx.TE_MULTILINE)

		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(self.my_text, 1, wx.ALL|wx.EXPAND)
		self.SetSizer(sizer)

		#prompt for password		
		dlg = wx.TextEntryDialog(parent, 'Enter password','Password')
		if dlg.ShowModal() == wx.ID_OK:
			password = dlg.GetValue()
			password = password.encode(encoding="utf-8")
			print('You entered: %s\n' % password)
		dlg.Destroy()
		
		p = subprocess.Popen(CMD, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
		output, error = p.communicate(input=password)
		print(error)
		print(output)
		return



RE: Call CLI app with input? - Gribouillis - May-23-2025

How does the app normaly read the password? Are there command line switches to pass the password on the command line?


RE: Call CLI app with input? - Winfried - May-23-2025

It prompts the user after typing "app.exe myfile":

Password:

---
Edit: Using 1) this command with 2) a string (instead of bytes) shows no error, but the output file isn't available on disk, unlike what happens when I run the command manually:

...
if dlg.ShowModal() == wx.ID_OK:
password = dlg.GetValue()
#password = password.encode(encoding="utf-8")
dlg.Destroy()
...
subprocess.run(CMD, capture_output=True, text=True, input=password)


Edit: No better

p = subprocess.Popen(CMD, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
#b'Password: Error reading password!\n'
p.stdin.write(b"mypasswd\n")
result = p.stdout.read()
print(result)


---
Edit: Here's what it looks like when manually typed:
c:\>app.exe input.txt.xyz
Password: ********
Completed -> input.txt



RE: Call CLI app with input? - Gribouillis - May-23-2025

First read the manual of app.exe to see if there is a way to provide the password on the command line instead of being prompted for it.

If there is no way, you could perhaps try the pexpect module to simulate interaction.


RE: Call CLI app with input? - Winfried - May-23-2025

It's the first thing I checked, but it can only prompt the user.

I'll experiment with pexpect.

Thank you.


RE: Call CLI app with input? - snippsat - May-23-2025

A couple of option for Windows.
Python bindings like pywinpty will give you a real console for the subprocess.
import pywinpty

# spawn the app 
pty = pywinpty.PtyProcess.spawn(["app.exe", "input.txt"])

# write the password + newline
pty.write(password + b"\r\n")

# read everything until it exits
output = pty.read()
print(output.decode("utf-8"))
wexpect is a fork of pexpect for Windows that also drives a real console.
import wexpect

# spawn the process
child = wexpect.spawn("app.exe input.txt")

# wait until it prints “Password:”
child.expect("Password:")

# send the password and a newline
child.sendline(password.decode("utf-8"))

# read until EOF
child.expect(wexpect.EOF)
print(child.before)