-
-
Save vardrop/b543e2bb85b0c24c66765cb142729f20 to your computer and use it in GitHub Desktop.
A simple IRC client written in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# [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 '.encode() | |
pong = 'PONG '.encode() | |
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').encode()) | |
client.send(('USER ' + lines[2] + ' 0 * :' + lines[3] + '\r\n').encode()) | |
# 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