0

I have 2 scripts.

1 - php web-scripts which collects information from a user and execute python script with sending all collected data as arguments

2 - python script which gets arguments from php-script using sys.argv and updates a MySQL using MySQLdb

When I run python script via command line - it works like a charm. It does whatever it suppose to do. But when I try to execute the python script using PHP-script - nothing happens until I delete import MySQLdb from my python script.

As soon as I delete "import MySQLdb" from my python script - PHP script can execute it and they both work (except updating MySQL).

Do you have and idea what's going wrong?

P.S. I was trying to get an error

in python file I use this:

import traceback
import sys
import MySQLdb

# Some classes and functions

if __name__ == '__main__':
    try:
        main()
    except Exception, err:
        print traceback.format_exc()

and in the php i use:

$result = shell_exec("/usr/bin/python /FULL_PATH_TO/script.py $argument";
echo $result;

And still... Nothing. web page just reloads without giving any information

STK
  • 102
  • 4
  • 11
  • "nothing happens" -- Did you check for a traceback? – mgilson Oct 31 '14 at 06:31
  • @mgilson updated main topic regarding you comment – STK Oct 31 '14 at 06:53
  • Perhaps the environment variable PYTHONPATH is different when executed by the web server? PYTHONPATH directs python in finding the libraries... – Paul Oct 31 '14 at 06:57
  • @Paul might be a case, Thank you. Now need to figure out what should I do in order to change the PATH for web server :( – STK Oct 31 '14 at 07:33
  • Did you check php (web) error log? shell_exec() is available via web server? It's disabled with PHP safe-mode. – shoma Oct 31 '14 at 08:15
  • @shoma shell_exe() works like a charm without MySQLdb in my python file – STK Oct 31 '14 at 17:19
  • Have you tried passing a static string instead of `$argument`? Perhaps it is not escaped properly and therefore causing issues. – Terry Feb 19 '16 at 23:31

1 Answers1

0

I encountered the same problem. the key problem here, is that your python script does not output anything!

and the key to find out what happened, is that you need to know, here some error occurred but you can't see the error output. you need to check this question: PHP script can't get output from Python script

and here is the short-answer: you need to exec and put the error output(stderr) to the stdout. you need to do something like: exec('python hello-numpy.py 2>&1');

and you will see the problem, which must be that the MySQLDB lib needs some Permission which was denied.

how to aquire the Permission needed, that, is quite another question :)

Community
  • 1
  • 1
jasonchen
  • 3
  • 2