socket.error: [Errno 10054]

2019-06-21 23:30发布

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)

3条回答
SAY GOODBYE
2楼-- · 2019-06-22 00:07

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.

查看更多
你好瞎i
3楼-- · 2019-06-22 00:18

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)

查看更多
再贱就再见
4楼-- · 2019-06-22 00:22

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?

查看更多
登录 后发表回答