我有这样的django项目:
view.py
def runKeyGenProcess(request):
message = "User not autentificated!Please either signup or login"
req_user = request.user
openKeyGen = ['openvpn', '--genkey', '--secret', 'key_' + str(req_user)]
openKeyTest = ['openvpn', '--test-crypto', '--secret', 'key_' + str(req_user)]
if request.method == 'POST' and request.user.is_authenticated():
os.chdir('/etc/openvpn')
try:
subprocess.Popen(openKeyGen)
subprocess.Popen(openKeyTest)
except IndexError:
print "Server error"
return HttpResponse(message)
scrit.py
$(document).ready(function(){
$('.progress-button').progressInitialize();
$('#submitButton').click(function(e){
e.preventDefault();
$(this).progressTimed(3);
$.ajax({
type: "POST",
url: "/run-key-gen-process/"
}).done(function(response) {
$( document ).ajaxStop(function() {
setTimeout(function(){
$("#jmessage").addClass("alert alert-success")
.text( "Key was successfully generated and tested!" );
}, 4000);
});
});
});
});
我的代码表现得很奇怪。如果我将它作为python manage.py runserver domainname.com:80运行,代码运行良好。
否则,如果我停止本地主机(ctrl + C)然后启动apache(service apache2 start)就会出错:
回溯: get_response中的文件“/var/www/lib/python2.7/site-packages/django/core/handlers/base.py” 132. response = wrapped_callback(request,* callback_args,** callback_kwargs) runKeyGenProcess中的文件“/var/www/pulsarvoip/src/pulsarvpn/views/button_handler.py” 38. subprocess.Popen(openKeyGen) 在 init 中输入文件“/usr/lib/python2.7/subprocess.py” 679. errread,errwrite) 在_execute_child中输入文件“/usr/lib/python2.7/subprocess.py” 1259.提出child_exception 异常类型:/ run-key-gen-process /的OSError 例外值:[Errno 2]没有这样的文件或目录
我在Firefox和Chrome以及隐身窗口中尝试了很多次。 可能是什么?
更新:改变pach像这样绝对:
.....
req_user = request.user
openKeyGen = ('cd /etc/openvpn && openvpn --genkey --secret key_' + str(req_user))
openKeyTest = ('cd /etc/openvpn && openvpn --test-crypto --secret key_' + str(req_user))
if request.method == 'POST':
try:
subprocess.Popen(openKeyGen)
subprocess.Popen(openKeyTest)
....
但现在两种变体都有相同的错误(如上所示)。
UPDATED2: 添加
subprocess.Popen(openKeyGen, sheel = True )
此代码最后在本地和apache上工作,但只有在本地运行时才在文件夹中生成密钥!我想知道,为什么!?