1

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 から渡されません。

4

1 に答える 1

1

あなたのコード (でパスワードを設定する限りRSYNC_PASSWORD) は正しいです。

私の結論は、ssh のような Rsync の別のモジュールを使用してサーバーに接続しようとしているということです。ssh の場合、RSYNC_PASSWORD変数は機能しませ

コマンドの実行にfabricを使用するのは良い考えでしょうか? パスワードの問題は解決しませんが、いくつかのことは処理できます。ssh 経由で接続している場合は、秘密鍵認証を設定することをお勧めします。

ssh を介したパスワードなしの rsync の詳細については、この質問を参照してください:パスワード プロンプトを求めずに rsync を自動化する方法

于 2012-11-19T13:14:51.153 に答える