好的,我正在Django建立一个网站,我需要使用一个用Ruby编写的API。我已经下载了API并编写了一个ruby脚本。现在我正在使用子进程POpen方法从Django调用脚本。这是我在Django视图中的内容:
def call(request):
context = RequestContext(request)
if request.method == 'POST':
form = sendCall(request.POST)
if form.is_valid():
form.save(commit=True)
p=subprocess.Popen("cd verboice-api-ruby/lib;ruby \"verbyTest.rb\"",shell=False, stdout=subprocess.PIPE)
output, errors = p.communicate()
return mainIndex(request)
else:
print form.errors
else:
form = sendCall()
return render_to_response('main/call.html', {'form': form}, context)
我运行服务器并激活呼叫视图。 ruby脚本将发送一个调用,然后返回主页面。但是当它调用实际的ruby脚本时,它给了我一个错误。它给了我这个错误:
OSError at /main/call/
[Errno 2] No such file or directory
Request Method: POST
Request URL: http://127.0.0.1:8000/main/call/
Django Version: 1.5.4
Exception Type: OSError
Exception Value:
[Errno 2] No such file or directory
Exception Location: /anaconda/lib/python2.7/subprocess.py in _execute_child, line 1308
Python Executable: /anaconda/bin/python
Python Version: 2.7.5
Python Path:
['/Users/manavdutta/Downloads/savinglives',
'/anaconda/lib/python27.zip',
'/anaconda/lib/python2.7',
'/anaconda/lib/python2.7/plat-darwin',
'/anaconda/lib/python2.7/plat-mac',
'/anaconda/lib/python2.7/plat-mac/lib-scriptpackages',
'/anaconda/lib/python2.7/lib-tk',
'/anaconda/lib/python2.7/lib-old',
'/anaconda/lib/python2.7/lib-dynload',
'/anaconda/lib/python2.7/site-packages',
'/anaconda/lib/python2.7/site-packages/PIL',
'/anaconda/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time: Sun, 5 Jan 2014 16:46:38 -0600
我该怎么做才能阻止此错误?
答案 0 :(得分:2)
我认为您错误地使用Popen
:
p = subprocess.Popen(['ruby', 'verbyTest.rb'], cwd='verboice-api-ruby/lib', shell=False, stdout=subprocess.PIPE)