Sure. Code below makes connection and reads the chat. As far as I can tell, it appears to be responding to two PINGs from Twitch and then disconnects after 10 minutes or so.
import socket
import time
HOST = "irc.chat.twitch.tv"
PORT = 6667
NICK = "xxxxxxxxxxxx"
PASS = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
CHANNEL = "xxxxxxxxxxxxxxxx"
def send_message(msg):
s.send(bytes("PRIVMSG #" + CHANNEL + " :" + msg + "\r\n", "UTF-8"))
print("Connecting to host.")
s = socket.socket()
s.connect((HOST, PORT))
s.send(bytes("PASS " + PASS + "\r\n", "UTF-8"))
s.send(bytes("NICK " + NICK + "\r\n", "UTF-8"))
s.send(bytes("JOIN #" + CHANNEL + " \r\n", "UTF-8"))
print("Connected to host")
send_message("Connected to chat.")
while True:
for line in str(s.recv(1024)).split('\\r\\n'):
if "PING :tmi.twitch.tv" in line:
print("PONG :tmi.twitch.tv")
s.send(bytes("PONG :tmi.twitch.tv", "UTF-8"))
parts = line.split(':')
if len(parts) < 3:
continue
message = parts[2][:len(parts[2])]
usernamesplit = parts[1].split("!")
username = usernamesplit[0]
print(time.strftime("%H:%M:%S"), username + ": " + message)