简体   繁体   中英

How can I send “user input” to python script from another python script (or php execute)?

I'm creating a website using php and XAMPP. This website accepts a python file from the user, then it needs to run that python file and receive all output from that file. I've been able to accomplish this by using shell_exec() in the php file.

The problem is getting user input for the python files. The files use both the input() and sys.stdin.readline() functions. I need to be able to automatically send the python file "user input" (sometimes several inputs). I know exactly what and how many "user inputs" I will need to send so this doesn't require my intervention.

I think one way to do this is by changing the way the submitted python files get input. If I could do this then I would have each input stored in a list that would be iterated through. Unfortunately, I cannot edit the actual input commands in the submitted files so this would have to be done at the top of the submitted python file.

Here is how the php file executes the python files.

$command = "python python_script.py";
$output = shell_exec($command);
echo nl2br($output);

I tried using the echo function (with the && for multiple inputs), but the only problem with this is the fact that the echo function can't press enter after each echo. This did however successfully match the inputs to the input prompts. Below is an example. python_script.py should take input then print it, but the cmd displayed this:

C:\>(echo 2 && echo 3) | python python_script.py 
number one: 2
number two: 3

C:\>

Now I'm trying to use the subprocess functions in a separate python file. Again the problem is "user input"; I don't know how to send inputs. The solutions to like problems have not been able to work for me, but I'm certain that this is due to my lack of understanding of the subprocess functions.

I think that subprocess is my best bet, but I would also appreciate any suggestions utilizing php. I would also prefer to not have to create a batch file for this problem.

I found a solution to this problem.

To send inputs from one python file to another (python version 3.7), I used three files.

  1. File for running the subprocess
  2. File for outputs (very simple)
  3. File that needs the inputs

Here are the three files in the same order as above.

You don't need to print out the output, but I'll include the terminal output below the file examples. The subprocess file:

from subprocess import Popen,PIPE

p1 = Popen(["python","output_file.py"], stdout=PIPE)
p2 = Popen(["python", "input_file.py"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()

output = p2.communicate()[0]

print(output)

The output file is very simple and there may be a way to work around it. Nevertheless, here is my version:

print(1)
print(2)
print('My String')

The input file requires type casting for numbers.

i = input('Enter a number: ')
j = input('Enter another: ')
k = int(i) + int(j)
print(k)
l = input('Tell me something. ')
print(l)

Here is the terminal output:

b'Enter a number: Enter another: 3\r\nTell me something. My String!\r\n'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM