I am really stuck with serving dynamically created content in Flask.
If I understand everything right, the only way to do it in Python code is to use Flask native send_file(). Is there a way to perform send_file not through Flask itself? It's extremely slow, I can't afford it :(
I know how to serve statics via nginx, but it seems to be not suitable in my case, cause it links a web address with the real path on my server. When I perform send_file(), the file does not have any web address (am I right?).
So, what should I do?
Go read about X-Accel-Redirect response header and how that can be used in conjunction with a nginx front end to have nginx serve up a file which has been written to the file system by a backend web application.
http://wiki.nginx.org/X-accel
Related
Let's say I have a domain. Under home directory of the domain I have a text(.txt) file called note.txt. Like below
https://www.example.com/note.txt
When I access the url, browser display text string contained inside the file. But when I run a Flask under that domain instead of a traditional html,css,javascript,php app, server return 404error even though the file exists in fact at the same location. I can see this from the ftp client.
So why does the server returns 404 error when the site hosts a python app instead of the more traditional html,css,javascript,php app?
What you are missing here is that Flask has its own URL routing.
Python code must be exposed through app.route decorator
Static files, like your note.txt can be served through Flask, but they are often handled by the front end web server (Nginx, Apache) through their configuration
The answer for "why does the server returns 404 error" is that URL routing should be explicit (nothing happens unless you tell it to happen) instead of implicit (everything on the server is exposed by default). Because PHP chose the latter approach, WordPress, Drupal, et. al. traditional PHP sites are getting hacked very easily when they are given to people who don't have the full picture what they are doing. It might be convenient in the beginning, but it is also an open invitation for script kiddies to raid your server.
Im trying to figure out how to serve static pages from heroku with a flask app. I found this with some searching:
#app.route('/foo/<path:filename>')
def send_foo(filename):
return send_from_directory('/path/to/static/files', filename)
But, this would be quite inefficient. Is there a way to have the front facing server directly serve these files?
Normally you would do this with mod_rewrite with apache or similar, but afaik heroku doesn't let's you change the http server configuration.
You'll need to use the rack middleware, it lets you write url rewrite rules with ruby. (check this out : http://icelab.com.au/articles/useful-heroku-friendly-rewrites-with-rack-rewrite/ )
This might be a bit of a noob question, so I apologize in advance.
How do I make a web server running flask+redis serve binary files as a response to a link/query?
I want the response to the link to be either some AJAX action such as changing a div, or popping up an "unavailable" response, or to serve back some binary file.
I would like help both with the client side (jQuery / other Javascript) and the server side.
Thanks!
Side question: Would you choose redis for this task? Or maybe something else such as MongoDB, or a regular RDBMS? And why?
Normally you would configure your webserver so that URLs that refer to static files are handled directly by the server, rather than going through Flask.
I use django to run my website and nginx for front webserver ,
but when i upload a very large file to my site,
it take me very long time ,
there is some thing wrong when nginx hand upload large file;
the nginx will send the file to django after receive all my post file;
so this will take me more time;
i want to find some other webserver to replace the nginx;
wish your suggest?
You problem not in nginx you problem in nginx settings.
If you want handle files with django - you should change some params
Timeout when uploading a large file?
Else nginx may handle files itself
http://www.grid.net.ru/nginx/upload.en.html
Nginx is probably the best http server, there is no need to replace it. I will advise you to upload very large files via ftp or nfs share.
If you want to not pass file to your django application, then you should use:
fastcgi_pass_request_body off;
Also you may want to use the upload module: http://www.grid.net.ru/nginx/upload.en.html
Look at tornado at http://www.tornadoweb.org/ You may use it beside the django and handle file upload.
On my project I successfully use django with tornado, that handles API calls and long ajax requests.
I am currently working on a project to create simple file uploader site that will update the user of the progress of an upload.
I've been attempting this in pure python (with CGI) on the server side but to get the progress of the file I obviously need send requests to the server continually. I was looking to use AJAX to do this but I was wondering how hard it would be to, instead of changing to some other framerwork (web.py for instance), just write my own web server for receiving the XML HTTP Requests?
My main problem is that sending the request is done from HTML and Javascript so it all seems like magic trickery at the moment.
Can anyone advise me as to the best way to go about receiving these requests on the server?
EDIT: It seems that a framework would be the way to go. Would web.py be a good route to take?
I would recommend to use a microframework like Sinatra for Ruby. There seem to be some equivalents for Python. What python equivalent of Sinatra would you recommend?
Such a framework allows you to simply map a single method to a route.
Writing a very basic HTTP server won't be very hard (see http://docs.python.org/library/simplehttpserver.html for an example), but you will be missing many features that are provided by real servers and web frameworks.
For your project, I suggest you pick one of the many Python web frameworks and run your application behind Apache/mod_wsgi.
There's absolutely no need to write your own web server. Plenty of options exist, including lightweight ones like nginx.
You should use one of those, and either your own custom WSGI code to receive the request, or (better) one of the microframeworks like Flask or Bottle.