Skip to main content

A sorta familiar HTTP framework.

Project description

Responder: a familiar HTTP Service Framework for Python

The Python world certainly doesn't need more web frameworks. But, it does need more creativity, so I thought I'd bring some of my ideas to the table and see what I could come up with.

But will it blend?

import responder

api = responder.API()

@api.route("/{greeting}")
def greet_world(req, resp, *, greeting):
    resp.text = f"{greeting}, world!"

if __name__ == '__main__':
    api.run()

This gets you a WSGI app, with WhiteNoise pre-installed, jinja2 templating (without additional imports), and a production webserver (ready for slowloris attacks), serving up requests with gzip compression automatically.

Class-based views (and setting some headers and stuff):

@api.route("/{greeting}")
class GreetingResource:
    def on_request(req, resp, *, greeting):   # or on_get...
        resp.text = f"{greeting}, world!"
        resp.headers.update({'X-Life': '42'})
        resp.status_code = api.status_codes.HTTP_416

Render a template, with arguments:

@api.route("/{greeting}")
def greet_world(req, resp, *, greeting):
    resp.content = api.template("index.html", greeting=greeting)

The api instance is available as an object during template rendering.

Serve a GraphQL API:

import graphene

class Query(graphene.ObjectType):
    hello = graphene.String(name=graphene.String(default_value="stranger"))

    def resolve_hello(self, info, name):
        return "Hello " + name

api.add_route("/graph", graphene.Schema(query=Query))

We can then send a query to our service:

>>> requests = api.session()
>>> r = requests.get("http://;/graph", params={"query": "{ hello }"})
>>> r.json()
{'data': {'hello': 'Hello stranger'}}

Or, request YAML back:

>>> r = requests.get("http://;/graph", params={"query": "{ hello(name:\"john\") }"}, headers={"Accept": "application/x-yaml"})
>>> print(r.text)
data: {hello: Hello john}

Want HSTS?

api = responder.API(enable_hsts=True)

Boom. ✨🍰✨

The Basic Idea

The primary concept here is to bring the nicities that are brought forth from both Flask and Falcon and unify them into a single framework, along with some new ideas I have. I also wanted to take some of the API primitives that are instilled in the Requests library and put them into a web framework. So, you'll find a lot of parallels here with Requests.

  • Setting resp.text sends back unicode, while setting resp.content sends back bytes.
  • Setting resp.media sends back JSON/YAML (.text/.content override this).
  • Case-insensitive req.headers dict (from Requests directly).
  • resp.status_code, req.method, req.url, and other familar friends.

New Ideas

  • A built in testing client that uses the actual Requests you know and love.
  • The ability to mount other WSGI apps easily.
  • Automatic gzipped-responses (still working on that).
  • In addition to Falcon's on_get, on_post, etc methods, Responder features an on_request method, which gets called on every type of request, much like Requests.
  • WhiteNoise is built-in, for serving static files.
  • Waitress built-in as a production web server. I would have chosen Gunicorn, but it doesn't run on Windows. Plus, Waitress serves well to protect against slowloris attacks, making nginx unnecessary in production.
  • GraphQL support, via Graphene. The goal here is to have any GraphQL query exposable at any route, magically.

Old Ideas

  • Flask-style route expression, with new capabilities -- primarily, the ability to cast a parameter to integers as well as other types that are missing from Flask, all while using Python 3.6+'s new f-string syntax.

  • I love Falcon's "every request and response is passed into to each view and mutated" methodology, especially response.media, and have used it here. In addition to supporting JSON, I have decided to support YAML as well, as Kubernetes is slowly taking over the world, and it uses YAML for all the things. Content-negotiation and all that.

Future Ideas

  • I want to be able to "mount" any WSGI app into a sub-route.
  • Cooke-based sessions are currently an afterthought, as this is an API framework, but websites are APIs too.
  • Potentially support ASGI instead of WSGI. Will the tradeoffs be worth it? This is a question to ask. Procedural code works well for 90% use cases.
  • If frontend websites are supported, provide an official way to run webpack.

The Goal

The primary goal here is to learn, not to get adoption. Though, who knows how these things will pan out.

When can I use it?

When it's ready. It's not. I started work on this a few days ago. It works surprisingly well, considering! :)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

responder-0.0.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

responder-0.0.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file responder-0.0.1.tar.gz.

File metadata

  • Download URL: responder-0.0.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for responder-0.0.1.tar.gz
Algorithm Hash digest
SHA256 33fd4296bb27ced193cb63cbda35cd0ba0bc4aff387a2e196add6e82b7fb6508
MD5 9896066cf8d082cdec651512a729bc86
BLAKE2b-256 f02334717616e5b4f1200951f0c9723c085692081735b82013fe856c679d67a2

See more details on using hashes here.

Provenance

File details

Details for the file responder-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: responder-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for responder-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 360eac4e3c0f3ac6b7c0979e0f735b0c4b3657147efa7ea70b269fa984d83fb0
MD5 9d4bda3f52c2edb172502e676de42b89
BLAKE2b-256 bb3d9cfe1b27de3b45b376b33df83145f68c4156f22430f42eb09481834bdfdc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page