gpt4 book ai didi

python - 无法在 Django 中使用 Popen 传递服务器密码?

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:36 25 4
gpt4 key购买 nike

我无法在 Django 中使用 subprocess.Popen 传递服务器密码。这是我的完整views.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>

我做错了什么?密码不是从environment_variable传递的。

最佳答案

您的代码(就 RSYNC_PASSWORD 中设置的密码而言)是正确的。

我的结论是,您正在尝试使用 Rsync 中的不同模块(如 ssh)连接到服务器。对于 ssh,RSYNC_PASSWORD 变量不起作用

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

有关通过 ssh 进行无密码 rsync 的更多信息,请参阅此问题:How to automate rsync without asking for password prompt

关于python - 无法在 Django 中使用 Popen 传递服务器密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13447354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com