好的,所以它一直在循环,我不知道如何在Python3.4上停止它
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dat = ""
try:
s.connect(("67.220.79.37", 6667))
while True:
s.send(bytes("NICK Conscience\r\n", "UTF-8"))
s.send(bytes("USER Conscience chat.frostwire.com ident :realname\r\n", "UTF-8"))
s.send(bytes("PASS ********\r\n", "UTF-8"))
dat = dat + s.recv(1024).decode("UTF-8")
s1 = str.split(dat, "\n")
for line in s1:
line = str.rstrip(line)
line = str.split(line)
print(line)
if (len(line) == 0):
continue
if(line[0] == "PING"):
s.send(bytes("PONG %s\r\n" % line[1], "UTF-8"))
s.send(bytes("JOIN #nerdrage\r\n", "UTF-8"))
except Exception as e:
print(e)
答案 0 :(得分:1)
我意识到没有回答你的问题。我建议不手动执行此操作,但要使用专为此类设计的框架。请参阅上面的评论。
这是一个使用circuits
的非常简单的IRC Bot私密消息时,它也会响应一个非常简单的命令。 hello
。其他任何内容都会产生Unknown Command
响应。
更新:如果您仍然坚持学习所有这些工作原理并手工编写一个简单的irc机器人,我建议您阅读:http://hawkee.com/snippet/2497/(或其他类似的文章网络)并从中学习。
为方便起见,粘贴在此处的示例:
import socket
network = 'irc.snm.co.nz'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'NICK botty\r\n' )
irc.send ( 'USER botty botty botty :Python IRC\r\n' )
irc.send ( 'JOIN #paul\r\n' )
irc.send ( 'PRIVMSG #Paul :Hello World.\r\n' )
while True:
data = irc.recv ( 4096 )
if data.find ( 'PING' ) != -1:
irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
if data.find ( '!botty quit' ) != -1:
irc.send ( 'PRIVMSG #paul :Fine, if you don't want me\r\n' )
irc.send ( 'QUIT\r\n' )
if data.find ( 'hi botty' ) != -1:
irc.send ( 'PRIVMSG #paul :I already said hi...\r\n' )
if data.find ( 'hello botty' ) != -1:
irc.send ( 'PRIVMSG #paul :I already said hi...\r\n' )
if data.find ( 'KICK' ) != -1:
irc.send ( 'JOIN #paul\r\n' )
if data.find ( 'cheese' ) != -1:
irc.send ( 'PRIVMSG #paul :WHERE!!!!!!\r\n' )
if data.find ( 'slaps botty' ) != -1:
irc.send ( 'PRIVMSG #paul :This is the Trout Protection Agency. Please put the Trout Down and walk away with your hands in the air.\r\n' )
print data
警告:正如已经注意到的那样(请参阅我之前的评论),手工完成此操作非常复杂且难以正确完成。像读取任意数量的字节这样的事情,盲目搜索这些字节是最容易出错的,最坏的情况是不可靠的。 请使用框架!。