Skip to content

Commit abcec1a

Browse files
committed
日常更新
1 parent e4c9549 commit abcec1a

File tree

5 files changed

+105
-10
lines changed

5 files changed

+105
-10
lines changed

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

2-
1. 快速启动
3-
==============
2+
# 1. 快速启动
43

54
1.1 启动脚本
65
---------------
@@ -38,3 +37,58 @@
3837
Hit Ctrl-C to quit.
3938

4039
运行启动脚本后,访问 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

litefs.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
from subprocess import Popen, PIPE
4949
from tempfile import NamedTemporaryFile, TemporaryFile
5050
from time import time, strftime, gmtime
51-
from traceback import print_exc
5251
from urllib import splitport, unquote_plus
5352
from UserDict import UserDict
5453
from uuid import uuid4
@@ -614,10 +613,42 @@ def session_id(self):
614613
def session(self):
615614
return self._session
616615

616+
@property
617+
def request_method(self):
618+
return self.environ['REQUEST_METHOD']
619+
620+
@property
621+
def server_protocol(self):
622+
return self.environ['SERVER_PROTOCOL']
623+
617624
@property
618625
def path_info(self):
619626
return self.environ['PATH_INFO']
620627

628+
@property
629+
def query_string(self):
630+
return self.environ['QUERY_STRING']
631+
632+
@property
633+
def request_uri(self):
634+
environ = self.environ
635+
path_info = environ['PATH_INFO']
636+
query_string = environ['QUERY_STRING']
637+
if not query_string:
638+
return path_info
639+
return '?'.join((path_info, query_string))
640+
641+
@property
642+
def referer(self):
643+
return self.environ.get('HTTP_REFERER')
644+
645+
@property
646+
def cookie(self):
647+
cookie_str = self.environ.get('HTTP_COOKIE', '')
648+
cookie = SimpleCookie()
649+
cookie.load(cookie_str)
650+
return cookie
651+
621652
def start_response(self, status_code=200, headers=None):
622653
buffers = self._buffers
623654
if self._headers_responsed:
@@ -1033,7 +1064,7 @@ class Litefs(object):
10331064

10341065
def __init__(self, **kwargs):
10351066
self.config = config = make_config(**kwargs)
1036-
level = logging.ERROR if config.debug else logging.DEBUG
1067+
level = logging.DEBUG if config.debug else logging.ERROR
10371068
self.logger = make_logger(__name__, level=level)
10381069
self.server_info = server_info = make_server(
10391070
config.address, request_size=config.request_size

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
long_description=litefs.__doc__,
2525
author=__author__,
2626
author_email='leafcoder@gmail.com',
27-
url='https://coding.net/u/leafcoder/p/litefs',
27+
url='https://github.com/leafcoder/litefs',
2828
py_modules=['litefs'],
2929
ext_modules=cythonize('litefs.py'),
3030
scripts=['litefs.py'],

site/helloword.mako

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
<%def name="handler(self)">
2-
hello world
31

4-
{{'Good'}}
5-
</%def>
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>

site/index.html.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
def handler(self):
2-
return 'ok'
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+
'''

0 commit comments

Comments
 (0)