1

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

asked Dec 26, 2017 at 16:44
3
  • You tagged this as django but you don't mention Django anywhere in the question. If you are actually using Django then you can use a Django python page to do everything and avoid mixing languages. If you are not using Django then remove the tag. Commented Dec 26, 2017 at 17:19
  • I think you've got a couple of issues here. And, to be quite honest, I'm not sure that it's a good idea to submit a user's input directly to a shell_exec call --- you ought to at least sanitize any web input. Anyway: sys.argv[0] is the first the element in the argv list. The first element of the argv list is always the name of the program being invoked. Commented Dec 26, 2017 at 17:20
  • Can you help me about solving this problem if you can do it @JawguyChooser Commented Dec 26, 2017 at 17:21

2 Answers 2

2

I assume the reason that you want to do it this way (i.e., 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.

answered Dec 20, 2020 at 14:32
Sign up to request clarification or add additional context in comments.

1 Comment

In addition, you can use something like shared database between the two applications or a shared redis server. This will give the flexibility if you want to scale later
0

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.

answered Dec 26, 2017 at 17:26

2 Comments

my username is clear and sent from AJAX like that notepad.pw/share/426sj6w8 I can print that username with using php file. Thanks for your effort
Ok, glad you're taking steps to sanitize your input. Focus on the example I gave you, if you want to print the first parameter, it's argv[1], not argv[0]. Good luck.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.