无需任何I/O即可编写网络协议分析器的实用程序
ohneio的Python项目详细描述
用于编写网络协议分析器sans I/O的实用程序库
OhneI/O(德语中没有I/O)是一个使用 asyncio协同编程风格。
ohneio允许您以编写异步协议的方式编写协议解析器:
>>>importbase64>>>importohneio>>>>>>defwait_for(s):...whileTrue:...data=yield fromohneio.peek()...pos=data.find(s)...ifpos>=0:...returnpos...yield fromohneio.wait()...>>>defread_until(s):...pos=yield fromwait_for(s)...data=yield fromohneio.read(pos)...returndata...>>>@ohneio.protocol...defecho_base64(separator):...whileTrue:...segment=yield fromread_until(separator)...yield fromohneio.read(len(separator))...yield fromohneio.write(base64.b64encode(segment)+separator)...>>>connection=echo_base64(b'\r\n')>>>connection.send(b'hello')>>>connection.read()b''>>>connection.send(b'\r\nworld\r\n')>>>connection.read()b'aGVsbG8=\r\nd29ybGQ=\r\n'
上面的示例还显示了ohneio如何允许组合原语 进入更大的解析函数(比如wait_for和read_until)。