2

I am trying to build a web interface to execute some python scripts. One such script is used to login to network devices and run some commands then saves them to an excel file.

Im unsure if this is the right way to go about it but I have made the script into a management command.

I can get the script to run from a view by using call_command() but I am stumped on how to pass the form fields as variables to the script.

Here is what I believe to be the relevant parts;

form.py

class BaseLineForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Required', 'size': 20}), error_messages={'required': 'Required'})
    cec_pass = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Required', 'size': 20}), error_messages={'required': 'Required'})
    enable_pass = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Required', 'size': 20}), error_messages={'required': 'Required'})
    <additional form fields snipped...>

views.py

from baseline.forms import BaseLineForm
from django.core.management import call_command

def baseline(request):
    if request.method == 'POST':
        form = BaseLineForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['cec_pass']
            enable = form.cleaned_data['enable_pass']
            call_command('baseline')

baseline.py

from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    args = ''
    help = ''

    def handle(self, *args, **options):
        <I have tried to import the view but I get an error cannot import name views>
        from baseline import views <if I run this from the shell it imports fine>
        <rest of script here>

Environment; Centos 6.4, virtualenv, django 1.6, python 2.6

I am really stumped on this so thank you in advance to anyone who has any ideas to help.

Santosh Ghimire
  • 3,087
  • 8
  • 35
  • 63
  • Why don't you just run the script from your view in a separate thread and have the script post the results to another view which can handle that response accordingly. This would all be done asynchronously so it does not bog down your web app – arnm Jan 10 '14 at 04:49
  • @Alexei Nunez do you have an example of this ? In the future I am planning to use celery for task queuing. Do you have any ideas on passing the form fields as variables to the script ? – Bradley Searle Jan 10 '14 at 05:01
  • No I don't have an example but I don't see why it wouldn't work. I'm not saying it's the best suggestion but it will work. How to run script from python: http://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-args. Do the post with this: http://docs.python-requests.org/en/latest/ – arnm Jan 10 '14 at 05:08
  • just get the data from the form by doing: form.cleaned_data['some_field'] and pass it to your script – arnm Jan 10 '14 at 05:11
  • Thanks @Alexei Nunez for the advice. Maybe i'm missing something really obvious here but its the 'pass it to the script' part that i am having trouble with :) – Bradley Searle Jan 10 '14 at 05:28
  • 1
    If I understand you correctly you want to get field values from a form and pass them to a script, correct? If so, extract the values from the form with "field_value = form.cleaned_data['some_field']" and then pass the values to your script like this: "os.system('you_script.py ' + field_value)" – arnm Jan 10 '14 at 05:32

1 Answers1

2

This might help someone in the future so I am adding this here.

add a task in task.py file in the main project directory

@app.task(name='tasks.get_baseline')
def get_baseline(args):
    result = call(args)

import the task into the view

from nettools import tasks

create a list of args

args = ['/path/tp/script/get_baseline.py', username, password, enable, user_email, device]

run the task

tasks.get_baseline.delay(args)