Shraveen B.S
Shraveen B.S

Reputation: 3

How to make a django rest api run a python file in my computer

I am making an API for an android app. As soon as the input is given to the API, I need the API to execute a python file for me which is in my computer. I'm thinking of using my computer as a server on trial basis. I hope you are able to understand my question. Please let me know on how to run a python file from the API

Upvotes: 0

Views: 1168

Answers (1)

Alvaro Bataller
Alvaro Bataller

Reputation: 483

You can have a view that when someone visits the url it will launch another process:

from django.http import HttpResponse
from django.views.generic import View
import subprocess

class ExecutePythonFileView(View):
    def get(self, request):
        # Execute script
        subprocess.call(['python', 'somescript.py'])

        # Return response
        return HttpResponse("Executed!")

Upvotes: 3

Related Questions