【问题标题】:How to raise this exception or error message?如何引发此异常或错误消息?
【发布时间】:2012-11-23 03:35:49
【问题描述】:

我一直在 Python/Django 中实现 rsync 以在文件之间传输数据。这是我的views.py:

def upload_file(request):
    '''This function produces the form which allows user to input session_name, their remote host name, username 
    and password of the server. User can either save, load or cancel the form. Load will execute couple Linux commands
    that will list the files in their remote host and server.'''

    if request.method == 'POST':    
        # session_name = request.POST['session']
        url = request.POST['hostname']
        username = request.POST['username']
        global password
        password = request.POST['password']
        global source
        source = str(username) + "@" + str(url)

        command = subprocess.Popen(['sshpass', '-p', password, 'rsync', '--list-only', source],
                           stdout=subprocess.PIPE,
                           env={'RSYNC_PASSWORD': password}).communicate()[0]
    command = command.split(' ')[-1]

        result = subprocess.Popen(['ls', '/home/nfs/django/genelaytics/user'], stdout=subprocess.PIPE).communicate()[0].splitlines()

        return render_to_response('thanks.html', {'res':result, 'res1':command}, context_instance=RequestContext(request))

    else:
        pass
    return render_to_response('form.html', {'form': 'form'},  context_instance=RequestContext(request))

我从表单中输入远程主机、用户名和密码。但那些密码、用户名或服务器名可能不正确。即使它们不正确,这段代码也会将我转换为thanks.html,但这些服务器上的文件当然没有列出,因为用户名、密码、主机名不正确。我如何验证它?如何引发异常或错误的用户名、密码或主机名错误?

【问题讨论】:

  • 你可能想阅读return codesPopen
  • 你能说得更具体一点吗?我想显示如下消息:主机名不正确或用户名/密码不正确!!
  • 当然。如果您真的想使用 Popen(而不是库,正如 Marwan Alsabbagh 建议的那样)来执行此操作,您应该检查您进行的每个 Popen 调用的返回代码,并将其与每个程序返回的返回代码列表进行比较(sshpass, rsync)。您可能可以在程序手册上找到该信息,或者,如果失败,可以通过反复试验找到。

标签: python django exception-handling


【解决方案1】:

在 python 中,如果您想使用 ssh 或 sftp(通过 ssh 连接复制文件),那么 paramiko 库是您的最佳选择。如果您只想检查提供的主机、用户名、密码组合是否有效,此功能将完成这项工作:

import paramiko

def test_ssh(host, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=username, password=password)

函数的示例调用是:

test_ssh('10.0.0.10', 'myuser', 'mypassword')

如果它能够正确连接到主机,它将成功返回。否则,它将通过异常详细说明失败的原因。例如,当放置无效主机时,会引发以下异常:

socket.error: [Errno 113] No route to host

用户名无效,会引发密码:

paramiko.AuthenticationException: Authentication failed.

您可以像在 Python 中通常那样捕获这些异常,并向用户显示您希望的任何类型的消息。我建议不要使用 sshpass 和 subprocess 来使用 paramiko。

【讨论】:

    【解决方案2】:

    在你做任何其他事情之前,停止。您正在使用全局变量来存储用户名和密码。这意味着来自其他用户的后续请求将有权访问前一个用户的数据。 不要这样做。如果你在 Python 中使用全局变量,你可能做错了:如果你在 Django 中使用它在请求之间传递数据,那么你肯定做错了。

    请注意I've warned you about this before。请停止实施根本不安全的架构。

    【讨论】:

    • 虽然我完全同意并赞同您回答的精神,但我很好奇 HTTP 请求如何可能从他的代码中的前一个进程中泄露密码值 - 除非有某种安全性Django 本身的缺陷?
    • 它不会透露价值,不。但它很可能允许后续请求使用该值:可能将密码存储在全局中的原因是为了在不同的视图中访问它,并且可以想象攻击者可以直接进入该视图并使用之前存储的用户名/密码访问外部资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2018-05-05
    • 2013-12-07
    相关资源
    最近更新 更多