-2

I need you guys :D I have a web page, on this page I have check some items and pass their value as variable to python script. problem is: I Need to write a python script and in that script I need to put this variables into my predefined shell commands and run them. It is one gnuplot and one other shell commands.

I never do anything in python can you guys send me some advices ?

THx

Mirko
  • 39
  • 2
  • 13
  • possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Cory Kramer Dec 08 '14 at 12:46
  • No I need to help with whole script not only this call. – Mirko Dec 08 '14 at 12:54
  • @Cyber while this is seems like calling an external command in Python with subprocess, there is certain sophistication in his question. He mentioned clearly that this is for web development. While it is easy to call subprocesses in python, we have to consider the web element. There are many concerns that need to be address and tutorials/tips out there are insufficient . These concerns could include permissions, connection timeout etc. Instead of immediately dismissing his question, I think it is important to provide some a advice and clarification for his questions, – biobirdman Dec 08 '14 at 12:55

1 Answers1

0

I can't fully address your questions due to lack of information on the web framework that you are using but here are some advice and guidance that you will find useful. I did had a similar problem that will require me to run a shell program that pass arguments derived from user requests( i was using the django framework ( python ) )

Now there are several factors that you have to consider

  1. How long will each job takes
  2. What is the load that you are expecting (are there going to be loads of jobs)
  3. Will there be any side effects from your shell command

Here are some explanation that why this will be important

  1. How long will each job takes.

Depending on your framework and browser, there is a limitation on the duration that a connection to the server is kept alive. In other words, you will have to take into consideration that the time for the server to response to a user request do not exceed the connection time out set by the server or the browser. If it takes too long, then you will get a server connection time out. Ie you will get an error response as there is no response from the server side.

  1. What is the load that you are expecting.

You will have probably figure that if a work that you are requesting is huge,it will take out more resources than you will need. Also, if you have multiple requests at the same time, it will take a huge toll on your server. For instance, if you do proceed with using subprocess for your jobs, it will be important to note if you job is blocking or non blocking.

  1. Side effects.

It is important to understand what are the side effects of your shell process. For instance, if your shell process involves writing and generating lots of temp files, you will then have to consider the permissions that your script have. It is a complex task.

So how can this be resolve!

subprocesswhich ship with base python will allow you to run shell commands using python. If you want more sophisticated tools check out the fabric library. For passing of arguments do check out optparse and sys.argv

If you expect a huge work load or a long processing time, do consider setting up a queue system for your jobs. Popular framework like celery is a good example. You may look at gevent and asyncio( python 3) as well. Generally, instead of returning a response on the fly, you can retur a job id or a url in which the user can come back later on and have a look

Point to note!

Permission and security is vital! The last thing you want is for people to execute shell command that will be detrimental to your system

You can also increase connection timeout depending on the framework that you are using.

I hope you will find this useful

Cheers, Biobirdman

Community
  • 1
  • 1
biobirdman
  • 4,060
  • 1
  • 17
  • 15
  • Thanks for your answer ;) I need to do only simple tasks. I want to do that: people upload their datafile to page - get the filename of this datafile - script run the gnuplot and visualize data on web page... I also need to do some simple task but this is now critical for me so thanks for help ;) – Mirko Dec 08 '14 at 21:19
  • @mirko then a simple subprocess call will be what you are looking for. Just becareful of the write/read permissions – biobirdman Dec 09 '14 at 00:34