- Python-网络编程
- Python-网络简介
- Python-网络环境
- Python - 互联网协议
- Python-IP 地址
- Python-DNS 查找
- Python-路由
- Python - HTTP 请求
- Python-HTTP 响应
- Python - HTTP 标头
- Python - 自定义 HTTP 请求
- Python - 请求状态代码
- Python-HTTP 身份验证
- Python - HTTP 数据下载
- Python - 连接重用
- Python - 网络接口
- Python-套接字编程
- Python-HTTP 客户端
- Python-HTTP 服务器
- Python - 构建 URL
- Python - Web表单提交
- Python - 数据库和 SQL
- Python-远程登录
- Python - 电子邮件消息
- Python-SMTP
- Python-POP3
- Python-IMAP
- Python-SSH
- Python-FTP
- Python-SFTP
- Python - Web 服务器
- Python-上传数据
- Python-代理服务器
- Python - 目录列表
- Python-远程过程调用
- Python - RPC JSON 服务器
- Python - 谷歌地图
- Python - RSS 源
Python-远程登录
Telnet 是一种网络协议,允许一台计算机上的用户登录到也属于同一网络的另一台计算机。telnet 命令与主机名一起使用,然后输入用户凭据。成功登录后,远程用户可以像系统的常规用户一样访问应用程序和数据。当然,某些权限可以由设置和维护系统的系统管理员控制。
在Python中,telnet是由telnetlib模块实现的,该模块具有Telnet类,该类具有建立连接所需的方法。在下面的示例中,我们还使用 getpass 模块来处理密码提示,作为登录过程的一部分。我们还假设连接是与 UNIX 主机建立的。下面解释了程序中使用的 telnetlib.Telnet 类的各种方法。
Telnet.read_until - 读取直到遇到预期的给定字符串或直到超时秒数过去。
Telnet.write - 将字符串写入套接字,将所有 IAC 字符加倍。如果连接被阻止,这可能会阻止。如果连接关闭,可能会引发 socket.error。
Telnet.read_all() - 读取所有数据直到EOF;阻塞直到连接关闭。
例子
import getpass import telnetlib HOST = "http://localhost:8000/" user = raw_input("Enter your remote account: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until("login: ") tn.write(user + "\n") if password: tn.read_until("Password: ") tn.write(password + "\n") tn.write("ls\n") tn.write("exit\n") print tn.read_all()
当我们运行上面的程序时,我们得到以下输出 -
- lrwxrwxrwx 1 0 0 1 Nov 13 2012 ftp -> . - lrwxrwxrwx 1 0 0 3 Nov 13 2012 mirror -> pub - drwxr-xr-x 23 0 0 4096 Nov 27 2017 pub - drwxr-sr-x 88 0 450 4096 May 04 19:30 site - drwxr-xr-x 9 0 0 4096 Jan 23 2014 vol
请注意,此输出特定于远程计算机,其详细信息在程序运行时提交。