Jump to content


Photo

[Answered] Running Python from Php - Alternative for shell_exec()?

php python

  • Please log in to reply
3 replies to this topic

#1 rodbm

rodbm

    Newbie

  • Members
  • 3 posts

Posted 24 August 2019 - 06:32 PM

Hey there everyone,

 

I have good knowledge of python but I'm still learning Php (well to be honest I started learning it for this purpose).

I have a Python script in /cgi-bin and it works fine when I go to mydomain.heliohost.org/cgi-bin/script.py. What I want to do is make it so that it starts from a Php file outside of /cgi-bin.

 

I tried shell_exec() but I'm getting the "Warning: shell_exec() has been disabled for security reasons..." message. I know this function can be used for malicious purposes and that's why it's blocked.

I tried some other functions but nothing seems to work or I'm doing it wrong.

 

Question: Can this be done without the use of shell_exec() by any chance?

 

I know there are some duplicate topics of this subject, but as I said I tried a few solutions posted on them and these didn't work.

 

Thanks for your attention!



#2 wolstech

wolstech

    Rank X Member

  • Root Admin
  • 10,878 posts
  • Gender:Male
  • Location:Pennsylvania

Posted 24 August 2019 - 09:14 PM

Assuming the python can be run in a browser by calling it’s URL and it producing browser-compatible output (I.e. http headers, etc.), call it via file_get_contents() using its full public URL. The function will call the script and return the output for further use.

Please do not PM me for support. Posting on our forums lets all of us learn.
Do you play Minecraft? | Server Monitors | And the Dumbass of the Decade award goes to...


#3 rodbm

rodbm

    Newbie

  • Members
  • 3 posts

Posted 24 August 2019 - 10:24 PM

Thank you wolstech.
 
I tried that before and I still can't get this function to work. The py file I'm using is as follows:

 

#!/usr/bin/python
 
print("Content-Type: text/html\n\n")
 
print("Heliohost rules!")

 

 

And the php I'm trying to make it to work is as follows:

<?php
$grab = file_get_contents("http://mydomain.heli...cgi-bin/test.py");
echo $grab;
?>

 

I get the message:

Warning: file_get_contents(http://mydomain.heli...cgi-bin/test.py): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in ../test.php on line 3

 

I with the dir path instead of the url but it just returns the full code in plain text.

Am I doing something wrong?



#4 rodbm

rodbm

    Newbie

  • Members
  • 3 posts

Posted 24 August 2019 - 10:43 PM

Edit: I fogot I had the directory privacy up for that url.

 

It works perfectly now, thanks!







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users