This builder takes in a Python program that defines a singular HTTP handler and outputs it as a Lambda.
When to Use It
Whenever you want to expose an API or a function written in Python.
How to Use It
Define a index.py
file inside a folder as follows:
from http.server import BaseHTTPRequestHandler from cowpy import cow class handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type','text/plain') self.end_headers() message = cow.Cowacter().milk('Hello from Python on Now Lambda!1111111') self.wfile.write(message.encode()) return
Inside requirements.txt
define:
cowpy==1.0.3
And define a now.json
like so:
{ "version": 2, "builds": [{ "src": "*.py", "use": "@now/python" }] }
The example above can be seen live at https://python-deployment-mhssg6mqx.now.sh/
Also, the source code of the deployment can be checked by appending /_src
e.g. https://python-deployment-mhssg6mqx.now.sh/_src.
Web Server Gateway Interface
The Web Server Gateway Interface (WSGI) is a calling convention for web servers to forward requests to web applications written in Python. You can use WSGI with frameworks such as Flask or Django.
Instead of defining a handler
, define an app
variable in your Python file.
For example, define a index.py
file inside a folder as follows:
from flask import Flask, Response app = Flask(__name__) @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): return Response("<h1>Flask on Now</h1><p>You visited: /%s</p>" % (path), mimetype="text/html")
Inside requirements.txt
define:
flask==1.0.2
And define a now.json
like so:
{ "version": 2, "builds": [{ "src": "index.py", "use": "@now/python" }], "routes": [{ "src": "(.*)", "dest": "index.py" }] }
Most frameworks use their own implementation of routing. However, you can use a catch-all route to circumvent the framework and instead use the Now Routing Layer to match a route to a serverless function.
The example above can be seen live at https://simple-python-flask.now-examples.now.sh/Hello
Asynchronous Server Gateway Interface
The Asynchronous Server Gateway Interface (ASGI) is a calling convention for web servers to forward requests to asynchronous web applications written in Python. You can use ASGI with frameworks such as Sanic.
Instead of defining a handler
, define an app
variable in your Python file.
For example, define a index.py
file inside a folder as follows:
from sanic import Sanic from sanic.response import json app = Sanic() @app.route('/') @app.route('/<path:path>') async def index(request, path=""): return json({'hello': path})
Inside requirements.txt
define:
sanic==19.6.0
And define a now.json
like so:
{ "version": 2, "builds": [{ "src": "index.py", "use": "@now/python" }], "routes": [{ "src": "(.*)", "dest": "index.py" }] }
Most frameworks use their own implementation of routing. However, you can use a catch-all route to circumvent the framework and instead use the Now Routing Layer to match a route to a serverless function.
The example above can be seen live at https://simple-python-sanic-rf2yqvtg9.now.sh/world
Technical Details
Entrypoint
The entrypoint file must be a .py
source file with one of the following variables defined:
handler
that inherits from theBaseHTTPRequestHandler
classapp
that exposes a WSGI or ASGI Application
Version
Python 3.6 is used.
Dependencies
This builder supports installing dependencies defined in the requirements.txt
file or a Pipfile.lock
file.