Created
October 30, 2016 23:33
-
-
Save hackers-terabit/2055018b60a3d7a845d34a27ebfb1a26 to your computer and use it in GitHub Desktop.
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
import sys,socket, ssl | |
import time,os,getopt | |
from threading import Thread,Event | |
from Queue import Queue | |
class SimpleClient: | |
def __init__(self): | |
self.server="chat.freenode.net" #default server | |
self.port=6697 #default port | |
self.nick="AshKetchum_" | |
def connect_tls(self): | |
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) | |
context.verify_mode = ssl.CERT_REQUIRED | |
context.check_hostname = True | |
context.load_default_certs() | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
ssl_sock = context.wrap_socket(s, server_hostname=self.server) | |
ssl_sock.connect((self.server, self.port)) | |
return ssl_sock | |
def usage(self): | |
print "Usage: python2 ",os.path.basename(__file__)," [-vh] -s server -p port",(''' | |
-s Server name (e.g.: chat.freenode.net) | |
-p Port number (e.g.: 6697) | |
-n Nick name | |
-v Print this applications current version string | |
-h Display this usage instruction.''') | |
sys.exit(1) | |
def handle_args(self,args): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "s:p:vh") | |
except getopt.GetoptError as err: | |
print (str(err)) | |
self.usage() | |
for opt, arg in opts: | |
if opt == '-s': | |
self.server=arg | |
if opt == '-p': | |
self.port=int(arg,10) | |
if opt == '-n': | |
self.nick=arg | |
if opt == '-v': | |
print "Version: 0.1a" | |
sys.exit(0) | |
if opt == '-h': | |
self.usage() | |
class IO: | |
def __init__(self,ssl_sock): | |
self.threads=[] | |
self.ssock=ssl_sock | |
self.in_queue=Queue() | |
self.out_queue=Queue() | |
self.in_event=Event() | |
self.out_event=Event() | |
def start_threads(self): | |
in_thread = Thread(target=self.run_input) | |
in_thread.daemon=True | |
self.threads.append(in_thread) | |
in_thread.start() | |
out_thread = Thread(target=self.run_output) | |
out_thread.daemon=True | |
self.threads.append(out_thread) | |
out_thread.start() | |
def run_input(self): | |
try: | |
while True: | |
time.sleep(0.5) | |
msg = self.ssock.recv(512) | |
if len(msg) > 0: | |
self.in_queue.put(msg) | |
self.in_event.set() | |
except Exception as e: | |
print "Input exception:",e | |
def run_output(self): | |
try: | |
while True: | |
self.out_event.wait() | |
msg = self.out_queue.get().strip() | |
if len(msg) > 0: | |
self.ssock.sendall(msg+"\n") | |
time.sleep(2) # without this we get k-lined | |
except Exception as e: | |
print "Output exception:",e | |
def send(self,msg): | |
self.out_event.clear() | |
self.out_queue.put(msg) | |
self.out_event.set() | |
def recv(self): | |
self.in_event.wait() | |
msg = self.in_queue.get() | |
self.in_event.clear() | |
return msg | |
def raw_print(io): #this will ideally be replaced by something that handles server responses | |
while True: | |
msg=io.recv() | |
if not msg is None and len(msg)>0: | |
print msg | |
def input_send(io): | |
while True: | |
io.send(raw_input(">")) | |
def main(): | |
client=SimpleClient() | |
client.handle_args(sys.argv) | |
io=IO(client.connect_tls()) | |
io.start_threads() | |
io.send("NICK "+client.nick) | |
io.send("USER "+client.nick+" "+client.nick+" "+client.server+" :"+client.nick) | |
time.sleep(1) | |
raw_thread = Thread(target=raw_print,args=(io,)) | |
raw_thread.daemon=True | |
raw_thread.start() | |
input_thread = Thread(target=input_send,args=(io,)) | |
input_thread.daemon=True | |
input_thread.start() | |
while True: | |
1==1 | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment