【问题标题】:Can not connect to IRC Hackerzvoice channel by python?无法通过 python 连接到 IRC Hackerzvoice 频道?
【发布时间】:2017-05-30 01:58:50
【问题描述】:

我正在尝试编写一个小程序,它可以连接到 irc 通道然后发送和接收消息。我成功连接到 irc 服务器,但是我无法加入任何频道。我尝试了命令JOIN #channel,但我一无所获,服务器没有响应。 这是我的代码:

try:
  con = socket.socket()
  con = socket.create_connection((client.svhost, client.svport))
  print(('CONNECTING TO.. %s\r\n' % client.svhost).encode())
  con.send(('NICK %s \r\n' % client.user).encode())
  con.send(('USER %s %s root-me :%s \r\n' %(client.user, client.svhost, client.user)).encode())

  con.send(('JOIN %s' %client.channel).encode())


  print('Joined to.. %s\r\n' %client.channel)

  while True:
    respond = ""
    respond = con.recv(1024).strip().decode()

    print(respond)

    #PING - PONG
    pp = respond.split()
    if pp[0] == 'PING':
      print(pp)
      con.send(('PONG %s' %pp[1]).encode())
      print(('PONG %s' %pp[1]).encode())
      print('Responded')

      con.send(('JOIN %s' %client.channel).encode()) 

except Exception as e:
  print(e)
  sys.exit(1)

感谢阅读!

【问题讨论】:

    标签: python-3.x sockets irc


    【解决方案1】:

    您在发送 Pong 响应 时忘记添加 NewLine 字符,并且在服务器识别之前添加了 JOIN 请求。

    更改:

    con.send(('PONG %s' %pp[1]).encode()) # Change this line
    con.send(('PONG %s\r\n' %pp[1]).encode()) # To This line
    

    另外你还需要在注册前移除Join(实际标识为服务器中的用户)。

    con.send(('JOIN %s' %client.channel).encode())
    print('Joined to.. %s\r\n' %client.channel)
    

    【讨论】:

    • 您好,我尝试在注册前删除 JOIN,但得到了相同的结果。我无法连接到我需要访问的频道。
    • 连接服务器成功了吗?如果是这样,这只是一个 JOIN 问题,而不是以前的注册问题。
    • 连接服务器成功,但无法连接频道
    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多