This topic describes how to create a custom runtime in Python by using a Tornado application.
Prerequisites
You have installed Tornado. For more information, see
Install Tornado.
Procedure
- Write the server.py code. Example:
import tornado.ioloop
import tornado.web
import os
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("GET: Hello world")
def post(self):
self.write("POST: Hello world")
def make_app():
return tornado.web.Application([
r"/. *", MainHandler),
])
if __name__ == "__main__":
app = make_app()
port = os.environ.get("FC_SERVER_PORT", "9000")
app.listen(int(port))
tornado.ioloop.IOLoop.current().start()
- Write a file named bootstrap to start the HTTP server for the preceding code and grant it the permission to run
the code. Example:
Notice The code must contain the #! /bin/bash
comment.
#! /bin/bash
python server.py
- Compress the server.py and bootstrap files into a ZIP package, and then create a function that uses the custom runtime
based on this ZIP package.
Note In this case, the handler
of the function is useless. You can enter any string that meets the character set
constraints of handlers
in Function Compute.
root@33a2b0c2****:/code# ls -ll
total 16
-rwxr-xr-x 1 root staff 17 Aug 16 22:19 bootstrap
-rw-r--r-- 1 root staff 414 Aug 16 17:24 server.py
drwxr-xr-x 38 root staff 1216 Aug 16 22:20 tornado
root@33a2b0c2****:/code# zip -r code.zip *
root@33a2b0c2****:/code# ls -ll code.zip
-rw-r--r-- 1 root staff 943389 Aug 16 22:24 code.zip