我尝试将bbs与python的库连接起来" telnetlib",尝试让机器人回答
消息。当我回答消息时,机器人返回了多条消息。这些是我的
码
# -*- coding: cp950 -*-
import telnetlib,random
#f= open("ans.txt","r")
ans = [b"oao", b"xd"]
'''while True:
line = f.readline()
if line = "":
break
ans.append(line)
'''
tn = telnetlib.Telnet("ptt.cc")
tn.read_very_eager()
tn.write(b"*****\r\n") # this is where i enter my username
tn.read_very_eager()
tn.write(b"*****\r\n") # this is wher i enter my password
tn.read_very_eager()
tn.write(b"\r\n")
while True:
if tn.read_very_eager() != "" :
tn.write(b"")
tn.read_very_eager()
tn.write(b"su\r\n")
tn.read_very_eager()
tn.write(b"\r\n")
tn.read_very_eager()
tn.write(b"\r\n\r\n←")
tn.read_very_eager()
tn.read_very_eager()
for i in range(0,1000000):
x = 1
答案 0 :(得分:0)
首先,我绝对没有使用过telnet的经验。
查看Python documentation on telnetlib我可以看到您的代码与文档页面底部的示例之间存在一些差异。主要区别在于他们等待登录或提供密码的提示。即使您的read_very_eager
应做同样的事情,read_until
也会更清楚。它可以解决您的问题,或者给您一些提示。
尝试调整示例以满足您的需求。
import sys
import telnetlib
HOST = 'ptt.cc'
user = 'username'
password = 'pass123'
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
# Do anything you need to here.
# If your server will accept these, try them first to isolate the problem
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
正如MatthieuW所说,你可以和时间库一起睡觉。
from time import sleep
print 'Start'
sleep(1)
print 'One second later'