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.

6
  • 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 Commented Jan 10, 2014 at 4: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 ? Commented Jan 10, 2014 at 5: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: stackoverflow.com/questions/3781851/…. Do the post with this: docs.python-requests.org/en/latest Commented Jan 10, 2014 at 5:08
  • just get the data from the form by doing: form.cleaned_data['some_field'] and pass it to your script Commented Jan 10, 2014 at 5:11
  • 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)" Commented Jan 10, 2014 at 5:32

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.