Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 KB | None | 0 0
  1. from socket import *
  2. import time, thread, random
  3. HostIP = "localhost"
  4. Port = 50003
  5. soc = socket(AF_INET, SOCK_STREAM)
  6. soc.bind((HostIP, Port))
  7. soc.listen(100)
  8. Clients = {}
  9. Unverified = {}
  10. Outtube = []
  11. Worktube = []
  12. maxsockets = 9
  13.  
  14. ####### Tube Format:  [[NAME, JOB, DATA], [NAME, JOB, DATA], [NAME, JOB, DATA]...]
  15. ####### Clients/Unverified Format: [name] = [[values stored in db], [temporary values], [socketlist], socketcounter]
  16.  
  17. def writedatabase():    ########## Database Format: name, password, value, value, value, value... (newline)
  18.     try:
  19.         f = open("database", "r+")
  20.     except:
  21.         f = open("database", "w")
  22.         f.close()
  23.         print "database created"
  24.         f = open("database", "r+")
  25.     for element in Clients:
  26.         savestring = ""
  27.         for item in Clients[element][0]:
  28.             savestring = "%s%s " % (savestring, item)
  29.         savestring = "%s\n" % savestring
  30.         f.write(savestring)
  31.     f.close()
  32.     print "database saved"
  33.  
  34. def readdatabase():    ####### will create new database if none found or error during processing
  35.     try:
  36.         f = open("database", "r+")
  37.         for line in f:
  38.             g = line.split()
  39.             name = g[0]
  40.             Clients[name] = [[name], ["offline", "dummy"], ["socketlist"], 0]
  41.             g.pop(0)
  42.             for element in g:
  43.                 Clients[name][0].append(element)
  44.             while len(Clients[name][0]) < 10:
  45.                 Clients[name][0].append("X")
  46.         print "finished reading database"
  47.     except:
  48.         print "couldnt read database"
  49.         writedatabase()
  50.  
  51. def acceptconthread(soc):  ## im always waiting for connections on Port
  52.     while 1:
  53.         try:
  54.             conn, addr = soc.accept()
  55.             thread.start_new_thread(connectconthread, (conn, addr))
  56.         except Exception, e:
  57.             print "WAAAAAH ERROR:", e
  58.     thread.exit()
  59.  
  60. def connectconthread(conn, addr):   ## i open a bunch of sockets, each on another port
  61.     Purt = 50004
  62.     socketlist = []
  63.     try:
  64.         for i in range(0, maxsockets):
  65.             Purt = 50004+i
  66.             suc = socket(AF_INET, SOCK_STREAM)
  67.             suc.bind((HostIP, Purt))
  68.             suc.listen(100)
  69.             cunn, uddr = suc.accept()
  70.             socketlist.append([cunn, uddr])
  71.         name = cunn.recv(1024)
  72.         thread.start_new_thread(listentosocketthread, (cunn, name))
  73.         print name, "has connected"
  74.         Unverified[name] = [[name, ""], ["offline", "dummy"], socketlist, 0]   ### and add connecting to "Unverified"
  75.     except Exception, e:
  76.         print "OMG CONNECT ERROR:", e
  77.         socketlist = ["owned!"]
  78.         name = "owned!s"
  79.     thread.exit()
  80.  
  81. def listentosocketthread(cunn, name):   ##### i receive name's new data on his last socket
  82.     while 1:
  83.         try:
  84.             data = cunn.recv(1024)
  85.             if len(data) > 5:
  86.                 datalist = data.split()
  87.                 job = datalist[0]
  88.                 datalist.pop(0)
  89.                 Worktube.append([name, job, datalist])
  90.         except Exception, e:
  91.             print e
  92.             Clients[name][1][0] = "offline"
  93.             break
  94.     thread.exit
  95.  
  96. def getitoutthread(todolist):   #### i send stuff through the proper socket
  97.     try:
  98.         sendtext = " ".join(todolist[2])
  99.         sendstring = "%s %s" % (todolist[1], sendtext)
  100.         Clients[todolist[0]][2][Clients[todolist[0]][3]][0].send(sendstring)
  101.         Clients[todolist[0]][3] = Clients[todolist[0]][3] + 1
  102.         if Clients[todolist[0]][3] > maxsockets-1:
  103.             Clients[todolist[0]][3] = 0
  104.     except Exception, e:
  105.         print e, "damn", len(Outtube)
  106.         Clients[todolist[0]][1][0] = "offline"
  107.     thread.exit()
  108.  
  109. def sendcannonthread(Port):   ######## i make a thread for each Outtube job
  110.     while 1:
  111.         if len(Outtube)>0:
  112.             thread.start_new_thread(getitoutthread, (Outtube[0], ))
  113.             time.sleep(0.001)
  114.             Outtube.pop(0)
  115.  
  116. def Workthrough():          ###### im working through all the stuff thats communicated
  117.     if len(Worktube)>0:
  118.         name = Worktube[0][0]
  119.         job = Worktube[0][1]
  120.         data = Worktube[0][2]
  121.         if job == "login":
  122.             poolean = name in Clients
  123.             if poolean == True:
  124.                 if data[1] == Clients[data[0]][0][1] and data[0] == Clients[data[0]][0][0]: ###pw true
  125.                     Clients[name][2] = Unverified[name][2]   ### give sockets
  126.                     Clients[name][1][0] = "online"
  127.                     print name, "has logged in"
  128.                 if data[1] != Clients[data[0]][0][1] or data[0] != Clients[data[0]][0][0]:
  129.                     print "wrong password by", name
  130.             if poolean != True:
  131.                 Clients[name] = [[name, data[1]],["online", "dummy"], Unverified[name][2], 0]
  132.                 print name, "has logged in"
  133.         Worktube.pop(0)
  134.  
  135.  
  136. readdatabase()    
  137. thread.start_new_thread(acceptconthread, (soc, ))
  138. thread.start_new_thread(sendcannonthread, (Port, ))
  139.  
  140. def spamtest(port):
  141.     while 1:
  142.         try:
  143.             for client in Clients:
  144.                 if Clients[client][1][0] == "online":
  145.                     Outtube.append([Clients[client][0][0], "spam", ["moagrihoaweg oaeghoawegih oseg"]])
  146.         except Exception, e:
  147.             print "boah ey", e
  148.         time.sleep(1)
  149.  
  150. thread.start_new_thread(spamtest, (Port, ))
  151. while 1:
  152.     Workthrough()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement