本文整理汇总了Python中connection.Connection.read_line方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.read_line方法的具体用法?Python Connection.read_line怎么用?Python Connection.read_line使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connection.Connection
的用法示例。
在下文中一共展示了Connection.read_line方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read_line [as 别名]
class Boteco:
def __init__(self, host, port, nick):
self.state = BotState()
self.state._host = host
self.state._port = port
self.state._nick = nick
self._command = BotCommands()
self._is_connected = False
def connect(self):
if self._is_connected:
return
self._sock = Connection()
self._sock.connect(self.state._host, self.state._port)
while 1:
data = self._read_line()
if data == "":
break
print(data)
self._send_command("NICK " + self.state._nick)
self._send_command("USER " + self.state._nick + " myhost myhost :" + self.state._nick)
# Read the whole MOTD and whatever else there is and simply ignore
while 1:
data = self._read_line()
if data == "":
return
print(data)
def get_socket(self):
return self._sock
def join_channel(self, channel):
self._command.join(channel)
def is_connected(self):
return self._sock.is_connected
def parse(self, data):
ircmsg = IRCMessage(data)
if ircmsg.msg_type == "":
return
if ircmsg.msg_type == "PING":
self._send_command("PONG " + ircmsg.from_host)
elif ircmsg.msg_type == "ERROR":
self._sock.disconnect()
elif ircmsg.msg_type == "JOIN":
self._handle_join(ircmsg)
elif ircmsg.msg_type == "PRIVMSG":
self._handle_privmsg(ircmsg)
def _read_line(self):
return self._sock.read_line()
def _send_command(self, command):
self._command.send_command(command)
def _send_message(self, to, msg):
self._command.send_message(to, msg)
def _handle_privmsg(self, ircmsg):
if ircmsg.cmd == "":
return
self._command.execute(ircmsg)
# TODO: Might want to rewrite this...
def _handle_join(self, ircmsg):
# Is it the bot joining?
if ircmsg.from_nick == self.state._nick:
people_on_channel = []
joined_channel = ircmsg.to
while 1:
data = self._read_line()
print(data)
tokens = [el.strip() for el in data.split(" ")]
if tokens[1] == "353":
for i in tokens[5:]:
people_on_channel.append(i.strip(":"))
self.state.joined_channel(ircmsg.to, i.strip(":"))
print("Joined channel %s\n" % ircmsg.to)
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import read_line [as 别名]
#.........这里部分代码省略.........
if len(line) > 0:
self.send_command(line)
self._modules.append(mod)
except:
msg = "Module %s does not exist or has errors (%s)" % (ircmsg.cmd, sys.exc_info())
self.send_message(ircmsg.reply_to, msg)
else:
line = getattr(sys.modules[mod_name], "run")(self.botstate, ircmsg, self._conn)
if len(line) > 0:
self.send_command(line)
def send_command(self, line):
self._conn.send(line)
def send_message(self, to, msg):
line = "PRIVMSG %s :%s" % (to, msg)
self.send_command(line)
def join(self, channel):
ircmsg = IRCMessage("")
ircmsg.msg_type = "JOIN"
ircmsg.cmd = "join"
ircmsg.args = channel
self._join(ircmsg)
def _read_line(self):
return self._conn.read_line()
def _list_modules(self, ircmsg):
line = "Loaded modules: "
if len(self._modules) > 0:
loaded_modules = [m.__name__[m.__name__.find(".")+1:] for m in self._modules]
line += ", ".join(loaded_modules)
else:
line = "No modules have been loaded yet"
self.send_message(ircmsg.reply_to, line)
def _quit(self, ircmsg):
if not ircmsg.args:
line = "QUIT :%s" % " ".join(ircmsg.args)
else:
line = "QUIT :Don't kill meeeeeeeeeeeeeeeeee"
self.send_command(line)
self._conn.disconnect()
def _reload_modules(self, ircmsg):
'''
Reloads specified module. If no module is specified, reload them all
If module specified has not been loaded yet, load it
'''
reloaded_modules = []
module_found = False