Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call CLI app with input?
#1
Question 
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
Reply
#2
How does the app normaly read the password? Are there command line switches to pass the password on the command line?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
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
Reply
#4
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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
It's the first thing I checked, but it can only prompt the user.

I'll experiment with pexpect.

Thank you.
Reply
#6
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)  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what would you call the input for the parameter(s) of a function you have defined? rix 3 3,456 Dec-16-2019, 12:04 AM
Last Post: rix
  How to take input in POST call from aother pthon file network2016 0 2,851 Jun-22-2017, 10:34 AM
Last Post: network2016

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020