5
import socket, sys

if len(sys.argv) !=3 :
print "Usage: ./supabot.py <host> <port>"
sys.exit(1)

irc = sys.argv[1]
port = int(sys.argv[2])
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((irc, port))
sck.send('NICK supaBOT\r\n')
sck.send('USER supaBOT supaBOT supaBOT :supaBOT Script\r\n')
sck.send('JOIN #darkunderground' + '\r\n')
data = ''
while True:
      data = sck.recv(1024)
      if data.find('PING') != -1:
         sck.send('PONG ' + data.split() [1] + '\r\n')
         print data
      elif data.find('!info') != -1:
          sck.send('PRIVMSG #darkunderground supaBOT v1.0 by sourD' + '\r\n')


print sck.recv(1024)

when I run this code I get this error..

socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host

it says that the error is in line 16, in data = sck.recv(1024)

4

3 回答 3

4

您需要再检查一下 IRC 协议;在某些操作完成之前,您的 IRC 会话不会被视为已连接(由服务器),服务器将通知您的客户端有关使用 IRC 协议代码的信息。如果在您连接时服务器或网络很忙,这些操作将需要更长的时间才能完成。

在这种情况下,在服务器为您提供 MOTD(当日消息)之前尝试加入频道会导致服务器断开连接。MOTD 协议代码的结尾是 376,表示 IRC 连接序列结束,您可以继续您的 IRC 会话,例如:输入命令(如加入)。

我建议在您尝试加入频道之前进入 RECV 循环并监控从服务器接收到的 IRC 代码 376 的数据,在 Perl 中,这看起来像这样:

 sub chan_join{
  while(my $input = <SOCK>){
    if($input =~ /376/){
      my $talk = "JOIN $channel";
      &send_data($talk);
      &monitor;
    }
    else { print "$input";  }
  }
}

很穷,但你明白吗?(请注意只需要检查一次 376,一旦看到您已连接,您只需要通过响应服务器“PING”来维持连接)

于 2010-06-20T16:34:59.377 回答
1

远程主机在接受连接后发出 TCP 重置 ( RST)。发生这种情况的原因有很多,包括:

  • 防火墙规则
  • 远程应用程序错误
  • 远程应用程序只是关闭连接
  • 等等

正如 John Weldon 所说,尝试远程登录到同一台机器和端口并手动输入命令。

此外,一个好的电线嗅探器(Ethereal、WireShark 等)对于诊断此类问题非常有用。

于 2010-06-20T12:59:34.377 回答
1

That probably means that you're not supplying the expected handshake or protocol exchange for the server, and it is closing the connection.

What happens if you telnet to the same machine and port and type in the same text?

于 2010-06-17T04:49:56.570 回答