User100696
User100696

Reputation: 707

Django - Pysftp authentication error on template

I'm trying to solve this but I have no clue how to.

I'm working with pysftp on Django. the problem is if everything goes ok, nothing happens but if I enter a wrong host, usuario, or clave. I'm still getting Success instead of Error. Something Failed. or Authentication Failed as say if I open Django console when I enter a bad host , usuario or clave. How can I make it work to get Authentication failed error in both places? Django console and Django webapp? Thanks..!!

Here's what I have:

def sftp_form(request):
    if request.method == 'POST':
        form = sftpForm(request.POST or None)
        if form.is_valid():
            data = form.cleaned_data
            host = data['host']
            usuario = data['usuario']
            clave = data['clave']
            print host
            print usuario
            print clave
            try:
                SFTP_bajar(host,usuario,clave)
                messages.success(request,' Success')
                return redirect('auth_login')
            except:
                messages.error(request, 'Error. Something Failed.')
    else:
        form=sftpForm()
    return render(request, 'sftp.html', {'form':form})

def SFTP_bajar(host,usuario,clave):
    try:
        transferencia = sftp.Connection(host=host, username=usuario, password=clave)

        remotepath= 'myremotepath'
        localpath="mylocalpath"

        print ('\n' + 'Success')

    except Exception, e:
        print str(e)

Upvotes: 1

Views: 654

Answers (1)

e4c5
e4c5

Reputation: 53774

Don't catch any exception in SFTP_bajar

def SFTP_bajar(host,usuario,clave):
    transferencia = sftp.Connection(host=host, username=usuario, password=clave)

    remotepath= 'myremotepath'
    localpath="mylocalpath"

    print ('\n' + 'Success')
    # here do whatever you have to do on your sftp server

Then in your sftp_form view change the way you are catching exception. One thing you must never ever do is to catch broad exceptions like you are doing and ignore it. Catch specific ones.

        from pysftp import AuthenticationException

        try:
            SFTP_bajar(host,usuario,clave)
            messages.success(request,' Success')
            return redirect('auth_login')
        except AuthenticationException:
            messages.error(request, 'Authentication failed')
        except :
            import traceback
            traceback.print_exc()

Upvotes: 1

Related Questions