跳转到主要内容

Python 3 asyncio Telnet服务器和客户端协议库

项目描述

Coveralls Code Coverage Latest Version Downloads

简介

telnetlib3是一个用于Python的Telnet客户端和服务器库。此项目需要Python 3.7及以后版本,使用asyncio模块。

快速示例

使用流接口编写提供基本战争游戏的Telnet服务器

import asyncio, telnetlib3

async def shell(reader, writer):
    writer.write('\r\nWould you like to play a game? ')
    inp = await reader.read(1)
    if inp:
        writer.echo(inp)
        writer.write('\r\nThey say the only way to win '
                     'is to not play at all.\r\n')
        await writer.drain()
    writer.close()

loop = asyncio.get_event_loop()
coro = telnetlib3.create_server(port=6023, shell=shell)
server = loop.run_until_complete(coro)
loop.run_until_complete(server.wait_closed())

编写使用此服务器玩战争游戏的Telnet客户端

import asyncio, telnetlib3

async def shell(reader, writer):
    while True:
        # read stream until '?' mark is found
        outp = await reader.read(1024)
        if not outp:
            # End of File
            break
        elif '?' in outp:
            # reply all questions with 'y'.
            writer.write('y')

        # display all server output
        print(outp, flush=True)

    # EOF
    print()

loop = asyncio.get_event_loop()
coro = telnetlib3.open_connection('localhost', 6023, shell=shell)
reader, writer = loop.run_until_complete(coro)
loop.run_until_complete(writer.protocol.waiter_closed)

命令行

此软件包包含两个命令行脚本。

telnetlib3-client

小型终端Telnet客户端。一些示例目标选项

telnetlib3-client nethack.alt.org
telnetlib3-client --encoding=cp437 --force-binary blackflag.acid.org
telnetlib3-client htc.zapto.org

telnetlib3-server

提供默认调试Shell的Telnet服务器。这提供了一个简单的Shell服务器,允许检查会话值,例如

tel:sh> help
quit, writer, slc, toggle [option|all], reader, proto

tel:sh> writer
<TelnetWriter server mode:kludge +lineflow -xon_any +slc_sim server-will:BINARY,ECHO,SGA client-will:BINARY,NAWS,NEW_ENVIRON,TTYPE>

tel:sh> reader
<TelnetReaderUnicode encoding='utf8' limit=65536 buflen=0 eof=False>

tel:sh> toggle all
wont echo.
wont suppress go-ahead.
wont outbinary.
dont inbinary.
xon-any enabled.
lineflow disabled.

tel:sh> reader
<TelnetReaderUnicode encoding='US-ASCII' limit=65536 buflen=1 eof=False>

tel:sh> writer
<TelnetWriter server mode:local -lineflow +xon_any +slc_sim client-will:NAWS,NEW_ENVIRON,TTYPE>

这两个命令行脚本都接受参数 --shell=my_module.fn_shell,该参数描述了一个协程的Python模块路径,其签名形式为 shell(reader, writer),就像上面的示例一样。

功能

实现了以下RFC规范

  • rfc-727,“Telnet注销选项”,1977年4月。

  • rfc-779,“Telnet发送位置选项”,1981年4月。

  • rfc-854,“Telnet协议规范”,1983年5月。

  • rfc-855,“Telnet选项规范”,1983年5月。

  • rfc-856,“Telnet二进制传输”,1983年5月。

  • rfc-857,“Telnet回显选项”,1983年5月。

  • rfc-858,“Telnet抑制前进选项”,1983年5月。

  • rfc-859,“Telnet状态选项”,1983年5月。

  • rfc-860,“Telnet定时标记选项”,1983年5月。

  • rfc-885,“Telnet记录结束选项”,1983年12月。

  • rfc-1073,“Telnet窗口大小选项”,1988年10月。

  • rfc-1079,“Telnet终端速度选项”,1988年12月。

  • rfc-1091,“Telnet终端类型选项”,1989年2月。

  • rfc-1096,“Telnet X显示位置选项”,1989年3月。

  • rfc-1123,“对互联网主机的需求”,1989年10月。

  • rfc-1184,“Telnet Linemode选项(扩展选项)”,1990年10月。

  • rfc-1372,“Telnet远程流控制选项”,1992年10月。

  • rfc-1408,“Telnet环境选项”,1993年1月。

  • rfc-1571,“Telnet环境选项互操作性问题”,1994年1月。

  • rfc-1572,“Telnet环境选项”,1994年1月。

  • rfc-2066,“Telnet字符集选项”,1997年1月。

进一步阅读

更多文档可在 https://telnetlib3.readthedocs.org/ 获取

项目详情


下载文件

下载适合您平台的应用程序文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

telnetlib3-2.0.4.tar.gz (72.4 kB 查看哈希值)

上传时间 源代码

构建分发

telnetlib3-2.0.4-py2.py3-none-any.whl (93.3 kB 查看哈希值)

上传于 Python 2 Python 3

由以下支持