Henry Desai
Henry Desai

Reputation: 45

Working Python script does not work when run via PHP

I am trying to run a Python script on a web server. I have been unable to run the script directly in the cgi-bin folder (kept getting 500 server errors) so I am currently attempting to call the script via a PHP script placed in the cgi-bin folder.

I am using a php script that executes a shell command on my server:

<?php
shell_exec('python /home/stevesloane8/www/cgi-bin/test.py');
?>

This method works on a few test python scripts which I have tried, but will not work on my script.

Here a portion of my script:

#!/usr/bin/python

CATEGORIES_INDEXED = ["36"]#, "6000", "6002",]

NUMBER_TO_INDEX = 25

import requests
import mysql.connector
import datetime
import time

def get_today():
    today = datetime.date.today()
    return today.strftime("%Y-%m-%d")

def get_start_date():
    today = datetime.date.today()
    start_date = today - datetime.timedelta(weeks=2)
    return start_date.strftime("%Y-%m-%d")

def get_today_underscore():
    today = datetime.date.today()
    return today.strftime("%Y_%m_%d")

def get_token(client, secret):

    payload = {"Content-Type" : "application/x-www-form-urlencoded", "client" : client, "secret" : secret}
    auth = requests.post('https://integrations.apptopia.com/api/login', params=payload)
    return auth.json()['token']

def get_cat_ids():

    r = requests.get('https://integrations.apptopia.com/api/itunes_connect/categories', headers={"Authorization":TOKEN})

    cat_dict = {}

    for cat in r.json():
        cat_dict[cat['id']] = cat['name'].replace(" ", "_").replace("&", "and").replace("-", "to")

    return cat_dict

def pull_top_chart(cat, kind, quant):



    today = time.strftime("%Y-%m-%d")
    top_chart = requests.get("https://integrations.apptopia.com/api/itunes_connect/rank_lists", params={"id":cat, "date":today, "country_iso":"US", "kind":kind}, headers={"Authorization":TOKEN})



    top_app_ids = top_chart.json()[0]['app_ids']
    top_app_ids = top_app_ids[:quant]

    rank_dict = {i:k for k, i in enumerate(top_app_ids)}

I went through it line by line and the script worked when called by the above PHP script up until I pasted in the last line:

rank_dict = {i:k for k, i in enumerate(top_app_ids)}

After I insert this line, the Python script does not run through the PHP script. It still runs when I call it from the command line.

Because this script works when I call it from the command line, is there something particular about the operation of the PHP shell_exec function that prevents this from working. Or is there some kind of permissions issue with this being run on a web server? The script permissions of both files are set to 755.

Thanks

Upvotes: 0

Views: 1030

Answers (2)

Danish Absar
Danish Absar

Reputation: 354

Technically you must have certain OS privileges to Run such type of script through php.

If you good at php then Recently I develop a script for such type of issues kindle have a look, I hope it will help

Run Complex Shell scripts through php

Happy Coding

Upvotes: 2

Henry Desai
Henry Desai

Reputation: 45

I finally figured out what the problem was. Apache was not using my user PATH so it was calling the wrong version of Python (2.4) rather than 3.6. When I substituted the full path to the right version of Python, it worked perfectly.

Upvotes: 1

Related Questions