import pickle
import os
from shutil import copyfile
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR

if not os.path.isfile("settings.py"):
    copyfile("settings.py.example", "settings.py")
from settings import HIVEPORT, AUTHORISEDBEARS
from myenc import AESCipher
from dbconnect import Insert

def DumpToFile(data):
    try:
        with file('temp.db') as f:
            stringfile = f.read()
        db = pickle.loads(stringfile)
    except:
        db = list()
    db.append(data)
    with open('temp.db', "w") as f:
        f.write(str(pickle.dumps(db)))


def main(args, update_event):
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serverSocket.bind(('', HIVEPORT))
    serverSocket.listen(1)
    if args.verbose:
        print "Awaiting for bears on port %s" % HIVEPORT
    while True:
        if update_event.is_set():
            break
        connectionSocket, addr = serverSocket.accept()
        try:
            message = connectionSocket.recv(30000)
            request = message.split(":")
            key = AUTHORISEDBEARS[request[0]]
            deciper = AESCipher(key)
            data = pickle.loads(deciper.decrypt(request[1]))
            if args.verbose:
                print data
            try:
                Insert(data)
            except:
                DumpToFile(data)
                if args.verbose:
                    print "Error writing data to clickhouse, writing to file"
            connectionSocket.send("200")
            connectionSocket.close()
        except:
            try:
                connectionSocket.send("CODE 300 FUCK YOU")
            finally:
                connectionSocket.close()
    serverSocket.close()

@@ -64,16 +64,17 @@
  import 
+(
 socket, 
@@ -119,16 +119,57 @@
 EUSEADDR
+,%0A                    error as sockerror)
 %0A%0Aif not
@@ -356,16 +356,17 @@
 Insert%0A%0A
+%0A
 def Dump
@@ -1614,24 +1614,71 @@
 ket.close()%0A
+        except sockerror:%0A            continue%0A
         exce
@@ -1773,15 +1773,14 @@
     
-finally
+except
 :%0A  
@@ -1796,37 +1796,22 @@
      con
-nectionSocket.close()
+tinue%0A
 %0A    ser

Add correct handle for socket exception