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
CR7sun
V2EX  ›  Python

请问 Python 利用 telnetlib 登录被 ACL 拒绝该如何处理异常?

  •  
  •   CR7sun · 2020-12-24 13:42:02 +08:00 · 1280 次点击
    这是一个创建于 1220 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问用 python 的 telnetlib 进行 telnet 登录时,连接上去又被 ACL 拒了然后代码报错。请问该如何处理这个异常,结束这次登陆尝试?

     try:
        self.tn = telnetlib.Telnet(host_ip,port=23,timeout=2)
     except:
        logging.warning("failed to connect to host:%s" %host_ip)
        return False
     self.tn.read_until(b'Username:', timeout=3)
     self.tn.write(user_name.encode('ascii') + b'\n')
     self.tn.read_until(b'Password:', timeout=3)
     self.tn.write(pass_word.encode('ascii') + b'\n') 
    

    手动登录测试截图:

    https://imgchr.com/i/rgKksH

    代码报错截图:

    https://imgchr.com/i/rgKFQe

    1 条回复    2020-12-25 11:12:13 +08:00
    julyclyde
        1
    julyclyde  
       2020-12-25 11:12:13 +08:00
    这种都是先建立连接再断开连接的。错误信息写的很明白啊 EOFError 你试试 except 这个
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2806 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 12:09 · PVG 20:09 · LAX 05:09 · JFK 08:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.