后台小程序开发的全方位指南
632
2022-10-30
netius:可读的、简单和快速异步非阻塞的网络应用程序
Fast and readable async non-blocking network apps
Netius is a python network library that can be used for the rapid creation of asynchronous non-blocking servers and clients. It has no dependencies, it's cross-platform, and brings some sample netius-powered servers out of the box, namely a production-ready WSGI server.
Simplicity and performance are the main drivers of this project. The codebase adheres to very strict code standards, and is extensively commented; and as far as performance is concerned, it aims to be up to par with equivalent native implementations, where PyPy can be used to provide the extra boost to raise performance up to these standards.
Bear in mind that although netius is non-blocking, it will naturally still block if the operations performed within the event loop are blocking, like reading or writing a file, which are both blocking operations in the Python standard library. Running multiple netius instances in parallel, and having a fast server like NGINX act as their reverse proxy, is one way of minimising the perceptibility of such blockages.
Installation
pip install netius
Or download the source from GitHub.
Netius has no dependencies, and is therefore cross-platform. It's compatible with PyPy, with which it benefits of performance increases up to 1.5x - 2.5x faster in most environments, when compared with running it with the cPython interpreter.
Usage
WSGI Server
import netius.serversdef app(environ, start_response): status = "200 OK" contents = "Hello World" content_l = len(contents) headers = ( ("Content-Length", content_l), ("Content-Type", "text/plain"), ("Connection", "keep-alive") ) start_response(status, headers) yield contentsserver = netius.servers.WSGIServer(app = app)server.serve(port = 8080)
HTTP Client
Synchronous usage
import netius.clientsresult = netius.clients.HTTPClient.get_s( "http://flickr.com/", asynchronous = False)print(result["data"])
Asynchronous usage
import netius.clientsdef on_partial(client, parser, data): print(data)def on_message(client, parser, message): netius.clients.HTTPClient.cleanup_s()netius.clients.HTTPClient.get_s( "http://flickr.com/", callback = on_message, on_data = on_partial)
Test servers
The servers that come with netius out-of-the-box, can be tested through the command line:
WSGIServer - python -m netius.servers.wsgiFTPServer - python -m netius.servers.ftpHelloServer - MESSAGE="Hello Netius" python -m netius.extra.helloFileServer - BASE_PATH=/ python -m netius.extra.fileSMTPServer - python -m netius.servers.smtpRelaySMTPServer - python -m netius.extra.smtp_r
Learn more
Basic
Configuration - how to configure your server/client
Advanced topics
More information can be found in the Advanced Topics page.
License
Netius is currently licensed under the Apache License, Version 2.0.
Build Automation
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~