Python remote trigger Scripts on server anytime

I’ve got some automations, such as getting daily electricity Spot Prices emailed to myself on a PDF chart sent to me at specified times, in this case daily at 6am using Task Scheduler on the server to run scripts at the specified time.

I wanted to be able to run them at a time of my own choosing. So started to explore ways in which that could be done.

There are cases where scheduled automations at set times are useful, like getting sensor data and posting it to my database, to be displayed using Google Data Studio. This process, over time, gathers historical data about the sensors and their performance in different conditions.

There are times when you want access to the information at different time intervals. Such as current share prices, current weather, tides etc. To be able to do this with the task scheduler setup you have to be on your PC and remote connect (with Remote Desktop Connection, RDC) into your server and trigger the scripts manually with Task Scheduler. A rather inconvenient process.

I wanted to be able to remote trigger the scripts using something like the RDC method, but using powershell or command prompt to begin with , then develop a .BAT file on my PC that would open a connection to the server and run a .BAT file that would run the scripts on the server. I spent a while trying to get that process to work but in the end found that it wasn’t meeting my requirements.

A big button to press

After thinking about it a bit more I decided that what I really wanted was a BUTTON! A button on a web page, that when I pressed it, it would trigger the script on the server and run the python scripts. This seemed to be a more fruitful avenue to pursue,and it also appealed to me because you could access this method on any OS system and device that had an internet connection and browser.

On my VAST website that I test ideas out I’d already got these type of processes running so I thought the method should work ok. So I asked Bing Crosby (aka Bing Chat) to write me an html file with a button that would trigger a script. It gave me a couple of options initially with JavaScript.

I was OK with JavaScript, although I have trouble coding with it, it does what you want as far as triggering events from a button.

Bing Crosby (aka Bing Chat) initially gave me a script that used the following method:


When I ran it, nothing happened, and after some research found that it needed node.js to be running as a server on my VPS (Virtual Private Server) to be able to activate the script.

I asked if there was a python solution and Bing Crosby (aka Bing Chat) said that I could use Flask or Django to activate the script.

Managing Servers

It seemed that every solution that I looked at needed a server in the background to run the script, that being , start python.exe then run the python script:


I presume it needed the server as it’s a 2 step process.

I’m a bit cautious about servers since playing with OpenMain where I had to try and get 5 servers all running together and to talk to each other, so every time I rebooted the VPS I had to also start all of the servers, I’d do it manually.

So I didn’t want to have to remember to start the Flask server every time I rebooted the VPS.

I followed Gyan Of Python’s Python Flask Application On IIS | Deploy Flask Application On IIS:

In the video there in a github link to some files for helping to set up the program.

Once I’d got this setup it actually ran as a site, similar to my other websites. To give it a URL I initially set it up with my server IP address as the main link to click on, which would then open a web page with a button to click on. Once the button was clicked it would trigger running the script and activating the automation that would:

Take information from an API, put it into a dataframe and reconfigure and process it, create a plot in Matplotlib and then save that plot to a pdf file and then email to me the pdf file as an attachment.

It is easy to get the URL running from a browser opened from the server, but when you try to access it remotely there were a lot of issues surrounding CORS (Cross Origin Requests) that are discussed in the video. I added a flask-cors library to my Flask app to try and resolve this issue, as well as adding a certificate so that the site could be made HTTPS:// instead of HTTP:// and although the certificate was not specific to the site and the browser reacted to the incorrect certificate you could still access the site and press the button.

So as a proof of concept (PoC) I was happy with the result.

Part 2. Setting up process on a subdomain

After the PoC above I decided that this was a useful process that I’d use in the future, so I wanted to be able to set it up on its own domain or sub-domain.

This caused a bit of grief as I have not been playing with domains for a while and forgot how to setup subdomains for a site, so I went charging in and got lots of grief.

I initially tried setting up subdomains from inside the server and on IIS, and these worked as long as I was using the server browser to access information as this was accessing local host (the server itself) but as soon as I tried to access them remotely I kept on getting UNKNOWN_HOST errors. So I wasted quite a lot of time trying to figure out what was happening.

The HOSTS (C:\Windows\System32\drivers\etc\hosts) file is a useful file, where you have to add the subdomain of the local server IIS only made subdomain to the list, for oit to work, see the video above on how to do that.

So I tried a very simple site that was just a HTML page and tried to connect to it, and kept on coming to the same issue above.

So I started to do some research on subdomains and suddenly realised my error. I should have been going to the site where the domains were being hosted, in this case cloudflare and freenom. As soon as I added a new “A” type record with the subdomain name, then tried creating a new site on IIS (Internet Information Services) Manager and it worked fine, it recognised the host and ran fine.

I had to reinstall Flask app etc in a subdirectory of the domain I want it to run as a sub-domain. So that took a little bit of setting up, just time consuming rather than complicated, although getting all the permissions is an important step for making it work.

So, the 2nd part of the process now allows me to run this webpage.

Further development

Now I have a functional button on a web page that I can access from anywhere on a browser, I can extend it further by adding more buttons (eg image above) to trigger other scripts that I may want to run at an arbitrary time.

With automations that send me emails with information, either embodied in the email or as an attachment, the recipient is myself. I could look at doing a generalised one that would be something like a newsletter, where someone coming to the site could add their own email and instead of sending me the information, it would send it to them instead.

For the ephemeris which uses the python ephem library to calculate the position of the planets at specific times depending where you are on the Earth, all you need to do is add your own latitude & longitude locations and your own email address for the process the information and send the table and plot there.

End comment

This has been a bit of a frustrating process, lots of challenges along the way, for something that on the surface, is quite a simple thing, trigger a script at ones own preferred time.

Now that its up and running I can add other buttons to activate other scripts that I find are useful and want access to. Things like weather and tide.

The appeal is that it’s a simple way to access bespoke information that you have tailored to your personal needs and if its information either web scraped or from an API, then you can gather it at a time that suits you and in a format that is tailored to your personal preference.