socket.error: [Errno 10054]

こ雲淡風輕ζ 提交于 2019-12-10 13:50:40

问题


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)


回答1:


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)




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/3058932/socket-error-errno-10054

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!