|
| 1 | +# Litefs: A Lite Python Web Framework |
1 | 2 |
|
2 |
| -# 1. 快速启动 |
| 3 | +Build a web server framework using Python. Litefs was developed to implement a server framework that can quickly, securely, and flexibly build Web projects. Litefs is a high-performance HTTP server. Litefs has the characteristics of high stability, rich functions, and low system consumption. |
3 | 4 |
|
4 |
| -1.1 启动脚本 |
5 |
| ---------------- |
| 5 | +## Installation |
6 | 6 |
|
7 |
| - import sys |
8 |
| - sys.dont_write_bytecode = True |
| 7 | +It can be installed via pip: |
9 | 8 |
|
10 |
| - import litefs |
11 |
| - litefs = litefs.Litefs( |
12 |
| - address='0.0.0.0:8080', webroot='./site', debug=True |
13 |
| - ) |
14 |
| - litefs.run(timeout=2.) |
| 9 | + $ pip install litefs |
15 | 10 |
|
16 |
| -将上面的代码保存为 run.py 文件。 |
| 11 | +It can be installed via source code: |
17 | 12 |
|
18 |
| -1.2 页面脚本 |
19 |
| ---------------- |
| 13 | + $ git clone https://github.com/leafcoder/litefs.git litefs |
| 14 | + $ cd litefs |
| 15 | + $ python setup.py install |
20 | 16 |
|
21 |
| -在网站目录(注:启动脚本中 webroot 的目录)中,添加一个后缀名为 **.py** 的文件,如 example.py,代码如下: |
| 17 | +## Quickstart: "Hello world" |
22 | 18 |
|
23 |
| - def handler(self): |
24 |
| - self.start_response(200, headers=[]) |
25 |
| - return 'Hello world!' |
| 19 | +Firstly, let's write a basci example via litefs. Save it to "example.py". |
| 20 | + |
| 21 | + # /usr/bin/env python |
| 22 | + |
| 23 | + import litefs |
| 24 | + litefs.test_server() |
| 25 | + |
| 26 | +Secondly, you should create a directory named "site" (or any other name which is same as __"--webroot"__). |
| 27 | + |
| 28 | + $ mkdir ./site |
26 | 29 |
|
27 |
| - or |
| 30 | +Thirdly, you can copy the below code into a new file "./site/helloworld.py". |
28 | 31 |
|
29 | 32 | def handler(self):
|
30 |
| - return 'Hello world!' |
31 |
| - |
32 |
| -1.3 启动网站 |
33 |
| ------------ |
34 |
| - |
35 |
| - $ python run.py |
36 |
| - Server is running at 0.0.0.0:8080 |
37 |
| - Hit Ctrl-C to quit. |
38 |
| - |
39 |
| -运行启动脚本后,访问 http://0.0.0.0:8080/example,您会看到 `Hello world!`。 |
40 |
| - |
41 |
| - |
42 |
| -# 2. CGI 规则 |
43 |
| - |
44 |
| -2.1 httpfile 对象是服务器上下文接口,接口如下: |
45 |
| --------------------------------------- |
46 |
| - |
47 |
| -接口类型 | 接口使用 | 接口描述 |
48 |
| ----- | --- | ---- |
49 |
| -环境变量(只读) | httpfile.environ | 环境变量 |
50 |
| -某环境变量 | httpfile.environ`[`_*envname*_`]` | 获取某环境变量 |
51 |
| -Session | httpfile.session | session 对象,可临时保存或获取内存数据 |
52 |
| -Session ID | httpfile.session_id | session 对象 ID,将通过 SET_COOKIE 环境变量返回给客户端浏览器 |
53 |
| -Form | httpfile.form | form 为字典对象,保存您提交到服务器的数据 |
54 |
| -Config | httpfile.config | 服务器的配置对象,可获取初始化服务器的配置信息 |
55 |
| -files | httpfile.files | 字典对象,保存上传的文件,格式为:{ *filename1*: *\<StringIO object\>*, *filename2*: *\<StringIO object\>* } |
56 |
| -cookie | httpfile.cookie | SimpleCookie 对象,获取 Cookie 数据 |
57 |
| -页面跳转 | httpfile.redirect(url=None) | 跳转到某一页面 |
58 |
| -HTTP头部 | httpfile.start_response(status_code=200, headers=None) | HTTP 返回码和头部 |
59 |
| - |
60 |
| -2.2 以下为 httpfile 对象中环境变量(environ)包含的变量对应表 |
61 |
| ---------------------------------------------------- |
62 |
| - |
63 |
| -环境变量 | 描述 | 例子 |
64 |
| -------- | ------ | ---- |
65 |
| -REQUEST_METHOD | 请求方法 | GET、POST、PUT、HEAD等 |
66 |
| -SERVER_PROTOCOL | 请求协议/版本 | HTTP/1.1" |
67 |
| -REMOTE_ADDR | 请求客户端的IP地址 | 192.168.1.5 |
68 |
| -REMOTE_PORT | 请求客户端的端口 | 9999 |
69 |
| -REQUEST_URI | 完整 uri | /user_info?name=li&age=20 |
70 |
| -PATH_INFO | 页面地址 | /user_info |
71 |
| -QUERY_STRING | 请求参数 | name=li&age=20 |
72 |
| -CONTENT_TYPE | POST 等报文类型 | application/x-www-form-urlencoded 或 text/html;charset=utf-8 |
73 |
| -CONTENT_LENGTH | POST 等报文长度 | 1024 |
74 |
| -HTTP_*_HEADERNAME_* | 其他请求头部 | 如 HTTP_REFERER:https://www.baidu.com/ |
75 |
| - |
76 |
| -2.3 部分环境变量也可以使用 httpfile 属性的方式获取 |
77 |
| ------------------------------------------- |
78 |
| - |
79 |
| -环境变量 | 对应属性 |
80 |
| -------- | ------- |
81 |
| -PATH_INFO | httpfile.path_Info |
82 |
| -QUERY_STRING | httpfile.query_string |
83 |
| -REQUEST_URI | httpfile.request_uri |
84 |
| -REFERER | httpfile.referer |
85 |
| -REQUEST_METHOD | httpfile.request_method |
86 |
| -SERVER_PROTOCOL | httpfile.server_protocol |
87 |
| - |
88 |
| -# 3. Mako 文件支持 |
89 |
| - |
90 |
| -TODO |
91 |
| - |
92 |
| -# 4. CGI 支持 |
93 |
| - |
94 |
| -TODO |
| 33 | + return "Hello World!" |
| 34 | + |
| 35 | +Run "example.py", visit "http://localhost:9090/helloworld" via your browser. You can see "Hello World!" in your browser. |
| 36 | + |
| 37 | + $ ./example.py |
| 38 | + Litefs 0.3.0 - January 15, 2020 - 10:46:39 |
| 39 | + Starting server at http://localhost:9090/ |
| 40 | + Quit the server with CONTROL-C. |
| 41 | + |
| 42 | +## Command Help |
| 43 | + |
| 44 | + $ ./example --help |
| 45 | + usage: example.py [-h] [--host HOST] [--port PORT] [--webroot WEBROOT] |
| 46 | + [--debug] [--not-found NOT_FOUND] |
| 47 | + [--default-page DEFAULT_PAGE] [--cgi-dir CGI_DIR] |
| 48 | + [--log LOG] [--listen LISTEN] |
| 49 | + |
| 50 | + Build a web server framework using Python. Litefs was developed to implement a |
| 51 | + server framework that can quickly, securely, and flexibly build Web projects. |
| 52 | + Litefs is a high-performance HTTP server. Litefs has the characteristics of |
| 53 | + high stability, rich functions, and low system consumption. Author: leafcoder |
| 54 | + Email: [email protected] Copyright (c) 2017, Leafcoder. License: MIT (see |
| 55 | + LICENSE for details) |
| 56 | + |
| 57 | + optional arguments: |
| 58 | + -h, --help show this help message and exit |
| 59 | + --host HOST bind server to HOST |
| 60 | + --port PORT bind server to PORT |
| 61 | + --webroot WEBROOT use WEBROOT as root directory |
| 62 | + --debug start server in debug mode |
| 63 | + --not-found NOT_FOUND |
| 64 | + use NOT_FOUND as 404 page |
| 65 | + --default-page DEFAULT_PAGE |
| 66 | + use DEFAULT_PAGE as web default page |
| 67 | + --cgi-dir CGI_DIR use CGI_DIR as cgi scripts directory |
| 68 | + --log LOG save log to LOG |
| 69 | + --listen LISTEN server LISTEN |
| 70 | + |
| 71 | + |
| 72 | +## Object "self " |
| 73 | + |
| 74 | +List attributes of "self". |
| 75 | + |
| 76 | +Attributes | Description |
| 77 | +---------------------------------------------------- | ----------- |
| 78 | +self.environ | 环境变量(只读) |
| 79 | +self.environ`[`_*envname*_`]` | 获取某环境变量 |
| 80 | +self.session | session 对象,可临时保存或获取内存数据 |
| 81 | +self.session_id | session 对象 ID,将通过 SET_COOKIE 环境变量返回给客户端浏览器 |
| 82 | +self.form | form 为字典对象,保存您提交到服务器的数据 |
| 83 | +self.config | 服务器的配置对象,可获取初始化服务器的配置信息 |
| 84 | +self.files | 字典对象,保存上传的文件,格式为:{ *filename1*: *\<StringIO object\>*, *filename2*: *\<StringIO object\>* } |
| 85 | +self.cookie | SimpleCookie 对象,获取 Cookie 数据 |
| 86 | +self.redirect(url=None) | 跳转到某一页面 |
| 87 | +self.start_response(status_code=200, headers=None) | HTTP 返回码和头部 |
| 88 | + |
| 89 | +## HTTP Headers in Environ |
| 90 | + |
| 91 | +环境变量 | 描述 | 例子 |
| 92 | +------------------- | --------------------- | ---- |
| 93 | +REQUEST_METHOD | 请求方法 | GET、POST、PUT、HEAD等 |
| 94 | +SERVER_PROTOCOL | 请求协议/版本 | HTTP/1.1" |
| 95 | +REMOTE_ADDR | 请求客户端的IP地址 | 192.168.1.5 |
| 96 | +REMOTE_PORT | 请求客户端的端口 | 9999 |
| 97 | +REQUEST_URI | 完整 uri | /user_info?name=li&age=20 |
| 98 | +PATH_INFO | 页面地址 | /user_info |
| 99 | +QUERY_STRING | 请求参数 | name=li&age=20 |
| 100 | +CONTENT_TYPE | POST 等报文类型 | application/x-www-form-urlencoded 或 text/html;charset=utf-8 |
| 101 | +CONTENT_LENGTH | POST 等报文长度 | 1024 |
| 102 | +HTTP_*_HEADERNAME_* | 其他请求头部 | 如 HTTP_REFERER:https://www.baidu.com/ |
| 103 | + |
| 104 | +## Get HTTP Header via "self" |
| 105 | + |
| 106 | +环境变量 | 对应属性 |
| 107 | +--------------- | ------- |
| 108 | +PATH_INFO | self.path_Info |
| 109 | +QUERY_STRING | self.query_string |
| 110 | +REQUEST_URI | self.request_uri |
| 111 | +REFERER | self.referer |
| 112 | +REQUEST_METHOD | self.request_method |
| 113 | +SERVER_PROTOCOL | self.server_protocol |
| 114 | + |
| 115 | +## Mako Template Page |
| 116 | + |
| 117 | +## CGI Script Page |
| 118 | + |
| 119 | +## Python Script Page |
| 120 | + |
| 121 | +## Static Files |
0 commit comments