Connect to IRC server with python

553 Views Asked by At

i am trying to connect to anonops irc server and subsequently #anonops channel using python. What i have done so far :

import sys
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = 'irc.anonops.com' #irc server
PORT = 6697 #port
NICK = 'testingbot'



print('soc created |', s)
remote_ip = socket.gethostbyname(HOST)
print('ip of irc server is:', remote_ip)


s.connect((HOST, PORT))

print('connected to: ', HOST, PORT)
nick_cr = ('NICK ' + NICK + '\r\n').encode()
s.send(nick_cr)
s.send('JOIN #anonops \r\n'.encode()) #chanel

#
s.send(bytes("PRIVMSG " + '#anonops' +  'hi'+ "\n", "UTF-8"))

I think this connect succesfuly to the irc server but i can't seem to connect to the channel. I have an open irc client (Hexchat) in my pc and i don't see the message:
testingbot has joined nor do i see the hi message.
Any idea about what am i doing wrong?

1

There are 1 best solutions below

1
feature09 On

Use remote_ip instead of HOST.

s.connect((remote_ip, PORT))