0

I have a django website hosted on raspberry pi using apache server.

Is there any way I can execute python scripts on the pi server and get the output of the scripts in the website?

I have link to all the python files on the website but when I click them instead of executing the raw code opens.

  • It sounds like your apache server is setup to just return the files in a directory when you browse to that directory's url. What you want is create a url pointing to a django view that executes the script chosen (which would be passed as a query parameter to the view). But you should show us your Django code and be more specific about your issue. – dirkgroten Aug 21 '18 at 11:08
  • Yes this is true. But I don't know what should be written in that django view so that it can execute the script on server and return the result as httpresponse. – Harshit Agarwal Aug 21 '18 at 11:21
  • well hello, django is written in python and everything you run in a view is a python script. I don't understand the problem. If your python script defines a function, you just import it and run the function, passing the return value to a variable that you can then display in your template. – dirkgroten Aug 21 '18 at 11:32
  • I need to run script that is on the server from the django view and get the output back. – Harshit Agarwal Aug 21 '18 at 11:47
  • 1
    That’s exactly what my previous comment answers, how does this not help? – dirkgroten Aug 21 '18 at 12:04

1 Answers1

0

Aside that this is a very risky thing to do, you can do this in different ways - see these answers, where the second one also shows how to fetch the output:

How can I make one python file run another?

Running shell command from Python and capturing the output

ger.s.brett
  • 3,267
  • 2
  • 24
  • 30