Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created March 18, 2016 03:10
Show Gist options
  • Save primaryobjects/bef532fbbe032745ac7f to your computer and use it in GitHub Desktop.
Save primaryobjects/bef532fbbe032745ac7f to your computer and use it in GitHub Desktop.
A simple IRC client written in Python.
#
# [2016-03-14] Challenge #258 [Easy] IRC: Making a Connection
# https://www.reddit.com/r/dailyprogrammer/comments/4ad23z/20160314_challenge_258_easy_irc_making_a/
#
import socket
input = """chat.freenode.net:6667
dude1267
dude1267
John Doe"""
# Parse input.
ping = 'PING '
pong = 'PONG '
lines = input.split('\n')
host = lines[0].split(':')
# Connect.
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host[0], int(host[1])))
# Handshake.
client.send('NICK ' + lines[1] + '\r\n')
client.send('USER ' + lines[2] + ' 0 * :' + lines[3] + '\r\n')
# Output and ping/pong.
while True:
data = client.recv(1024)
print(data)
if data.startswith(ping):
resp = data.strip(ping);
client.send(pong + resp)
print(pong + resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment