V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
1.14D
V2EX  ›  Python

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

  •  
  •   CrystalMoling ·
    Malpl3naInk · Oct 22, 2022 · 1965 views
    This topic created in 1285 days ago, the information mentioned may be changed or developed.

    我尝试使用 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 replies    2022-10-22 13:05:32 +08:00
    ysc3839
        1
    ysc3839  
       Oct 22, 2022 via Android
    telnet 协议的换行是 CRLF ,需要回车符(CR)把输出位置移动到行首
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   942 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:36 · PVG 05:36 · LAX 14:36 · JFK 17:36
    ♥ Do have faith in what you're doing.