Call Python From PHP And Get Return Code

Posted by seaboy on Stack Overflow See other posts from Stack Overflow or by seaboy
Published on 2010-04-28T02:59:52Z Indexed on 2010/04/28 3:03 UTC
Read the original article Hit count: 278

Filed under:
|

Hello everyone,

I am calling a python script from PHP.

The python program has to return some value according to the arguments passed to it.

Here is a sample python program, which will give you a basic idea of what i am doing currently:

#!/usr/bin/python
import sys

#get the arguments passed
argList = sys.argv

#Not enough arguments. Exit with a value of 1.
if len(argList) < 3:
    #Return with a value of 1.
    sys.exit(1)

arg1 = argList[1]
arg2 = argList[2]

#Check arguments. Exit with the appropriate value.
if len(arg1) > 255:
    #Exit with a value of 4.
    sys.exit(4)
if len(arg2) < 2:
    #Exit with a value of 8.
    sys.exit(8)

#Do further coding using the arguments------

#If program works successfully, exit with a value of 0

As you can see from the above code, my basic aim is

  1. for the python program to return some values (0,1,4,8 etc) depending on the arguments.
  2. And then the calling PHP program to access these returned values and do the appropriate operation.

Currently i have used "sys.exit(n)", for that purpose.

Am i right in using sys.exit, or do I need to use something else?

And also what method exists in PHP so that I can access the return code from python?

Sorry for the long question, but hopefully it will help in you understanding my dilemma

Thanks a ton

© Stack Overflow or respective owner

Related posts about python

Related posts about php