这是一个简单的http服务器,采用类似mvc的设计。

simple-http-server的Python项目详细描述


#python simple http server

类似于请求映射的VC。
*易于使用。
*自由式控制器编写。


<









``shell
` ` ` ` `
` ` ` ` ` ` `
` ` ` ` ` ` ` ` `
` ` ` ` `
` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` u http_server import request_map
来自simple_http_server import response
from simple_http_server import multipartfile
from simple_http_server import参数
from simple_http_server import参数
from simple_http_server import header
from simple_http_server import jsonbody
from simple_http_server import httpe错误
从简单http服务器导入静态文件
从简单http服务器导入头
从简单http服务器导入cookie
从简单http服务器导入cookie
从简单http服务器导入重定向


e":0,"message":"success"}您可以返回字典、字符串或"simple_http_http_http.server.response"对象。



@请求映射("/say_http",method=["get","post"])
def my_ctrl2(name,name2=parameter("name",default="keijack"):
"name和name2是"ame"""
返回"<;!doctype html>;<;html>;<;body>;你好,%s,%s<;/body>;<;/html>;"(名称,名称2)



@request-u-map("/error")
def my-ctrl3():
返回响应(状态代码=500)



@request-u-map("/exception")
def exception-ctrl():
引发httperror(400,"例外"打开")

@请求映射("/upload",method="get")
def show_upload():
根=os.path.dirname(os.path.abspath(u file_uu))
返回静态文件("%s/my_u dev/my_u test_u index.html"%root,"text/html;charset=utf-8")

@请求映射("/upload",method="post")
定义my_u upload(img=multipartfile("img"):
root=os.path.dirname(os.path.abspath(uu file_uu))
img.save_u to_u file(root+"/my_u dev/imgs/"+img.filename)
return"<;!doctype html>;<;html>;<;body>;上传确定!<;/body>;<;/html>;"



@request_map("/post_txt",method="post")
def normal_form_post(txt):
return"<;!doctype html>;<;html>;<;body>;嗨,%s<;/body>;%txt

@request-map("/tuple")
def-tuple-results():
#输入的第一个有效对象(str、unicode、dict、staticfile、bytes)将被视为body
return 200,headers({"my header":"headers"})、{"success":true}

"
"cookie sc将不会写入response。这只是某种默认值
"value
""
@request-map("tuple-cookie")
def-tuple-u with-cookie s(all-cookies=cookies(),cookie-sc=cookie("sc"):
打印("cookies")
打印(all-cookies)
打印("cookies-sc")
打印(cookie-sc)
("==<;"
import datetime
expires=datetime.datetime(2018,12,31)

cks=cookies.simplecookie();您还可以使用内置cookie对象
cks["ck1"]="keijack"
cks["ck1"]["path"]="/"
cks["ck1"]["expires"]=expires.strftime(cookies.expire\u date\u格式)
您可以忽略此元组中的状态代码、头、cookies甚至正文。
返回头({"x x":"yyy"})、cks,"<;html>;<;正文>;确定<;/正文>;<;html>;"

"
"如果您访问/a/b/xyz/x,此控制器函数将被调用,并且"路径值"将是"xyz"
"
@请求映射("/a/b/b{path'u val}/x")
def my'u path'u val'u val'u ctr(path'u val=pathvalue()):
return"<;lt;html>;%s<;lt;lt;body>;%s<;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;html>;%path'u val



@request'u map(/redirect)

>def redirect()
>retretreturnredirect(/index"/index")

<


<



`` python
从简单的http_服务器导入筛选器映射


headers["filter set"]="through filter"
如果"user_name"不在ctx.request.parameter:
ctx.response.send_redirect("/index")
elif"pass"不在ctx.request.parameter:
ctx.response.send_error(400,"pass should be passed")
error
raise httperror(400,"pass should be passed")
否则:
您应该始终使用do\u chain方法转到下一个
ctx.do\u chain()
`````

应该将它们导入到这里。












>def main(*args):
>server.start()



>如果\\\\\\\\\\\\\\\\





>server.start \\\\\\\\;Unicode支持

,尽管我已经尝试过为了解决Python2.7中的Unicode问题,Python2.7对Unicode非常不友好,因此仍然可能导致一些问题。确保Unicode工作的最佳方法是使用Python3.6+


\n多线程安全性

因为这是一个简单的HTTP服务器,我没有做太多工作来确保多线程安全。如果您试图在一个请求作用域(包括在筛选器和控制器函数中)的多线程中将数据写入"request"和"response"对象,可能会导致一些问题。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在reducer中迭代自定义可写组件时出现java问题   属性文件中属性的java命名约定   任务链关闭的java Executor服务   java从Eclipse中的字段生成多个构造函数   java通过继承读取Json   java在不知道密钥的情况下解析json   java camel cxf如何在电子邮件中发送soap请求响应   java程序似乎跳过了if语句的一部分,在移回正确位置之前先移到else语句   测试简单的Java加密/解密inputFileName不存在   java从Jenkins REST API获取所有作业的所有构建的构建细节   java基本包装器和静态“类型”类对象   在WebSphere8.5上部署java代码   java对象相等(对象引用“=”)   java MongoDB整型字段到枚举的转换   每次我重新导入gradle时,IntelliJ都会不断重置Java设置   类型使用键或索引从Java中的数据类型检索值   在Java的列表接口中需要listIterator()和iterator()是什么?