1

I am building an application in Django and want to run a .php script I already have made earlier. I need to be able to give along a string to the .php file and catch the returning output. Does anyone have an idea how to do this? Can I make some kind of http request and catch the response?

p.s. I already tried converting the php file to python script but I couldn't get it to work..

2
  • I doubt that it's impossible to convert the PHP script to Python Commented May 30, 2011 at 0:31
  • 1
    Didn't say it was impossible, but that I couldn't. So i figured it would be easier to just call the php file. Commented May 30, 2011 at 0:36

2 Answers 2

8

try this one, i've found it here

import subprocess

#simple caller, disguard output

subprocess.call("php /path/to/my/old/script.php")

# if you want output

proc = subprocess.Popen("php /path/to/my/script.php", shell=True,
stdout=subprocess.PIPE)

script_response = proc.stdout.read()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but how would I be able to insert a input with this method? I have to input a string, catch that in the php file and then catch the output again in the python script. Any idea?
@Javaaaa proc = subprocess.Popen("php /path/to/my/script.php?myinput=whateverhere", shell=True, stdout=subprocess.PIPE) what you tried that?
I am using Popen in django but getting an error as follows "Errno 2] No such file or directory" Exception Location: /usr/lib/python2.7/subprocess.py in _execute_child, line 1259
0

If it's a php file served by web server (I assume that from your HTPP request question) do something like this:

In [1]: import urllib2
In [2]: resp = urllib2.urlopen('http://www.starenka.net/ip')
In [3]: resp.read()
Out[3]: 'ip: 62.245.xx.xx\nlocal ip: 62.245.xx.xx\nhostname:  ip-62-245-xx-xx.net.upcbroadband.cz\n'
In [4]: resp.info()
Out[4]: <httplib.HTTPMessage instance at 0x9f8720c>

1 Comment

Thanks, but how would I be able to insert a input with this method? I have to input a string, catch that in the php file and then catch the output again in the python script. Any idea? – Javaaaa 0 secs ago edit

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.