From 1b3c78dd2ae0c123e487a3fcfb6c37e537ef3959 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Thu, 28 Jan 2016 02:54:43 -0500 Subject: [PATCH] Connect to an IRC server Added a basic SSL connection; simplified Flea.py file --- Flea.py | 3 +-- core/main.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Flea.py b/Flea.py index a7302ed..46dd8f4 100644 --- a/Flea.py +++ b/Flea.py @@ -17,8 +17,7 @@ import traceback try: - from core.main import * - main() + import core.main except: traceback.print_exc() input() diff --git a/core/main.py b/core/main.py index b106a55..95b55f5 100644 --- a/core/main.py +++ b/core/main.py @@ -15,8 +15,52 @@ # along with this program. If not, see from core.config import * +import socket +import ssl def main(): config = cfgParser("settings.conf") - print "config = "+repr(config) + + # Create socket object + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(10) + + # Wrap socket object to create SSLSocket object + sock = ssl.wrap_socket(sock) + + # Connect to IRC server + sock.connect((config["host"], config["port"])) + print "Connecting to "+config["host"]+':'+str(config["port"]) + + # Display SSL information to the user + ssl_info = sock.cipher() + if ssl_info != None: + print "[SSL] Cipher: "+ssl_info[0] + print "[SSL] Version: "+ssl_info[1] + print "[SSL] Bits: "+str(ssl_info[2]) + + while True: + # Buffer to store data from server + data = '' + + while True: + # Receive data from connection + tmpdata = sock.recv(4096) + data = data + tmpdata + + if len(tmpdata) < 4096: + break + + # If no incoming data exists then connection has closed + if len(tmpdata) == 0: + input("Connection closed.") + sys.exit() + + # Split data to easily deal with it + data = tmpdata.split("\r\n") + for line in data: + if len(line) > 0: + print line + +main()