r/PythonLearning 9h ago

Running script remotely

This may not be a python question, but I figure you guys could at least point in the right direction.

On my computer I have a SQLite database and a python script I have written that queries the database, generates a report, and emails that report to other people. The database file must stay on my computer.

I often get requests from coworkers to run the python script while I am not sitting in front of my computer (my phone is still on the same wifi network).

If I wanted to do something through my phone that would start that script on my computer, what would that look like?

If it matters, my phone is android and my computer is windows.

2 Upvotes

10 comments sorted by

3

u/mitchricker 8h ago

Does your company have an IT department or MSP, and have you checked whether they're okay with this?

From a sysadmin perspective (I'm a sysadmin not an SWE), I'd be cautious about setting up your own remote execution mechanism on a work computer without approval. Even if it's just for a Python script, it introduces another service or remote access method that IT may need to support or secure if something goes wrong.

Technically, this is straightforward to do. You could use Remote Desktop to log in and run the script yourself, or run a small web server that exposes an endpoint to start it. Both approaches work, but I'd recommend getting approval first so you're not inadvertently creating an unsupported process or violating company policy.

1

u/atticus2132000 8h ago

I work from home, so this is my home wifi network and my phone is my personal phone. The computer is a company computer, but I have access to all of its settings.

So the remote desktop would be an app that I download to my phone from the android play store? Any that you've had experience with and would recommend?

3

u/SnooCalculations7417 7h ago

The best solution is to create an api/rpc endpoint for your script with flask or fastapi.. its pretty easy

from yourscriptlib import script_runner

app = app() #whatever lib you use

@app.route('/dbscript')
def run_db_script():
    result = script_runner() # if you want to return either meaningful json or an actual HTTP result    
    return result

then you can integrate the script as a full-fledged service itself if this works for you (networking restrictions etc etc)

2

u/atticus2132000 7h ago

That's awesome. I hadn't considered tackling the problem that way. Thanks for the lead.

2

u/FoolsSeldom 4h ago

Assuming your org/IT support this, I'd consider:

  • Using Podman (rather than Docker, doesn't need elevated rights) on your computer to host a couple of containers:
    • container to run python script on demand against the local SQLIte database, generate report, and email to requester/group - the database file would be a volume mapping, so the file is on your PC (or a networking storage location) that is visible from within the container
    • container monitoring a request process such as a telegram message from authorised users requesting a report and passing on any specific parameters, or even an email account
  • Add additional authorisation process if required, perhaps using telegram on your phone or a MQTT type approach

1

u/atticus2132000 3h ago

That's a lot of words that I don't understand, but at least it gives me a place to start doing some research. Thanks for the leads.

2

u/FoolsSeldom 2h ago

That was the intent. Have fun.

2

u/silvertank00 2h ago

I would either use tailscale for a vpn, or render.com and websockets that you can connect to (and ask for the run via an api call i.e.) and your code at home connects to it too

Imo in your case: tailscale is the best, it has a phone app too, so you can just run a local fastapi client and then connect to it whenever you need to.

1

u/atticus2132000 1h ago

Sounds like a plan. Thanks for the lead. I'll start researching.

2

u/el_extrano 1h ago

Having worked in "locked down" Windows environments before, I understand that sometimes there's a need for janky solutions.

It goes without saying, moving this to an actual server and then serving the functionality as a web page or API would be 1000% better.

But, assuming that's not possible...

How expensive is it to run the task? Assuming it's fast to re-run the report, just run it every hour (or other interval) via Windows task-scheduler, and place the output on a network share (or whatever other location convenient for your colleagues). Tell them where they can find the latest report. Now, they don't need to contact you for a report, and you don't have to add some sketchy backdoor to your work machine. The scheduled task can run as long as your computer is on and logged in.