嵌入式http服务器和客户端库
degu的Python项目详细描述
degu是python3的嵌入式http服务器和客户端库。
它可以用来构建网络透明的服务,不管另一个端点 在云中,在本地网络上,在本地主机上,甚至在 通过AF_UNIX使用http的本地主机。
degu特别适合为设备到设备实现restapi 通讯(物联网)。它是未来发展的基石, 你的车进入了一个大胆的,未知的领域。
degu是LGPLv3+授权的,需要Python 3.4或更新的,并且完全 支持Python 3.5。
一些值得注意的功能:
- Degu fully exposes HTTP chunked transfer-encoding semantics, including the optional per-chunk extension
- Degu fully exposes IPv6 address semantics, including the scopeid needed for IPv6 link-local addresses
- Degu transparently supports ^{tt2}$, ^{tt3}$, and ^{tt1}$, all via a single address argument used uniformly by the server and client
- Degu provides a safe and opinionated API for using TLSv1.2, with a particular focus on using client certificates to authenticate incoming TCP connections
示例
定义一个简单的rest网关接口(rgi)服务器应用程序:
>>> def app(session, request, api): ... if request.method != 'GET': ... return (405, 'Method Not Allowed', {}, None) ... if request.path != ['example']: ... return (404, 'Not Found', {}, None) ... return (200, 'OK', {}, b'hello, world') ...
在一个废弃的服务器上运行上面的app 端口:
>>> from degu.misc import TempServer >>> server = TempServer(('127.0.0.1', 0), app)
创建与上述服务器建立连接的客户端:
>>> from degu.client import Client >>> client = Client(server.address)
(属性server.address将包括 内核。)
创建连接并发出'GET'请求:
>>> conn = client.connect() >>> response = conn.get('/example', {})
返回值是Responsenamedtuple:
>>> response Response(status=200, reason='OK', headers={'content-length': 12}, body=Body(<reader>, 12))
按如下方式阅读响应正文:
>>> response.body.read() b'hello, world'
再次请求'GET',这次请求的uri将返回 404未找到错误:
>>> conn.get('/nope', {}) Response(status=404, reason='Not Found', headers={}, body=None)
性能
degu服务器和客户端使用c.degu中实现的共享http后端。 在适度运行时为低延迟和高吞吐量而优化 同意。
当两个端点都在本地主机上运行时,degu client+server duo是 对于通过同一个 连接。
在运行ubuntu 14.04 lts(64位)的intel i7-4900mqcpu上,degu可以实现 平均值:
- Over 76k request/response round-trips per second over ^{tt1}$ (less than 13.2μs per round-trip)
- Over 53k request/response round-trips per second over ^{tt3}$ (less than 18.9μs per round-trip)
这种级别的性能使http在进程间完全可行 通信(ipc),附加的好处是您可以获得相同的rest api 天哪,服务器是本地运行还是远程运行。
Novacut组件
德古正在作为Novacut项目的一部分进行开发。包裹是 可用于Novacut Stable Releases PPA中的Ubuntu和 Novacut Daily Builds PPA。
如果你有问题或者需要帮助开始学习德古,请停止 通过freenode上的#novacutirc通道。