Serving files through flask + redis - python

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.

Related

How to create an app (such as .apk or .exe) that communicates to web server

This is my first post. I tried to google and find my own solution, but I don’t even know where to start with this. I want to make a phone app (with Unity(C#) ideally) and eventually a website, while also hosting a web app (python) that the phone app can send basic info (ideally a json or dict) that the web app can do some processing and database referencing and just send back a short answer or two (again ideally a json or dict). I looked into bottlepy and just communicating through http but this seems clunky and I’m sure is not the best way. I don't need (although would appreciate) a full explanation, just the term/protocol/technology I am looking for. Thanks in advance for any help!
You should look at the UnityWebRequest : https://docs.unity3d.com/Manual/UnityWebRequest.html
And maybe you should try to use a REST API on your server to communicate with a UnityWebRequest : https://codetolight.wordpress.com/2017/01/18/unity-rest-api-interaction-with-unitywebrequest-and-besthttp/

Serve dynamic media in Flask with Nginx+uWSGI

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

An "image-serving web framework" in Python?

I'm planning an iOS app that requires a server backend capable of efficiently serving image files and performing some dynamic operations based on the requests it gets (like reading and writing into a data store, such as Redis). I'm most comfortable with, and would thus prefer to write the backend in Python.
I've looked at a lot of Python web framework/server options, Flask, Bottle, static and Tornado among them. The common thread seems to be that either they support serving static files as a development-time convenience only, discouraging it in production, or are efficient static file servers but not really geared towards the dynamic framework-like side of things. This is not to say they couldn't function as the backend, but at a quick glance they all seem a bit awkward at it.
In short, I need a web framework that specializes in serving JPEGs instead of generating HTML. I'm pretty certain no such thing exists, but right now I'm hoping that someone could suggest a solution that works without bending the used Python applications in ways they are not meant for.
Specifications and practical requirements
The images I'd be serving to the clients live in the file system in a shallow directory hierarchy. The actual file names would be invisible to the clients. The server would essentially read the directory hierarchy at startup, assigning a numeric ID for each file, and would route the requests to controller methods that then actually serve the image files. Here are a few examples of ways the client would want to access the images in different circumstances:
Randomly (example URL path: /image/random)
Randomly, each file only once (/image/random_unique), produces some suitable non-200 HTTP status code when the files are exhausted
Sequentially in either direction (/image/0, /image/1, /image/2 etc.)
and so on. In addition, there would be URL endpoints for things like ratings, image info and other metadata, some client-specific information as well (the client would "register" with the server, so that needs some logic, too). This data would live in a Redis datastore, most likely.
All in all, the backend needs to be good at serving image/jpeg and application/json (which it would also generate). The scalability and concurrency requirements are modest, at least to start with (this is not an App Store app, going for ad-hoc or enterprise distribution).
I don't want the app to rely on redirects. That is, I don't want a model where a request to a URL would return a redirect to another URL that is backed by, say, nginx as a separate static file server, leaving only the image selection logic for the Python backend. Instead, a request to a URL from the client should always return image/jpeg, with metadata in custom HTTP headers where necessary. I specify this because it is a way of avoiding serving static files from Python that I thought of, and someone else might think of too ;-)
Given this information, what sort of solution would you consider a good choice, and why? Or is this something for which I need to code non-trivial extensions to existing projects?
EDIT: I've been thinking about this a bit more. I don't want redirects due to the delay inherent in the multiple requests they entail, plus I'd like to abstract out the file names from the client, but I was wondering if something like this would be possible:
It's pretty self-explanatory, but the idea is that the Python program is given the request info by nginx (or whatever serves the role), mulls it over and then tells nginx to respond to the client's request with a specific file from the file system. It does so. The client is none the wiser about how the request was fulfilled, it just receives a response with the correct content type.
This would be pretty optimal in my view, but is it possible? If not with nginx, perhaps something else?
I've been using Django for well over a year now, and it is the hammer I use for all my nails. You could probably do this with a bit of database-image storage and django's builtin orm and url routing (with regex). If you store the images in the database, you will automatically get the unique-id's set. According to this stackoverflow answer, you can use redis with django.
I don't want a model where a request to a URL would return a redirect to another URL that is backed by, say, nginx as a separate static file server, leaving only the image selection logic for the Python backend.
I think Nginx for serving static and python for figuring out the image url is the better solution.
Still if you do not want to do that I would suggest you use any Python web framework (like Django) and write your models and convert them into REST resources (Eg. Using django-tastypie) and/or return a base64 encoded image which you can then decode in your iOS client.
Refs:
Decoding a Base64 image
TastyPie returns the path as default, you might have to do extra work to either store the image blob in the table or write more code to return a base64 encoded image string
You might want to look at one of the async servers like Tornado or Twisted.

Is there any way to upload a very large file use django on nginx?

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.

Creating a python web server to recieve XML HTTP 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.

Categories

Resources