Want to use python for mirc connection but it give error

179 Views Asked by At

hi i am trying to connect mirc using below script

import socket
import sys

server = "irc.all4masti.com"       #settings
channel = "#all4masti"
botnick = "botname"

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
print ("connecting to:"+server)
irc.connect((server, 6698))                                                         #connects to the server
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is a fun bot!\n") #user authentication
irc.send("NICK "+ botnick +"\n")                            #sets nick
irc.send("PRIVMSG nickserv :iNOOPE\r\n")    #auth
irc.send("JOIN "+ channel +"\n")        #join the chan

while 1:    #puts it in a loop
   text=irc.recv(2040)  #receive the text
   print (text)   #print text to console

   if text.find('PING') != -1:                          #check if 'PING' is found
      irc.send('PONG ' + text.split() [1] + '\r\n') #returnes 'PONG' back to the server (prevents pinging out!)


but every time it give me error of below

==== RESTART: C:/Users/MaK/OneDrive - Microsoft 365/Desktop/ircflooddeerr.py ===
connecting to:irc.all4masti.com
Traceback (most recent call last):
  File "C:/Users/MaK/OneDrive - Microsoft 365/Desktop/ircflooddeerr.py", line 11, in <module>
    irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is a fun bot!\n") #user authentication
TypeError: a bytes-like object is required, not 'str'

While the below is my server link

https://all4masti.com/mibbit/webchat.html?server=irc.all4masti.com:+6698&channel=%23All4Masti&nick=

any suggestion where i am wrong

1

There are 1 best solutions below

0
toyota Supra On

You need a bytes() to send to server.

irc_send(bytes("USER " + bot_nick + " " + bot_nick +" " + bot_nick + " :python\n", "UTF-8"))
irc_send(bytes("NICK " + bot_nick + "\n", "UTF-8"))
irc_send(bytes("JOIN " + channel + "\n", "UTF-8"))

Better used format() string. This is how I do.

_nick = f"NICK {self.irc_nick} \r\n"
            self.irc_sock.send(bytes(_nick, "UTF-8"))
    
user = (f"USER {self.irc_nick} {self.irc_host} bla :{self.irc_nick}\r\n")
            self.irc_sock.send(bytes(_user, 'UTF-8'))