Getting an output from Python that gets executed by PHP

Viewed 56

So my issue is that I have an AWS EC2 instance running Ubuntu server 18.04 and Apache2, when a button is pressed it posts some variables which are passed to a function which then uses shell_exec() to execute a Python script. The Python script then takes these variables (direction and angle) as command line arguments, it will then attempt to write then to a file called cmds.txt. When doing this however I get this error reported back to me:

    Traceback (most recent call last):
  File "/home/server/cmdWriter.py", line 45, in 
    main()
  File "/home/server/cmdWriter.py", line 41, in main
    servoCmds(direction, angle)
  File "/home/server/cmdWriter.py", line 28, in servoCmds
    cmdFile = open("cmds.txt", "a")
PermissionError: [Errno 13] Permission denied: 'cmds.txt'

After some looking I think this is because PHP executes as user "www-data" which doesn't have write privileges, so after looking at other questions I tried settung up permissions so that www-data has read and write privilages to the folder and the python file. For whatever reason this does not work! I've been pulling my hair out trying to get this to work, trying suggestions from several other questions, can anyone help me here?

1 Answers

You are getting the error with respect to 'opening' the file. You have to make sure that the permissions on the .txt file are at 755 or higher. If the server you are using is Linux, then you have two alternatives to modify the permissions:

  1. If you have cPanel, you can use the cPanel interface and just change permissions by going to the file, click on Change Permissions and then set the permissions to 777.

or

  1. From the linux command line, use chmod command - like this:
chmod 777 cmds.txt 

I can't speak to the security issues associated with same without knowing more.

The other possibility is that you are using the 'a' mode, but the file does not already exist. If you are not certain the file exists when the command is executed, you may try 'a+' as that will create the file if one does not already exist.

Does that work?

Related