简体   繁体   English

Python IRC机器人,不会停止循环

[英]Python IRC bot, wont stop the loop

Okay so it just keeps looping through this and I don't know how to stop it on Python3.4 好的,所以它一直在循环遍历,我不知道如何在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)

I realize this does not answer your question. 我知道这不能回答您的问题。 My recommendation is not to do this by hand but to utlize a framework designed for this kind of thing. 我的建议不是手工完成,而是建立一个为这种事情设计的框架。 See my comments above. 请参阅我上面的评论。

Here is a very trivial IRC Bot using circuits 这是一个使用电路的非常琐碎的IRC Bot

It will also responds to a very simple command when privately messaged. 当收到私人消息时,它也会响应一个非常简单的命令。 hello . hello Anything else will result in a Unknown Command response. 其他任何情况都会导致“ Unknown Command响应。

simpleircbot.py simpleircbot.py

Update: If you still insist on learning how all this works and writing a trivial irc bot by hand I recommend you read: http://hawkee.com/snippet/2497/ ( or other similar articles ont he web ) and learn from this. 更新:如果您仍然坚持学习所有方法并手工编写一个琐碎的irc机器人,我建议您阅读: http : //hawkee.com/snippet/2497/或网站上的其他类似文章 )并从中学习。

Example pasted here for your convenience: 为方便起见,此处粘贴了示例:

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

WARNING: As duly noted ( See my previous comments ) doing this by hand is complicated and difficulty to get right. 警告:正如适当指出的( 请参阅我以前的评论 ),手动执行此操作很复杂且很难正确进行。 Doing things like reading an arbitrary number of bytes, blinding searching through those bytes is error prone at best and at worst unreliable. 进行诸如读取任意数量的字节,盲目搜索这些字节之类的事情充其量是容易出错的,最糟糕的是不可靠的。 Please use a framework! 请使用框架! .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM