python - 在 Windows 中使用 pexpect.popen_spawn.PopenSpawn() 运行 telnet 实例
问题描述
有没有人幸运地运行 pexpect.popen_spawn.PopenSpawn() 在 Windows 中运行 telnet?
请注意,我已经从 Windows 启用了“Telnet 客户端”功能,因为只需在命令行中键入“telnet”,我就可以从命令行很好地运行“telnet”。
import sys
import pexpect
from pexpect.popen_spawn import PopenSpawn
EXPECTED_PROMPT = "> "
def test():
telnet_port=4444
if sys.platform.startswith('win'):
telnet = pexpect.popen_spawn.PopenSpawn(
"C:\Windows\System32\telnet localhost {}".format(telnet_port),
timeout=120,
logfile=sys.stdout)
telnet.expect(EXPECTED_PROMPT)
return telnet
if __name__ == "__main__":
telnet = test()
当我尝试运行上面的代码时,出现以下错误:
Traceback (most recent call last):
File ".\test.py", line 26, in <module>
telnet = test()
File ".\test.py", line 20, in test
logfile=sys.stdout)
File "C:\Python27\lib\site-packages\pexpect\popen_spawn.py", line 53, in
__init__
self.proc = subprocess.Popen(cmd, **kwargs)
File "C:\Python27\lib\subprocess.py", line 394, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 644, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
解决方案
通过使用 plink 解决了这个问题:https ://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
代替
C:\Windows\System32\telnet localhost {}
我用了
plink.exe -telnet localhost -P {}
推荐阅读
- python - 如何用分组绘制图表?
- vba - Windows Media Player 时间位置
- html - 如何修复导致我的网站格式中断的 iframe 图像?
- bash - 需要在 bash 脚本中创建文件
- java - 无法掌握如何使用 System.arraycopy();
- django - AttributeError: 'NoneType' 对象没有属性 'split' - 在 ajax 调用中保存用户时出错
- javascript - 将 this.size 函数更改为单独的宽度和长度?
- sql - 添加结合 IN 运算符“操作数类型冲突:DATE 与 INT 不兼容”的 CASE 语句时出错
- node.js - 如何从数组中只获取某些值?
- python-3.x - 从中央目录运行用户定义的模块