一个简单的、动态的、基于decorator的http服务器,灵感来自node.js的express。支持Trio、TLS和WebSocket。

dhttp的Python项目详细描述


DHTTP

dhttpdecarable http的缩写,它是 为python 3编写的简单、动态的http服务器。
它的灵感来自node.js的express库。

“decarable”指的是装饰师有多广泛 在整个项目中使用。它们非常有用,而且 是回调系统中最好的语法之一,比如 这个。

示例代码

import dhttp
import random

app = dhttp.DHTTPServer(int(sys.argv[1]) if len(sys.argv) > 1 else 8005)

app.alias('/index', '/')
app.alias('/index.htm', '/')
app.alias('/index.html', '/')

test_index = """<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>My first DHTTP server</title>
    </head>

    <body>
        <p><h2>Congratulations!</h2></p>
        <hr>
        <p><b>dhttp {version}</b> is now running on your machine.</p>
        <p>How about <i>{party}</i> to comemorate? :)</p>
    </body>
</html>"""

party_stuff = [
    'a bottle of wine', 'a bottle of champagne', 'a big party',
    'THE party, just', 'THE party', 'lots of cats', 'partyception',
    'balloons and cakes', 'a big-endian cake', 'lots of confetti',
    'the Confetti-o-Tron 2000', 'HTTP juice', 'Spicy Bytes',
    'antimatter', 'cats writing code', 'a smile breaking the 4th wall'
]

@app.get('/')
def serve_index(req, res):
    res.end(test_index.format(
        party = random.choice(party_stuff),
        version = DHTTP_VERSION
    ))

@app.on_log
def print_log(log):
    if log.request.get_header('X-Forwarded-For') is not None:
        log.ip = log.request.get_header('X-Forwarded-For')
        print(log, '  (forwarded)')

    else:
        print(log)

@app.serve_forever
def on_serve():
    print(f"   == Listening on port: {app.port} ==")

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

推荐PyPI第三方库


热门话题
java JAXB封送字符串,具有xml值,且不转义该值   java ModelMapper转换器不工作   java像HH000412或HCANN000001这样的前缀是什么意思?   验证日期输入修复java。lang.numberformatexception错误   当表具有外键时,java Telosys代码生成失败   如何使所有派生类一起只能实例化一个实例的单例抽象基类?(爪哇)   java如何在非静态服务类中使用广播接收器   java nutch爬虫相对URL问题   使用Jboss DMR下载/保存java附件   Rest模板:无法提取响应:当我们得到xml响应时,没有找到适合响应类型的HttpMessageConverter,没有绑定到JAVA对象   java如何编写可扩展窗格/面板/卡的代码   java是在ITreeViewerListener的treeExpanded()之前调用ContentProvider的getChildren()吗?   java将JComponent添加到小程序窗格   java混淆:使用简单逻辑的Flames程序