Python 的 telnetlib 模块出现问题
我有一个基本的聊天服务器,可以轻松地通过 telnet 连接到它。我只需输入主机和端口,无需任何进一步的身份验证,就可以开始输入服务器可以解释的命令。为了模拟用户流量,我想创建一个脚本来打开 telnet,连接到服务器,并立即开始向服务器发送命令。我最初尝试使用批处理脚本来完成此操作,但遇到太多障碍后,我决定使用 Python 的 telnetlib 模块。这就是脚本现在的样子
import telnetlib
myTel = telnetlib.Telnet('XXX.XXX.XXX.XXX', XXXX)
myTel.write('login')
myTel.write('move room')
myTel.write('say message')
myTel.write('exit')
myTel.write('logout')
它真的很简单,并且脚本运行没有错误。但是,如果我已经在另一个 telnet 会话上手动登录到服务器,则我无法看到脚本进入主房间或发送聊天消息,这让我相信出了问题。如果我手动启动两个 telnet 会话,我可以轻松地在会话之间发送和接收消息,因此我认为手动 telnet 会话应该从脚本启动的会话接收消息。
有什么想法吗?
I have a basic chat server which I can easily connect to with telnet. I simply enter the host and port and, without any further authentication, can begin entering commands that the server can interpret. In an effort to simulate user traffic, I would like to create a script that will open telnet, connect to the server, and immediately begin sending commands to the server. I initially attempted to do this with batch scripts, but after encountering too many road blocks I have decided to use Python's telnetlib module. This is what the script looks like now
import telnetlib
myTel = telnetlib.Telnet('XXX.XXX.XXX.XXX', XXXX)
myTel.write('login')
myTel.write('move room')
myTel.write('say message')
myTel.write('exit')
myTel.write('logout')
Its really that simple and the script runs with no errors. However, if I've already manually logged into the server on another telnet session, I fail to see the script entering the main room or sending a chat message which leads me to believe that something is going wrong. If I manually start two telnet sessions I can easily send and receive messages between the sessions, so I would think that a manual telnet session should be receiving the messages from the session started by the script.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许添加换行符会有所帮助?
我还建议使用一些网络嗅探工具(即 Wireshark)来验证您是否确实发送了您想要的内容。
Perhaps adding a newline would help?
I'd also recommend using some network sniffing tool (i.e. Wireshark) to verify that you're really sending what you intended.
我将使用 read_until 来检查服务器是否已准备好接收命令,这里是使用 telnet 与 cisco 交换机对话的示例:
这里,我在发送密码之前等待密码提示。
I would use read_until to check that the server is ready to receive the command here is a sample using telnet to talk to a cisco switch:
Here I wait for a password prompt before sending the password.
在代码中,您写入服务器,但为了接收数据,您最终必须使用 读取库的方法。要查看具有“阅读线程”的客户端实现示例,请参阅此博客文章:http:// hahong.org/blog/posts/261。
fn。还可以尝试:
.. 使用换行符。
In your code, you write to the server, but in order to receive data, you'll eventually have to one of the read methods of the library. To see an example implementation of a client with a 'reading thread', see this blog post: http://hahong.org/blog/posts/261.
fn. Also try:
.. with newlines.