Flask Apache on AWS EC2 - Read/Write Failing

2024/11/15 10:41:12

So I've been stumped by this problem for a day now. I'm relatively new to AWS EC2 so have been experimenting with Python Flask apps on it.

I have an Ubuntu instance, and can get a flask app to run fine on it using Apache2 and WSGI. Only problem is whenever I put a line into my app that requests either a read or a write on the file system the server errors, but the error logs don't produce an error.

Here's the function with some debug prints that errors the site:

def cleanup_temps():basePath = os.path.dirname(os.path.realpath('__file__')) + '/static/images/temp/'deleteDelay = 20print '1'for f in os.listdir(basePath):print '2'if os.path.getctime(basePath + f) < (time.time() - deleteDelay):print '3'os.remove(basePath + f)

When I load the page that calls that function it just gives me an Internal Server Error, and checking /var/log/apache2/error.log the only line that appears is the debug print '1'. What is strange is that if I run that server locally it works fine. And even if I run that function manually on the EC2 instance via a python shell it still works fine. So I thought it might have something to do with the Apache or WSGI permissions?

I've changed the whole flask site to chmod -R 777 and changed the user:group from root:root to ubuntu:ubuntu, yet non of that seems to have made a difference?

I'm at a bit of a blank now to be honest so not sure what else I can try?

For good measure here's my /etc/apache2/sites-available/ImgResizeApi.conf file:

<VirtualHost *:80>WSGIDaemonProcess ImgResizeApiWSGIScriptAlias / /var/www/ImgResizeApi/ImgResizeApi.wsgi<Directory /var/www/ImgResizeApi>WSGIProcessGroup ImgResizeApiWSGIApplicationGroup %{GLOBAL}Order allow,denyAllow from all</Directory>
</VirtualHost>

EDIT: I am pretty sure it's now something to do with permissions as I've made an even simpler test, adding the function below, which as soon as the 3 file IO lines are added errors the server!

@ImgResizeApi.route('/test')
def test():f = open('test.txt', 'w')f.write('Hi there')f.close()return render_template('index.html')
Answer

On thing that eats my eyes is that '__ file __' is in quotes. It is a special variable, not a string. It should not be quoted.

You may want to print you basePath variable first to see the actual path you get back.

Additionally, for cross platform compatibility, you may want to use os.path.sep instead of '/' as well as for the path concatenation use os.path.join instead of simple string concat.

https://en.xdnf.cn/q/119064.html

Related Q&A

AttributeError: unicode object has no attribute pop

I have this piece of python code in gae.def post(self):cases=self.request.get(cases)while cases:logging.info("cases: %s " % cases)case=cases.pop()Which produces this log.INFO 2012-09-19 2…

How to set platform when using pip download command

I want to download some pacakges(tensorflow, keras, imbalanced, xgboost, lightgbm, catboost) for centos 7.4 and python 3.7 on mac.How should i set platform name and ant other settings?I used below com…

How to split qr codes in separate images?

I have hundreds of pictures with qr codes (sometimes there are 0, 1, 2 or more qr codes on one page...but they are always in one line). I want to decode the qr codes from left to right. My idea is to s…

Python methods from csv

I am working on an assignment where I create "instances" of cities using rows in a .csv, then use these instances in methods to calculate distance and population change. Creating the instance…

Convert string numpy.ndarray to float numpy.ndarray

I have one problem. How can I convert:import numpy as npa = np.array([[0.1 0.2 0.3], [0.3 0.4 0.5], [0.5 0.6 0.7]])To:b = np.array([[0.1,0.2,0.3], [0.3,0.4,0.5], [0.5,0.6,0.7]])

function at the end of code- why do i have to place it?

Im just starting my adventure with Python. Unfortunately, I cant figure out why, at the end of my code, I have to add myfunc(). Without it my code doesnt display. How is it if I use more than one def…

Would like to write code in idle that I have in Jupyter notebook. It is using Timeit

code big_array= np.random.rand(1000000) %timeit sum(big_array) this code above is done in jupyter notebook how do I use this code in idle

How to pass python variable to shell command [duplicate]

This question already has answers here:How to escape os.system() calls?(10 answers)Closed 2 years ago.Is their any way that i can pass variable value of python to my unix command Note : var is a pytho…

A function inside a function Python

This is a problem asked before but I cant really understand the other explain of this kind of problem so Im here to re-write it in more details. While studying I have encountered this kind of code tha…

Accessing a parameter passed to one function in another function

I have two functions:def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something elsep1 and p2 are the same data, so I want to avoid asking …