Python 无法通过Django中的Popen传递服务器密码?

Python 无法通过Django中的Popen传递服务器密码?,python,django,subprocess,Python,Django,Subprocess,我无法通过Django中的subprocess.Popen传递服务器密码。以下是我的完整视图.py: from django.shortcuts import render_to_response from django.template import RequestContext import subprocess def upload_file(request): '''This function produces the form which allows user to inp

我无法通过Django中的subprocess.Popen传递服务器密码。以下是我的完整视图.py:

from django.shortcuts import render_to_response
from django.template import RequestContext
import subprocess

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(['rsync', '--list-only', source],
                           stdout=subprocess.PIPE, 
                           env={'RSYNC_PASSWORD': password}).communicate()[0]

        result1 = subprocess.Popen(['ls', '/home/zurelsoft/R'], stdout=subprocess.PIPE).communicate()[0]
        result = ''.join(result1)
        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))  
以下是我从中获取用户输入的表单:

<fieldset>
            <legend>Session</legend>
                <label for="input-one" class="float"><strong>Session Name:</strong></label><br />
                <input class="inp-text" name="session" id="sess" type="text" size="30" /><br />

                <label for="input-two" class="float"><strong>RemoteHost:</strong></label><br />
                <input class="inp-text" name="hostname"  id="host" type="text" size="30" />

                <label for="input-three" class="float"><strong>Username:</strong></label><br />
                <input class="inp-text" name="username"  id="user" type="text" size="30" />

                <label for="input-four" class="float"><strong>Password:</strong></label><br />
                <input class="inp-text" name="password"  id="pass" type="password" size="30" />
        </fieldset>

一场
会话名称:

远程主机:
用户名:
密码:
我做错了什么?未从环境变量传递密码

您的代码(只要在
RSYNC\u password
中设置密码)是正确的

我的结论是,您正在尝试使用Rsync中的不同模块(如ssh)连接到服务器。在ssh的情况下,
RSYNC\u PASSWORD
变量不起作用

也许使用它来执行命令是个好主意?它可以为您处理一些事情,虽然它不会解决您的密码问题。如果您是通过ssh连接的,我建议您设置私钥身份验证

有关通过ssh的无密码rsync的更多信息,请参阅此问题:

您的代码(只要在
rsync\u password
中设置密码)是正确的

我的结论是,您正在尝试使用Rsync中的不同模块(如ssh)连接到服务器。在ssh的情况下,
RSYNC\u PASSWORD
变量不起作用

也许使用它来执行命令是个好主意?它可以为您处理一些事情,虽然它不会解决您的密码问题。如果您是通过ssh连接的,我建议您设置私钥身份验证


有关通过ssh实现的无密码rsync的更多信息,请参见此问题:

能否显示更多视图代码?你遗漏了一些重要的要点。请检查这个问题。
全局密码
部分是一个非常糟糕的主意。使用globals通常是一个坏主意,但是使用密码似乎很危险。您能显示更多的视图代码吗?你遗漏了一些重要的要点。请检查这个问题。
全局密码
部分是一个非常糟糕的主意。使用globals通常是一个坏主意,但使用密码可能相当危险。谢谢您的回答。如您所见,我没有使用ssh连接到服务器。Rsync将我连接到服务器并传输文件。我该如何解决我的问题?@sachitad:远程服务器上有rsync服务器吗?如果不是,那么很可能您确实在使用ssh。默认情况下,rsync在连接到远程系统时使用ssh。解决我的问题的解决方案是什么?是的,rsync在本地和远程计算机上都可用。@sachitad:有两种可能的解决方案。1.使用公钥/私钥身份验证,因此不需要密码。2.设置rsyncd服务器,这样您就不需要密码,或者可以像这样传递密码。我建议设置公钥/私钥()谢谢您的回答。如您所见,我没有使用ssh连接到服务器。Rsync将我连接到服务器并传输文件。我该如何解决我的问题?@sachitad:远程服务器上有rsync服务器吗?如果不是,那么很可能您确实在使用ssh。默认情况下,rsync在连接到远程系统时使用ssh。解决我的问题的解决方案是什么?是的,rsync在本地和远程计算机上都可用。@sachitad:有两种可能的解决方案。1.使用公钥/私钥身份验证,因此不需要密码。2.设置rsyncd服务器,这样您就不需要密码,或者可以像这样传递密码。我建议设置公钥/私钥()