Jump to content

Hello everyone,

I am trying to create my first php site (locally) that should have two buttons that each execute a python script when clicked. Since I ahven't really worked with php yet I don't really know where to start here. I already have the php script active that prints out the current time and is available over the local ip so I already know how to do that 😄

Any help is appreciated

 

Thanks in advance

Quote or tag me( @SEAL62 ) if you want me to see your reply

consider a reaction if I was funny, informative, helpful, or agreeable

 

OS: Windows 10 Pro

CPU: Intel i9-9900K GPU: Aorus GeForce RTX 3080 Master Motherboard: Gigabyte Z390 Aorus Master
AIO: Corsair H150i RGB Platinum RAM: Corsair Vengeance RGB Pro 32GB 3000MHz Case: Corsair iCUE 465X RGB PSU: Corsair RM750x White

 

OS: Kali Linux

HP Envy x360 Convertible

CPU: Intel i5-10210U GPU: NVIDIA GeForce MX250 RAM: 16 GB DDR4 2666 SSD: 512GB PCIe

Link to post
Share on other sites

Configure your web server to handle php scripts and py scripts 

Then your php code can simply output html code on the page ..  you can have either simply links  or you can make buttons 

 

<a href="http://ip:port/your_python_script.py" alt=""> Text for the link </a>	

 

You can fake a button on the page, by setting CSS rules for your link ex border 1 pixel solid black to make a black rectangle around the link text,  text-decoration:none; to hide the underline for the link .. etc etc etc.

 

button

<form method="GET" action="http://ip:port/your_python_script.py"> <input type="submit" name="button" value="Text on the button..." /></form>

<form method="GET" action="http://ip:port/your_python_script.py"> <button type="submit" name="button" >Text on the button...</button></form>

 

form methods...

GET = the data in your form is sent to the script as stuff added to the url (script.php?name=John&age=15  if you had 2 input boxes named name and age in the form. 

POST = the data is submitted to the script but not added to the url, but browser treats the new page a bit differently when you do Back or hit Refresh on the page...

 

Link to post
Share on other sites

So the standard advice here is "you shouldn't do that". Giving somebody with access to your webserver the ability to run arbitrary scripts on your system isn't a good idea. Also, odds are that the better solution is to just do whatever the python scripts are doing in PHP or run the whole web server in python.

 

However, if it's just for fun/learning and you're not opening it up to anyone then you can use the exec function to do this. It would look something like this:

 

your python script saved under /path/to/my/script.py

print("Hello there, I'm a python script")

your index.php

<?php
  $output=null;
  $retval=null;
  exec('python /path/to/my/script.py', $output, $retval);
  echo "Your cool script returned the status $retval and output:\n";
  print_r($output);
?>

 

The method suggested by @mariushm will just return the text of your script, unless you specifically setup your webserver to handle the web request some other way.

 

You need to remember that the way a web browser works is it says to some server "give me the text response that goes with this input please" and then once it has the text response it can turn it into what we think of as a web page. There's nothing about writing your code in the python syntax or saving as a .py file that will, on its own, tell the web server that this text is anything more than plain old text.

 

This is why in the method I use here, the actual text of your script never leaves the web server and never needs to be sent to the client. The client doesn't care about the text of your script, they care about the output.

Link to post
Share on other sites

19 minutes ago, maplepants said:

So the standard advice here is "you shouldn't do that". Giving somebody with access to your webserver the ability to run arbitrary scripts on your system isn't a good idea. Also, odds are that the better solution is to just do whatever the python scripts are doing in PHP or run the whole web server in python.

The webserver is only meant to be hostet locally and the .py scripts simply change what list a ceratin program that is on the same machine as the webserver should use. I want this 'seeting' (meaning: changing the used list for program x) to be accessible for every user on my local network, so that lets say my wife, could click button 'List A' which would trigger script A, which would make program x use list A and vice versa.

Quote or tag me( @SEAL62 ) if you want me to see your reply

consider a reaction if I was funny, informative, helpful, or agreeable

 

OS: Windows 10 Pro

CPU: Intel i9-9900K GPU: Aorus GeForce RTX 3080 Master Motherboard: Gigabyte Z390 Aorus Master
AIO: Corsair H150i RGB Platinum RAM: Corsair Vengeance RGB Pro 32GB 3000MHz Case: Corsair iCUE 465X RGB PSU: Corsair RM750x White

 

OS: Kali Linux

HP Envy x360 Convertible

CPU: Intel i5-10210U GPU: NVIDIA GeForce MX250 RAM: 16 GB DDR4 2666 SSD: 512GB PCIe

Link to post
Share on other sites

As an observation, exec and other functions that launch programs are often disabled in the default configuration of php, precisely for security reasons.

 

Also, the way the code above uses exec, it could cause problems... For example, let's say you launch the php script by clicking on a link on a page, and then the php script runs exec, waits for python to finish and return something.  A person with bad intentions could easily click the scroll wheel over the link (to open link in new tab) and open the php script 20-30 times as fast as he/she can click the scroll wheel, and then each php script would in turn open python, and each python would run your python script which in turn  may read a configuration file or a playlist and multiple copies of your script start overwriting the same file and it all ends up a mess. 

Keep in mind that your server may also be busy, and it may take 5-10 seconds for python to be loaded and run the script and return ... so user may see the php page loading for a long time and think something crashed and hit reload...

 

If you want to keep at it with exec or other such methods, the php script should save whatever parameters were entered on the page (if any) to a temporary file and launch python... the python script would read the temporary file and do stuff with those parameters...  when it's done and you get your answers, delete the temporary file.

 

If another copy of your php script is launched (by opening a new tab or whatever), this other copy would detect the temporary file and if it's not older than let's say 30s (just in case the previous php script crashed and didn't delete the temporary file) then you don't do anything because you assume the other instance of your php script is still working on the request. 

 

This is kinda crude way of doing it, and it doesn't account for multiple people accessing that functionality at the same time.  If you want to be multi-user aware, you should read about Sessions or cookies.

 

Anyway yeah ... that's why my first line was "Configure your web server to handle php scripts and py scripts " use mod_wsgi with Apache to run python scripts, or use fastcgi or other methods... php support in apache is super easy, just add the dll / .so file to the configuration and add handler for .php files and add index.php / default.php to the DirectoryIndex list 

 

 

Link to post
Share on other sites

1 hour ago, mariushm said:

As an observation, exec ...

Thank you those are some valid points that I didn't really think about.

Quote or tag me( @SEAL62 ) if you want me to see your reply

consider a reaction if I was funny, informative, helpful, or agreeable

 

OS: Windows 10 Pro

CPU: Intel i9-9900K GPU: Aorus GeForce RTX 3080 Master Motherboard: Gigabyte Z390 Aorus Master
AIO: Corsair H150i RGB Platinum RAM: Corsair Vengeance RGB Pro 32GB 3000MHz Case: Corsair iCUE 465X RGB PSU: Corsair RM750x White

 

OS: Kali Linux

HP Envy x360 Convertible

CPU: Intel i5-10210U GPU: NVIDIA GeForce MX250 RAM: 16 GB DDR4 2666 SSD: 512GB PCIe

Link to post
Share on other sites

1 hour ago, SEAL62 said:

The webserver is only meant to be hostet locally and the .py scripts simply change what list a ceratin program that is on the same machine as the webserver should use. I want this 'seeting' (meaning: changing the used list for program x) to be accessible for every user on my local network, so that lets say my wife, could click button 'List A' which would trigger script A, which would make program x use list A and vice versa.

Your best bet is probably to do the whole thing in PHP or if python is what you know best, just do the webserver in python as well.

 

Writing your whole application, web server and all, in python is what I recommend. It's what I do for small projects. I have a standard python webserver that is extremely simple and I just treat it as a template.

 

I've been meaning to put it on github for a while now, and so I went ahead and did that today: https://github.com/PatrickTCB/super-simple-python-webserver

It works on it's own by just running `python3 -u server.py` but I'm a docker fan, so I've included a Dockerfile and some the scripts you'd need to build and run it as a container.

 

In case you also use Nova as your text editor, there a build & run tasks are already mapped to the respective buttons.

Link to post
Share on other sites

If you still want to go with your original idea, I'd recommend using quickserv since you're only going to use it locally, it should allow you to simply define your python scripts as regular http routes.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×