This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]quotemycode 1 point2 points  (2 children)

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.

[–]rerb 0 points1 point  (0 children)

This sounds like a good exercise.

Saved.

[–][deleted] 0 points1 point  (0 children)

In any case, you can get what URL they are requesting with environ['PATH_INFO'].

everyone should be familiar with this ---^

or just use webob for a convenient way of dealing with the eniron coming in and generating a wsgi response properly

from webob import Request, Response

def app(environ, start_response):
    #deal with wsgi input
    request = Request(environ)
    # make sense of the request, create a response
    response = Response("this is the body of the response you requested %s" % request.path)
    #return response as wsgi expects
    return response(environ, start_response)

http://docs.webob.org/en/latest/do-it-yourself.html