Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import *
- import time, thread, random
- HostIP = "localhost"
- Port = 50003
- soc = socket(AF_INET, SOCK_STREAM)
- soc.bind((HostIP, Port))
- soc.listen(100)
- Clients = {}
- Unverified = {}
- Outtube = []
- Worktube = []
- maxsockets = 9
- ####### Tube Format: [[NAME, JOB, DATA], [NAME, JOB, DATA], [NAME, JOB, DATA]...]
- ####### Clients/Unverified Format: [name] = [[values stored in db], [temporary values], [socketlist], socketcounter]
- def writedatabase(): ########## Database Format: name, password, value, value, value, value... (newline)
- try:
- f = open("database", "r+")
- except:
- f = open("database", "w")
- f.close()
- print "database created"
- f = open("database", "r+")
- for element in Clients:
- savestring = ""
- for item in Clients[element][0]:
- savestring = "%s%s " % (savestring, item)
- savestring = "%s\n" % savestring
- f.write(savestring)
- f.close()
- print "database saved"
- def readdatabase(): ####### will create new database if none found or error during processing
- try:
- f = open("database", "r+")
- for line in f:
- g = line.split()
- name = g[0]
- Clients[name] = [[name], ["offline", "dummy"], ["socketlist"], 0]
- g.pop(0)
- for element in g:
- Clients[name][0].append(element)
- while len(Clients[name][0]) < 10:
- Clients[name][0].append("X")
- print "finished reading database"
- except:
- print "couldnt read database"
- writedatabase()
- def acceptconthread(soc): ## im always waiting for connections on Port
- while 1:
- try:
- conn, addr = soc.accept()
- thread.start_new_thread(connectconthread, (conn, addr))
- except Exception, e:
- print "WAAAAAH ERROR:", e
- thread.exit()
- def connectconthread(conn, addr): ## i open a bunch of sockets, each on another port
- Purt = 50004
- socketlist = []
- try:
- for i in range(0, maxsockets):
- Purt = 50004+i
- suc = socket(AF_INET, SOCK_STREAM)
- suc.bind((HostIP, Purt))
- suc.listen(100)
- cunn, uddr = suc.accept()
- socketlist.append([cunn, uddr])
- name = cunn.recv(1024)
- thread.start_new_thread(listentosocketthread, (cunn, name))
- print name, "has connected"
- Unverified[name] = [[name, ""], ["offline", "dummy"], socketlist, 0] ### and add connecting to "Unverified"
- except Exception, e:
- print "OMG CONNECT ERROR:", e
- socketlist = ["owned!"]
- name = "owned!s"
- thread.exit()
- def listentosocketthread(cunn, name): ##### i receive name's new data on his last socket
- while 1:
- try:
- data = cunn.recv(1024)
- if len(data) > 5:
- datalist = data.split()
- job = datalist[0]
- datalist.pop(0)
- Worktube.append([name, job, datalist])
- except Exception, e:
- print e
- Clients[name][1][0] = "offline"
- break
- thread.exit
- def getitoutthread(todolist): #### i send stuff through the proper socket
- try:
- sendtext = " ".join(todolist[2])
- sendstring = "%s %s" % (todolist[1], sendtext)
- Clients[todolist[0]][2][Clients[todolist[0]][3]][0].send(sendstring)
- Clients[todolist[0]][3] = Clients[todolist[0]][3] + 1
- if Clients[todolist[0]][3] > maxsockets-1:
- Clients[todolist[0]][3] = 0
- except Exception, e:
- print e, "damn", len(Outtube)
- Clients[todolist[0]][1][0] = "offline"
- thread.exit()
- def sendcannonthread(Port): ######## i make a thread for each Outtube job
- while 1:
- if len(Outtube)>0:
- thread.start_new_thread(getitoutthread, (Outtube[0], ))
- time.sleep(0.001)
- Outtube.pop(0)
- def Workthrough(): ###### im working through all the stuff thats communicated
- if len(Worktube)>0:
- name = Worktube[0][0]
- job = Worktube[0][1]
- data = Worktube[0][2]
- if job == "login":
- poolean = name in Clients
- if poolean == True:
- if data[1] == Clients[data[0]][0][1] and data[0] == Clients[data[0]][0][0]: ###pw true
- Clients[name][2] = Unverified[name][2] ### give sockets
- Clients[name][1][0] = "online"
- print name, "has logged in"
- if data[1] != Clients[data[0]][0][1] or data[0] != Clients[data[0]][0][0]:
- print "wrong password by", name
- if poolean != True:
- Clients[name] = [[name, data[1]],["online", "dummy"], Unverified[name][2], 0]
- print name, "has logged in"
- Worktube.pop(0)
- readdatabase()
- thread.start_new_thread(acceptconthread, (soc, ))
- thread.start_new_thread(sendcannonthread, (Port, ))
- def spamtest(port):
- while 1:
- try:
- for client in Clients:
- if Clients[client][1][0] == "online":
- Outtube.append([Clients[client][0][0], "spam", ["moagrihoaweg oaeghoawegih oseg"]])
- except Exception, e:
- print "boah ey", e
- time.sleep(1)
- thread.start_new_thread(spamtest, (Port, ))
- while 1:
- Workthrough()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement