Execute python in PHP and get response as i get in terminal
<p>i am new to PHP Programming. I need to execute a python code from php. My Python code contains some external modules.
The Php code is 
    

<pre><code>$op=shell_exec('python mgs.py');
echo $op;

?&gt;
</code></pre>

<p>This is my intended code and i need to get the exact output as i get in terminal(if errors ,that too)</p>

<p>python code</p>

<pre><code>import mechanize
def mgs():
    a=0
    flag=0
    browser = mechanize.Browser(factory=mechanize.RobustFactory())
    browser.set_handle_robots(False)
    browser.open("http://14.139.185.88/cbcsshrCamp/index.php?module=public&amp;page=result") 
    browser.select_form(nr=0)
    control=browser.find_control('exam_id')
    print control
    control.value=['203']
    browser.form["prn"]="130021069679"
    browser.submit()
    html = browser.response().readlines()               
    for i in range(0,len(html)):
       if 'Failed' in html[i]:                 
          flag=1
    if flag==1:
       print "Fail"
    else:
       print "Pass"



mgs()
</code></pre>

<p>. I am not getting any output on browser window, but when i tried to execute the PHP code in terminal, its fine..
ie, ( php index.php)</p>

<p>My OS> Ubuntu 
Apache 2</p>