跳转到主要内容

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

项目描述

Latest Version Downloads

简介

asynctelnet 是一个用于 python 的异步 Telnet 客户端和服务器库。该项目需要 python 3.6 及以上版本,使用 anyio 模块。

asynctelnet 是对 telnetlib3 模块的 heavily modified anyio-ization。

快速示例

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

import anyio, asynctelnet

async def shell(tcp):
    async with asynctelnet.TelnetServer(tcp) as stream:
        # this will fail if no charset has been negotiated
        await stream.send('\r\nWould you like to play a game? ')
        inp = await reader.receive(1)
        if inp:
            await stream.echo(inp)
            await stream.send('\r\nThey say the only way to win '
                              'is to not play at all.\r\n')

async def main():
    listener = await anyio.create_tcp_listener(local_port=56023)
    await listener.serve(shell)
anyio.run(main)

编写一个与该服务器玩战棋游戏的 Telnet 客户端

import anyio, asynctelnet

async def shell(tcp):
    async with asynctelnet.TelnetClient(tcp, client=True) as stream:
        while True:
            # read stream until '?' mark is found
            outp = await stream.receive(1024)
            if not outp:
                # End of File
                break
            elif '?' in outp:
                # reply all questions with 'y'.
                await stream.send('y')

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

    # EOF
    print()

async def main():
    async with await connect_tcp('localhost', 56023) as client:
        await shell(client)
anyio.run(main)

命令行

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

asynctelnet-client

小型终端 Telnet 客户端。一些示例目标和方法

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

asynctelnet-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 Logout Option,”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://asynctelnet.readthedocs.org/找到

项目详情


下载文件

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

源代码发行版

asynctelnet-0.2.5.tar.gz (100.2 kB 查看哈希值

上传时间 源代码

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面