3

I am still fairly new to the python world and know this should be an easy question to answer. I have this section of a script in python that calls a script in Perl. This Perl script is a SOAP service that fetches data from a web page. Everything works great and outputs what I want, but after a bit of trial and error I am confused to how I can capture the data with a python variable and not just output to the screen like it does now.

Any pointers appreciated!

Thank you,

Pablo

# SOAP SERVICE
# Fetch the perl script that will request the users email.
# This service will return a name, email, and certificate. 

var = "soap.pl"
pipe = subprocess.Popen(["perl", "./soap.pl", var], stdin = subprocess.PIPE)
pipe.stdin.write(var)
print "\n"
pipe.stdin.close()
4
  • 1
    What is the purpose of var? Do you want to send the script the content of var on the command line or do you want to send it to its STDIN stream? Commented Jun 9, 2017 at 17:18
  • Var is a string of characters that is outputted by the perl script. Each variable is line by line already so I am just printing it to a txt file and then reading it in and using ".read()" and ".splitlines()" to organize it Commented Jun 9, 2017 at 19:29
  • @PabloSmith Ah, that explains it, thank you. So: when you say pipe.stdin.write(var) then the object pipe that represents the process is sending the contents of var to STDIN of the script it invoked. The part stdin = subprocess.PIPE is needed when you want to feed some STDIN into a program as it runs. It sounds like you don't need that here. Commented Jun 9, 2017 at 19:50
  • This question really has none of Perl (other than yours nicely informing the reader of the context). So I've removed the perl tag and the reference to perl from the title. If you don't like this by all means please roll it back. (Click on "edited ..." above my user name and you'll see how to "roll back" to a previous version.) Commented Jul 19, 2017 at 4:11

1 Answer 1

3

I am not sure what your code aims to do (with var in particular), but here are the basics.

There is the subprocess.check_output() function for this

import subprocess
out = subprocess.check_output(['ls', '-l'], text=True)
print(out)

If your Python is before 2.7 use Popen with the communicate() method

import subprocess
proc = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE, text=True)
out, err = proc.communicate()
print(out)

You can instead iterate proc.stdout but it appears that you want all output in one variable.

In both cases you provide the program's arguments in the list.

Or add stdin if needed

proc = subprocess.Popen(['perl', 'script.pl', 'arg'],\
    stdin  = subprocess.PIPE,\
    stdout = subprocess.PIPE)

The purpose of stdin = subprocess.PIPE is to be able to feed the STDIN of the process that is started, as it runs. Then you would do proc.stdin.write(string) and this writes to the invoked program's STDIN. That program generally waits on its STDIN and after you send a newline it gets everything written to it (since the last newline) and runs relevant processing.

If you simply need to pass parameters/arguments to the script at its invocation then that generally doesn't need nor involve its STDIN.

Since Python 3.5 the recommended method is subprocess.run(), with a very similar full signature, and similar operation, to that of the Popen constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.