|
| 1 | +# Introduction |
| 2 | + |
| 3 | +Litefs is a lite python web framework. |
| 4 | + |
| 5 | +Build a web server framework using Python. Litefs was developed to implement |
| 6 | +a server framework that can quickly, securely, and flexibly build Web |
| 7 | +projects. Litefs is a high-performance HTTP server. Litefs has the |
| 8 | +characteristics of high stability, rich functions, and low system |
| 9 | +consumption. |
| 10 | + |
| 11 | +# Installation |
| 12 | + |
| 13 | +It can be installed via pip: |
| 14 | + |
| 15 | + $ pip install litefs |
| 16 | + |
| 17 | +It can be installed via source code: |
| 18 | + |
| 19 | + $ git clone https://github.com/leafcoder/litefs.git litefs |
| 20 | + $ cd litefs |
| 21 | + $ python setup.py install |
| 22 | + |
| 23 | +# Quickstart: "Hello world" |
| 24 | + |
| 25 | +Firstly, let's write a basci example via litefs. Save it to "example.py". |
| 26 | + |
| 27 | + # /usr/bin/env python |
| 28 | + |
| 29 | + import litefs |
| 30 | + litefs.test_server() |
| 31 | + |
| 32 | +Secondly, you should create a directory named "site" (or any other name |
| 33 | +which is same as __"--webroot"__). |
| 34 | + |
| 35 | + $ mkdir ./site |
| 36 | + |
| 37 | +Thirdly, you can copy the below code into a new file "./site/helloworld.py". |
| 38 | + |
| 39 | + def handler(self): |
| 40 | + return "Hello World!" |
| 41 | + |
| 42 | +Run "example.py", visit "http://localhost:9090/helloworld" via your browser. |
| 43 | +You can see "Hello World!" in your browser. |
| 44 | + |
| 45 | + $ ./example.py |
| 46 | + Litefs 0.3.0 - January 15, 2020 - 10:46:39 |
| 47 | + Starting server at http://localhost:9090/ |
| 48 | + Quit the server with CONTROL-C. |
| 49 | + |
| 50 | +# URL ROUTES |
| 51 | + |
| 52 | +The relative path of the python scripts in the "site" folder will be url routes. |
| 53 | + |
| 54 | +For example, we create tow files in "site" folder like below. |
| 55 | + |
| 56 | +## Script route |
| 57 | + # Python route |
| 58 | + # ./site/hello.py => matches /hello |
| 59 | + def handler(self): |
| 60 | + return 'Hello world' |
| 61 | + |
| 62 | +## Template route |
| 63 | + |
| 64 | +Note: "http" means "self" in handler function. |
| 65 | + |
| 66 | + # Template route |
| 67 | + # ./site/cn/hello.mako => matches /cn/hello |
| 68 | + <pre> |
| 69 | + PATH_INFO : ${http.path_info} |
| 70 | + QUERY_STRING: ${http.query_string} |
| 71 | + REQUEST_URI : ${http.request_uri} |
| 72 | + REFERER : ${http.referer} |
| 73 | + COOKIE : ${http.cookie} |
| 74 | + |
| 75 | + hello world |
| 76 | + </pre> |
| 77 | + |
| 78 | +## Static file |
| 79 | + |
| 80 | + # Other static route => matches /hello.txt |
| 81 | + # ./site/hello.txt |
| 82 | + Hello world |
| 83 | + |
| 84 | +# HTTP Methods |
| 85 | + |
| 86 | +Litefs handlers all methods such as GET, POST, PUT, DELETE or PATCH in |
| 87 | +"handler" function. |
| 88 | + |
| 89 | + def handler(self): |
| 90 | + request_method = self.request_method |
| 91 | + logger.info(request_method) |
| 92 | + return 'request_method: %s' % request_method |
| 93 | + |
| 94 | +# Error Pages |
| 95 | + |
| 96 | +You can set a default 404 page when you start a litefs server. |
| 97 | + |
| 98 | + $ ./example.py --not-found=not_found.html |
| 99 | + |
| 100 | +# Help |
| 101 | + |
| 102 | + $ ./example.py --help |
| 103 | + usage: example.py [-h] [--host HOST] [--port PORT] [--webroot WEBROOT] |
| 104 | + [--debug] [--not-found NOT_FOUND] |
| 105 | + [--default-page DEFAULT_PAGE] [--cgi-dir CGI_DIR] |
| 106 | + [--log LOG] [--listen LISTEN] |
| 107 | + |
| 108 | + Build a web server framework using Python. Litefs was developed to implement a |
| 109 | + server framework that can quickly, securely, and flexibly build Web projects. |
| 110 | + Litefs is a high-performance HTTP server. Litefs has the characteristics of |
| 111 | + high stability, rich functions, and low system consumption. Author: leafcoder |
| 112 | + Email: [email protected] Copyright (c) 2017, Leafcoder. License: MIT (see |
| 113 | + LICENSE for details) |
| 114 | + |
| 115 | + optional arguments: |
| 116 | + -h, --help show this help message and exit |
| 117 | + --host HOST bind server to HOST |
| 118 | + --port PORT bind server to PORT |
| 119 | + --webroot WEBROOT use WEBROOT as root directory |
| 120 | + --debug start server in debug mode |
| 121 | + --not-found NOT_FOUND |
| 122 | + use NOT_FOUND as 404 page |
| 123 | + --default-page DEFAULT_PAGE |
| 124 | + use DEFAULT_PAGE as web default page |
| 125 | + --cgi-dir CGI_DIR use CGI_DIR as cgi scripts directory |
| 126 | + --log LOG save log to LOG |
| 127 | + --listen LISTEN server LISTEN |
| 128 | + |
| 129 | + |
| 130 | +## Context |
| 131 | + |
| 132 | +List attributes of "self". |
| 133 | + |
| 134 | +Attributes | Description |
| 135 | +---------------------------------------------------- | ----------- |
| 136 | +self.environ | 环境变量(只读) |
| 137 | +self.environ`[`_*envname*_`]` | 获取某环境变量 |
| 138 | +self.session | session 对象,可临时保存或获取内存数据 |
| 139 | +self.session_id | session 对象 ID,将通过 SET_COOKIE 环境变量返回给客户端浏览器 |
| 140 | +self.form | form 为字典对象,保存您提交到服务器的数据 |
| 141 | +self.config | 服务器的配置对象,可获取初始化服务器的配置信息 |
| 142 | +self.files | 字典对象,保存上传的文件,格式为:{ *filename1*: *\<StringIO object\>*, *filename2*: *\<StringIO object\>* } |
| 143 | +self.cookie | SimpleCookie 对象,获取 Cookie 数据 |
| 144 | +self.redirect(url=None) | 跳转到某一页面 |
| 145 | +self.start_response(status_code=200, headers=None) | HTTP 返回码和头部 |
| 146 | +self.path_Info | PATH_INFO |
| 147 | +self.query_string | QUERY_STRING |
| 148 | +self.request_uri | REQUEST_URI |
| 149 | +self.referer | REFERER |
| 150 | +self.request_method | REQUEST_METHOD |
| 151 | +self.server_protocol | SERVER_PROTOCOL |
| 152 | +## Environ |
| 153 | + |
| 154 | +环境变量 | 描述 | 例子 |
| 155 | +------------------- | --------------------- | ---- |
| 156 | +REQUEST_METHOD | 请求方法 | GET、POST、PUT、HEAD等 |
| 157 | +SERVER_PROTOCOL | 请求协议/版本 | HTTP/1.1" |
| 158 | +REMOTE_ADDR | 请求客户端的IP地址 | 192.168.1.5 |
| 159 | +REMOTE_PORT | 请求客户端的端口 | 9999 |
| 160 | +REQUEST_URI | 完整 uri | /user_info?name=li&age=20 |
| 161 | +PATH_INFO | 页面地址 | /user_info |
| 162 | +QUERY_STRING | 请求参数 | name=li&age=20 |
| 163 | +CONTENT_TYPE | POST 等报文类型 | application/x-www-form-urlencoded 或 text/html;charset=utf-8 |
| 164 | +CONTENT_LENGTH | POST 等报文长度 | 1024 |
| 165 | +HTTP_*_HEADERNAME_* | 其他请求头部 | 如 HTTP_REFERER:https://www.baidu.com/ |
0 commit comments