简体   繁体   中英

Unable to write to file in Python when using Flask in mod_wsgi

I'm running a Flask app using Python 2.7.6 on mod_wsgi 3.4. OS is Ubuntu 14.04. I'm unable to write to a file. Below is the code that I'm using:

if __name__ == "__main__":
    f = open('/var/www/jcapp/foobar', 'r+')
    f.write('hello world')
    f.close()
    jcapp.run()

Apache site config file:

<VirtualHost *:80>
ServerName mysite.co
 WSGIDaemonProcess jcapp
 WSGIScriptAlias / /var/www/jcapp/jcapp.wsgi
 <Directory /var/www/jcapp/>
        WSGIProcessGroup jcapp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
 </Directory>
WSGIScriptAlias /notify /var/www/jcapp/jcapp.wsgi
<Directory /var/www/jcapp/>
    WSGIProcessGroup jcapp
    WSGIApplicationGroup %{GLOBAL}
    Order allow,deny
    Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined

Can someone please tell me where I might be going wrong?

You only write to the file if __name__ == '__main__' , which it does not when you run with mod_wsgi instead of python app.py .

Move the code outside of the __name__ guard.

Or use a separate entry point if you need to do some setup before running your app. Point mod_wsgi at this entry point instead.

wsgi_app.py :

from myapp import jcapp as application

with open('/var/www/jcapp/foobar', 'w') as f:
    f.write('hello world')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM