socket.error:[Errno 10054] [英] socket.error: [Errno 10054]

查看:671
本文介绍了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]远程主机强行关闭了现有连接

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

它说错误在第16行,在data = sck.recv(1024)

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

推荐答案

您需要多检查一下IRC协议.直到完成某些动作(服务器将通知您的客户端有关使用IRC协议代码的操作)后,您的IRC会话才被视为已被服务器(服务器)接听.如果服务器或网络在连接时很忙,则完成这些操作将需要更长的时间.

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.

在这种情况下,如果在服务器未向您发送MOTD(每日消息)之前尝试加入某个频道,则会导致服务器断开连接. MOTD协议代码的末尾是376,表示IRC连接序列已结束,您可以继续进行IRC会话,例如:输入命令(如join).

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).

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

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";  }
  }
}

相当贫穷,但是您知道这个主意了吗? (请注意,只需要检查一次376,一旦发现您已连接,您只需通过响应服务器"PING"来保持连接即可)

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)

这篇关于socket.error:[Errno 10054]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆