[英]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.我正在 mod_wsgi 3.4 上使用 Python 2.7.6 运行 Flask 应用程序。 OS is Ubuntu 14.04.操作系统是 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: Apache站点配置文件:
<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
.您只在__name__ == '__main__'
写入文件,当您使用 mod_wsgi 而不是python app.py
运行时不会写入文件。
Move the code outside of the __name__
guard.将代码__name__
保护之外。
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.将 mod_wsgi 指向此入口点。
wsgi_app.py
: wsgi_app.py
:
from myapp import jcapp as application
with open('/var/www/jcapp/foobar', 'w') as f:
f.write('hello world')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.