python编程语言的异步事件驱动程序web框架。
Pyjoyment的Python项目详细描述
快乐
python编程语言的异步事件驱动程序web框架。
pyjoyment提供了自己的reactor,它自己处理i/o和计时器事件。 主事件循环,但它支持其他循环,如libev、asyncio。
Pyjoyment密集使用自己的事件编辑器,这应该是熟悉的 node.js程序员。
它提供了用于解析和创建http消息和html文档的工具集。 它还支持wsgi接口。
pyjoyment与python 2.7+、python 3.3+和pypypy2.4+兼容。不是的 需要任何外部库或编译器。
Pyjoyment基于Mojolicious:下一代 Perl编程语言的Web框架。
状态
早期发育阶段。已实施:
- wsgi适配器
- HTTP独立异步IO服务器
- WebSocket客户端和服务器
- 支持TLS/SSL的HTTP用户代理
- 基于RFC6901 的json指针实现
- 嵌入式文件加载器
- 带有css选择器的html/xml dom解析器
- 具有url、path和querystring的容器类的url解析器
- 无阻塞TCP客户端和服务器
- 简单日志对象
- 多个事件的同步器和序列化器
- 处理IO和计时器事件的主事件循环
- 订阅的事件发射器
- 基于select(2)和poll(2) 的低级事件反应器
- 方便的函数,并为Unicode和字节字符串及列表分类
- 对象的惰性属性
- 使用基于perl的Test::More和TAP协议的api测试单元
示例
刮网
importPyjo.UserAgentfromPyjo.String.Unicodeimportutx=Pyjo.UserAgent.new().get('https://html.spec.whatwg.org')fornintx.res.dom('#named-character-references-table tbody > tr').each():u(n.at('td > code').text+' '+n.children('td')[1].text).trim().say()
url操作
importPyjo.URLfromPyjo.String.Unicodeimportu# 'ssh+git://git@github.com/dex4er/Pyjoyment.git'url=Pyjo.URL.new('https://github.com/dex4er/Pyjoyment')print(url.set(scheme='ssh+git',userinfo='git',path=u(url.path)+'.git'))# 'http://metacpan.org/search?q=Mojo::URL&size=20'print(Pyjo.URL.new('http://metacpan.org/search').set(query={'q':'Mojo::URL','size':20}))
非阻塞TCP客户机/服务器
importPyjo.IOLoop# Listen on port 3000@Pyjo.IOLoop.server(port=3000)defserver(loop,stream,cid):@stream.ondefread(stream,chunk):# Process input chunkprint("Server: {0}".format(chunk.decode('utf-8')))# Write responsestream.write(b"HTTP/1.1 200 OK\x0d\x0a\x0d\x0a")# Disconnect clientstream.close_gracefully()# Connect to port 3000@Pyjo.IOLoop.client(port=3000)defclient(loop,err,stream):@stream.ondefread(stream,chunk):# Process inputprint("Client: {0}".format(chunk.decode('utf-8')))# Write requeststream.write(b"GET / HTTP/1.1\x0d\x0a\x0d\x0a")# Add a timer@Pyjo.IOLoop.timer(3)deftimeouter(loop):print("Timeout")# Shutdown serverloop.remove(server)# Start event loopPyjo.IOLoop.start()
提供嵌入式模板文件的独立http服务器
# -*- coding: utf-8 -*-importPyjo.Server.DaemonimportPyjo.URLfromPyjo.Loaderimportembedded_filefromPyjo.Utilimportb,uimportsysopts=dict([['address','0.0.0.0'],['port',3000]]+list(map(lambdaa:a.split('='),sys.argv[1:])))listen=str(Pyjo.URL.new(scheme='http',host=opts['address'],port=opts['port']))daemon=Pyjo.Server.Daemon.new(listen=[listen])daemon.unsubscribe('request')# Embedded template fileDATA=u(r''' @@ index.html.tpl <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Pyjoyment</title> </head> <body> <h1>♥ Pyjoyment ♥</h1> <h2>This page is served by Pyjoyment framework.</h2> <p>{method} request for {path}</p> </body> </html> ''')@daemon.ondefrequest(daemon,tx):# Requestmethod=tx.req.methodpath=tx.req.url.path# Templatetemplate=embedded_file(sys.modules[__name__],'index.html.tpl')# Responsetx.res.code=200tx.res.headers.content_type='text/html; charset=utf-8'tx.res.body=b(template.format(**locals()))# Resume transactiontx.resume()daemon.run()