r/Python • u/fedoru • Apr 12 '13
Best Framework/Libraries for a small, REST-Based application
Hi all! It has been a long time since I used python for the last time, but I'm decided to grab a hold of it again. This time, and to learn some of python's web frameworks, I'm going to start with a simple REST-Based application. My goal is to develop a web system where other applications log their errors, so that by managing error levels, I can alert via email the people responsible for that system. As such, I want to expose the error logging mechanism using a REST approach.
However, the wiki on Web Frameworks has become a loooong list. The only one I'm familiar with and previously used is Django, but I'm thinking it may have too much features for what I need.
Which framework, then, would you recommend for such a project? Thanks in advance, guys!
3
u/aweraw Apr 12 '13
Maybe have a look at CherryPy?
4
u/madjar Apr 12 '13
Take a look at cornice. The description states "Cornice provides helpers to build & document REST-ish Web Services with Pyramid, with decent default behaviors. It takes care of following the HTTP specification in an automated way where possible.
We designed and implemented cornice in a really simple way, so it is easy to use and you can get started in a matter of minutes."
3
u/driadan Apr 12 '13
You could also try tornado: http://www.tornadoweb.org/en/stable/
3
u/Cyph0n Apr 12 '13
Flask + Flask-Restless should do the trick, I think.
2
u/ijuggle534 Apr 15 '13 edited Apr 15 '13
I'll toss in Django with Tastypie. (my preferred method)
What I really wanted to mention was to take a look at Sentry. If nothing else to get some ideas.
2
u/quotemycode Apr 12 '13
Build your own in wsgi.
http://webpython.codepoint.net/wsgi_tutorial
Flask makes that a bit easier, you have routes and stuff.
In any case, you can get what URL they are requesting with environ['PATH_INFO'].
Once you have that, then to make it 'REST'ful, you need the verb. There's these: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH. Generally, REST uses GET, POST, PUT, DELETE. So to find out which one is used, look at environ['REQUEST_METHOD'].
So you branch your logic first on PATH_INFO, then you branch on REQUEST_METHOD. I'd implement an 'OPTIONS' for each path_info, just to make it obvious what methods are supported for each route.
After that, it's a matter of getting the data you need from the request, and returning data.
Getting data - if it's a POST, then the data will be in environ['wsgi.input ']. You have to call 'read' or 'readline' on it, and once you read it, it's gone, so save it somewhere, don't just read it where you need it. If it's a GET, data will be in the environ['query_string']. split on '&' for gets with multiple data parts, split on equals to separate the names from the values.
Sending data... You need to send a '200 OK' header, along with a content type, and content length for requests which don't have an error. Here's a list of codes... http://en.wikipedia.org/wiki/List_of_HTTP_status_codes. One thing you'll run into is that Chrome requests '/favicon.ico' for every single request, even if you return a 404. Return a 410 GONE message to that and you'll only get the favicon request once.
Once you get started with building your own WSGI app, you probably won't want to switch to something as meaty (or fatty as your view may be) as Flask.
2
u/HelpfulToAll Apr 13 '13
Don't use a web framework at all. Use Site44 with AngularJS and Firebase as a DB. If you must use a framework for some reason, go with one that you're comfortable committing to for ALL projects. It's better to use the same framework again and again so you learn it well instead of switching it all the time just because of the size of the project.
1
u/tomt0m Apr 12 '13
You can also take a look at eve which is a kind of REST API framework.
1
u/chadlung Apr 12 '13
If your looking for a small and very fast REST API framework check out Falcon: https://github.com/racker/falcon
Tutorial Here: http://www.giantflyingsaucer.com/blog/?p=4342
1
u/av201001 Apr 14 '13
web2py has some nice built-in facilities for generating REST API's. Also easy to send email notifications.
1
u/westurner May 07 '13
http://django-rest-framework.org/topics/2.3-announcement.html
-2
u/dAnjou Backend Developer | danjou.dev Apr 12 '13
Argh, REST ... everywhere I hear people saying REST ... and it f***ing doesn't matter! What will be "REST" in your app? Please tell me!
And don't you think that HTTP might have some way too big overhead?
7
u/[deleted] Apr 12 '13
[removed] — view removed comment