Skip to content

Commit 05975f6

Browse files
committed
调整项目目录结构,完善项目发布相关代码
1 parent fa739c3 commit 05975f6

File tree

13 files changed

+188
-2
lines changed

13 files changed

+188
-2
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include LICENSE requirements.txt
2+
recursive-include demo *.*

README

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
# 1. 快速启动
3+
4+
1.1 启动脚本
5+
---------------
6+
7+
import sys
8+
sys.dont_write_bytecode = True
9+
10+
import litefs
11+
litefs = litefs.Litefs(
12+
address='0.0.0.0:8080', webroot='./site', debug=True
13+
)
14+
litefs.run(timeout=2.)
15+
16+
将上面的代码保存为 run.py 文件。
17+
18+
1.2 页面脚本
19+
---------------
20+
21+
在网站目录(注:启动脚本中 webroot 的目录)中,添加一个后缀名为 **.py** 的文件,如 example.py,代码如下:
22+
23+
def handler(self):
24+
self.start_response(200, headers=[])
25+
return 'Hello world!'
26+
27+
or
28+
29+
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

demo/example.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
sys.dont_write_bytecode = True
5+
6+
import litefs
7+
litefs = litefs.Litefs(
8+
address='localhost:8080', webroot='./site', debug=True
9+
)
10+
litefs.run(timeout=2.)

demo/site/form.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>表单提交、支持jQuery提交格式</title>
6+
</head>
7+
<body>
8+
<h1>表单提交一</h1>
9+
<form action="./form" method="post">
10+
<div><input type="text" name="name"></div>
11+
<br>
12+
<div><input type="submit" value="提交" /></div>
13+
</form>
14+
<h1>表单提交二</h1>
15+
<form action="./form" method="post">
16+
<div><input type="text" name="name"></div>
17+
<br>
18+
<div><input type="text" name="name"></div>
19+
<br>
20+
<div><input type="submit" value="提交" /></div>
21+
</form>
22+
</body>
23+
</html>

demo/site/form.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def handler(self):
2+
print self.form
3+
return 'ok'

demo/site/helloword.mako

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
<pre>
3+
PATH_INFO : ${http.path_info}
4+
QUERY_STRING: ${http.query_string}
5+
REQUEST_URI : ${http.request_uri}
6+
REFERER : ${http.referer}
7+
COOKIE : ${http.cookie}
8+
9+
hello world
10+
</pre>

demo/site/index.html.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def handler(self):
2+
return '''
3+
<a href="helloword">helloword</a>
4+
<a href="pathinfo">pathinfo</a>
5+
<a href="upload.html">upload.html</a>
6+
<a href="form.html">form.html</a>
7+
'''

demo/site/pathinfo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def handler(self):
2+
print self.environ
3+
print self.path_info
4+
return 'ok'

demo/site/upload.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>文件上传例子</title>
6+
</head>
7+
<body>
8+
<h1>上传文件</h1>
9+
<form action="./upload" method="post" enctype="multipart/form-data">
10+
<div><input type="file" name="file" /></div>
11+
<br>
12+
<div><input type="file" name="file1" /></div>
13+
<br>
14+
<div><input type="submit" value="开始上传" /></div>
15+
</form>
16+
</body>
17+
</html>

demo/site/upload.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def handler(self):
2+
files = self.files
3+
print files
4+
return 'ok'

0 commit comments

Comments
 (0)