0

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

1
  • Isn't there a return missing at the end? Just guessing. Commented Aug 30, 2017 at 0:06

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.