0

I am a novice at web development, and I'm trying to create a simple mod_wsgi app that calls an R script that will write some text to a .txt file, then mod_wsgi reads that file and displays it on the webpage. The python and R code work when I execute manually at the command line, but when I try to integrate it with my web app it doesn't execute the R script at all so no .txt file is generated for the web app to read. That is causing a 500 error.

I'm passing some command-line arguments to the R script with os.system(), but like I said before, the R script is not getting executed:

a=2
b=3    
commandString = "/home/usr/bin/R --no-save --quiet --slave --args " + str(a) + " " + str(b) + " <webcalc.R"
os.system(commandString)
f=open('/home/usr/webapps/simple/htdocs/prob.txt','r')  #This is the file that the R script was supposed to write.
prob=f.read(2)
prob=str(prob)+'%'
f.close()

I'm using an Apache server. Here is the exact error I get from my error log:

[Mon Jul 23 17:45:42 2012] [error] [client 127.0.0.1] mod_wsgi (pid=10367): Exception occurred processing WSGI script '/home/usr/webapps/simple/htdocs/index.py'.
[Mon Jul 23 17:45:42 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Mon Jul 23 17:45:42 2012] [error] [client 127.0.0.1]   File "/home/usr/webapps/simple/htdocs/index.py", line 88, in application
[Mon Jul 23 17:45:42 2012] [error] [client 127.0.0.1]     f=open('/home/usr/webapps/simple/htdocs/prob.txt','r')
[Mon Jul 23 17:45:42 2012] [error] [client 127.0.0.1] IOError: [Errno 2] No such file or directory: '/home/usr/webapps/simple/htdocs/prob.txt'
[Mon Jul 23 17:45:42 2012] [error] [client 127.0.0.1] File does not exist: /home/usr/webapps/simple/htdocs/favicon.ico

I'm sure this is a very simple problem but I can't find a solution anywhere. I really appreciate any help you can give me.

3
  • It says: No such file or directory: '/home/usr/webapps/simple/htdocs/prob.txt', so are you sure your R script is creating that file? Is it there when you look in the home/usr/webapps/simple/htdocs directory?
    – Jon Lin
    Jul 23, 2012 at 17:55
  • Don't use os.system. Use the subprocess module Jul 23, 2012 at 18:05
  • The R script definitely is not creating the file, but I'm almost sure it's because the R script is not getting run in the first place. When I manually place a "prob.txt" file of my creation in the directory, the mod_wsgi script works perfectly and displays whatever I put in the prob.txt file. However the issue is I want the R script to create that .txt file.
    – vokey588
    Jul 23, 2012 at 18:08

1 Answer 1

0

IMPORTANT: THIS IS NOT PROTECTED AGAINST MALICIOUS INPUT

import subprocess
a=2
b=3
commandString = "/home/usr/bin/R --no-save --quiet --slave --args " + str(a) + " " + str(b) + " < /path/to/webcalc.R"
# This is BAD PRACTICE, but necessary in this instance,
# if my assumptions of R are correct.
retval = subprocess.call(commandString, shell=True)

Unless your R interpreter (or whatever it is) can accept a filename directly instead of using input redirection, you must pass a string to subprocess.call() and use shell=True, which is a Bad Thing (tm).

You should also specify the input script by absolute path, not relative, as I've done in the example.

4
  • Your fixes worked! Also, I don't understand what you meant by input redirection. Could you explain what you mean by "...accept a filename directly instead of ...input redirection" and what the ideal alternative would be? Thanks so much for your help, Colin.
    – vokey588
    Jul 23, 2012 at 18:26
  • @user1385991 looking at the R manpage, it looks like you have to use < for input files and > for output files. Check out this page on bash I/O redirection. Ideally you would just give the R interpreter the script's filename (like you do with python) instead of the redirection. With redirection, you're telling the shell to do something like this: cat myRscript.r | /usr/bin/R --somearg. Jul 23, 2012 at 18:31
  • is it possible to give the R script command-line arguments without using input redirection?
    – vokey588
    Jul 23, 2012 at 18:44
  • @user1385991 I have no idea, I've never used R. Jul 23, 2012 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.