drk7700
drk7700
2018-07-16 11:57

使用shell_exec写入Python中的文本文件

已采纳

So first off, sorry if this question has already been answered elsewhere, but I couldn't find the answer myself.

I've got a LAMP server running in Ubuntu 16.04 and I'm trying to use Python 2.7.12 to write to a text file from PHP.

I've got the following two files (note, these two files are in the same directory):

index.php

<?php
    $command = escapeshellcmd('python test.py a b c');
    $output = shell_exec($command);
    echo $output;
?>

test.py

import sys

print "argv1: ", sys.argv[1]
print "argv2: ", sys.argv[2]
print "argv3: ", sys.argv[3]

f = open("test-" + sys.argv[1] + ".txt","w+")
f.write("test")
f.close()

print "Text added."

When I navigate to the index.php file in my browser, I get the following output:

argv1: a argv2: b argv3: c

However, when I execute this code in the terminal the file test-a.txt is created and I get the following output:

layer8@alpha:/var/www/html/$ python test.py a b c
argv1:  a
argv2:  b
argv3:  c
Text added.

I can't seem to understand and work out why the Python file isn't executing any code past the third print argv3 statement?

I've done a lot of research into this and I've been unable to find a solution. If you need any further information from me, please do not hesitate to ask; any responses are appreciated.

Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzhuo3059 dongzhuo3059 3年前

    shell_exec will only return what get's printed to stdout. If an exception arises in the Python script, the whole exception message and traceback are printed on stderr, so they won't be part of the output. Note that theoretically, shell_exec should return NULL if an error happens but well, this is PHP so it wouldn't be much fun if the builtin funcs behaved as documented...

    Anyway: the problem is very probably with opening the file, and is very probably a permission issue.

    First, you're using a relative path, and relative path are resolved against the current working directory, not against the directory where your script is located, so if you expect a sane and predictable behaviour always use an absolute path (either pass it as argument or build it against the script's current location).

    Also your Apache server may not (this is an understatement) run with the same env and user as you so it might just not be allowed to open a file for wrting in whatever-his-own-current-working-directory is at that time.

    And finally, even it's PYTHONPATH might be different than yours so it might even be executing another (leftover) test.py OR a stale test.pyc file.

    To make a long story short: it's impossible to tell for sure what's wrong without having access to your system, but at least now you know where to start debugging.

    The first thing I'd personnaly do would be to make sure any error in the python script is printed to stdout:

    import sys
    
    print "argv1: ", sys.argv[1]
    print "argv2: ", sys.argv[2]
    print "argv3: ", sys.argv[3]
    
    try:
        fname = "test-%s.txt" % sys.argv[1]
        with open(fname,"w+") as f:
            f.write("test")
    
        print "Text added."
    except Exception as e:
        print "oops : got %s" % e
        raise
    
    点赞 评论 复制链接分享