V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
CrystalMoling
V2EX  ›  Python

Python socket 实现的 Telnet 服务器数据回显格式问题

  •  
  •   CrystalMoling ·
    Crystal-Moling · 2022-10-22 12:56:02 +08:00 · 1239 次点击
    这是一个创建于 551 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我尝试使用 Python 中的 socket 创建 Telnet 服务器,并使用 Telnet 进行远程控制。但是当我使用 send() 在 Telnet 客户端回显数据时,换行的数据会从上一行的末尾开始显示,就像

    第一行
         第二行
              第三行
    

    有没有办法能够让后面的行从一行的开头开始显示?

    目前的代码:

    class Telnet(threading.Thread):
        def __init__(self, conn, add):
            threading.Thread.__init__(self)
            self.inputstr = ''
            self.connection = conn
            self.address = add
        def run(self):
            ii = 0
            self.connection.send(b'Hello controller')
            self.connection.send(b'\n>')
            while True:
                buf = self.connection.recv(1024)
                if buf.rfind(b'\n') > -1:
                    print("**-" + self.inputstr)
                    return_arr = self.inputstr.split(' ')
                    match return_arr[0]:
                        case 'list':
                            func_return = list_connections()
                            for key in func_return.keys():
                                self.connection.send(key.encode())
                    self.inputstr = ''
                    self.connection.send(b'\n>')
                else:
                    self.inputstr += buf.decode()
                if ii == 0:
                    self.connection.send(buf)
                ii += 1
                continue
    
    1 条回复    2022-10-22 13:05:32 +08:00
    ysc3839
        1
    ysc3839  
       2022-10-22 13:05:32 +08:00 via Android
    telnet 协议的换行是 CRLF ,需要回车符(CR)把输出位置移动到行首
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   980 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:03 · PVG 05:03 · LAX 14:03 · JFK 17:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.