简体   繁体   中英

Pass data from php to python and get result

I want to send data from php to python and make some computations. After that I want to send result of that. The problem is I cannot send data from php to python.

python.php username is working but shell_exec or python have problem

<?php
if(isset($_POST["username"])){

    $nick = $_POST["username"];
    echo shell_exec("python new.py '$nick'");
$jsonData = $_POST["prediction" ];
echo $jsonData;
}
?>

new.py When I run python it prints C:\\wamp\\www\\MLWebsite\\website\\new.py but it should be parameter

import pymysql.cursors
import sys
import urllib2, urllib
import requests

x=sys.argv[0]
print x

I want to get some idea about sending result because end of new.py

mydata=[('prediction','BIO')]
mydata=urllib.urlencode(mydata)
path='http://localhost/MLWebsite/website/python.php'    #the url you want to POST to
req=urllib2.Request(path, mydata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page=urllib2.urlopen(req).read()
print page

I use Firebug plugin in Firefox and this error is also shown in webpage.


( ! ) Notice: Undefined index: prediction in C:\\wamp\\www\\MLWebsite\\website\\python.php on line 6 Call Stack #TimeMemoryFunctionLocation 10.0006245144{main}( )..\\python.php 0 0

I assume the reason that you want to do it this way (ie, using PhP to interact with user but having Python actually do the processing) is that you want to take advantage of python language for some tasks, but avoid having to use a separate webframework just for those tasks.

One way to accomplish it (albeit perhaps not the way you want to solve it) is to have PhP write the data to a text file with delimiters separating different chunks of data. Then have PhP call the Python file, which knows to read the text file.

In my example below Python writes to a file and PhP can open it if it wants, but you can go the other way as well. PhP could write to a .txt file, Python can read and manipulate, and then save to the same or different .txt file, and PhP can open and render the results.

Basically, you are using a .txt file as 'memory'.

This is an example:

<?php

echo "<h1>This is PhP!</h1>";

$returnedValue = shell_exec('/home/sitename/public_html/pythonFile.py');

echo $returnedValue; //This line may not be needed if there is nothing to return.

echo "<h2> Completed </h2>";

//Once the 'Complete' Above Renders in the Browser You Know that Python Did Whatever it Was Going to Do to the .txt File

//Now, if you want to have PhP Open the .txt File and Display it You Can

?>

#THIS IS PYTHON
#!/usr/bin/env python

file_object = open("NameOfTextFile.txt", "w+")

file_object.write("Hello World!")

file_object.close()

I realize this question is old, but I recently had the same issue and this is how I tried to resolve it. Hopefully it helps someone.

I think your question needs refinement.

From what I can tell, your python program is doing what one would expect.

$ cat 0.py 
#!/usr/bin/python

import sys
print sys.argv[0]
print sys.argv[1]
$ chmod 755 0.py
$ python 0.py foo
0.py
foo
$ ./0.py foo bar
./0.py
foo

So, if your python program is prining 'new.py' as you wrote the question, I think that's expected behavior. Why you're passing unsanitized user input to a system call is another question. Why you're using a system call at all (why not set up a webservice with your python program?) is yet a further question.

I hope this helps.

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