How to test python script within PHP
<p>I am working on a .php file that takes a python function as an input. Then it checks the function if it returns the correct output.</p>

<p>First, I created a form in func.php. In the form, there is a textarea to input the function.</p>

<pre><code> &lt;textarea id="function" name="function" rows="10" cols="70" placeholder="Enter your function here..." required&gt;&lt;/textarea&gt;
</code></pre>

<p>So the user types his/her function(python) and then makes clic on summit button. Next the function is store into a variable.</p>

<pre><code>$func =$_POST['function'];
</code></pre>

<p>After the python function was saved, I want to test the function and see what it outputs. Assume I already have some parameters to test it.</p>

<p>I read some articles and they said that I can use exec() to execute a python script, but according to those examples it executes a .py file. In my case, I don't have a .py file but only the python function. I was thinking on creating a .py file within PHP using fopen and save my function there.</p>

<p>Python Code(*)</p>

<pre><code>#!/usr/bin/python
import sys

#printme function was the function that was input in the textarea
def printme( str ):
   print str
   return
if(__name__=='__main__'):
   printme(sys.argv[0])
</code></pre>

<p>Then execute the .py file using exec()</p>

<pre><code> exec("python myfile.py Monday",$output);
</code></pre>

<p>But I don't know if fopen can open a .py file. I also tried to store the python code(*) in an string variable named $code and run it.</p>

<pre><code> exec("python $code Monday",$output);
</code></pre>

<p>But it didn't work.
Please can someone help with this? Thank you in advance</p>