开发者

socket.error: [Errno 10054]

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)


You need to check the IRC protocol a little it more; your IRC session is not considered conncted (by the server) until certain actions have been completed which the server will inform your client about using IRC protocol codes. And if the server or network is busy when you are connecting it will take longer for these actions to complete.

In this case attempting to join a channel before the server has given you the MOTD (message of the day) would cause a disconnection by the server. The end of MOTD protocol code is 376 and indicates that the IRC connection sequence is over, and you can proceed with your IRC session eg: enter commands (like join).

I would suggest entering a RECV loop and monitoring data received from the server for the IRC code 376 before you attempt to join a channel, in Perl this would look somthing like this:

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

Pretty poor but you get the idea right? (please note its only necessary to check for 376 once, once seen you are connected and you only need to maintain the connection by responding to the server 'PING's)


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?


The remote host is issuing a TCP reset (RST), after accepting the connection. This can happen for a lot of reasons, including:

  • Firewall rules
  • Remote application error
  • Remote application simply closes the connection
  • etc.

As John Weldon said, try telnetting to the same machine and port and entering the commands manually.

Also, a good wire sniffer (Ethereal, WireShark, etc.) is highly useful for diagnosing this kind of problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜