Advertisement
Guest User

Untitled

a guest
Jun 24th, 2011
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. from socket import *
  2. import time, thread, random
  3. Clients, Clientsonline, Port, Port2, dummy, HostIP = {}, [], 50005, 50003, "", ""
  4. Outtube, Intube, Worktube = [], [], []
  5.  
  6. ###################################### DATABASE ################################
  7.  
  8. def writedatabase():
  9.     try:
  10.         f = open("database", "r+")
  11.     except:
  12.         f = open("database", "w")
  13.         f.close()
  14.         print "database created"
  15.         f = open("database", "r+")
  16.     for element in Clients:
  17.         savestring = ""
  18.         for item in Clients[element][0]:
  19.             savestring = "%s%s " % (savestring, item)
  20.         savestring = "%s\n" % savestring
  21.         f.write(savestring)
  22.     f.close()
  23.     print "database saved"
  24.  
  25. def readdatabase():
  26.     try:
  27.         f = open("database", "r+")
  28.         for line in f:
  29.             g = line.split()
  30.             name = g[0]
  31.             Clients[name] = [[name], ["conn", "addr"], ["conn2", "addr2"]]
  32.             g.pop(0)
  33.             for element in g:
  34.                 Clients[name][0].append(element)
  35.     except:
  36.         print "couldnt read database"
  37.  
  38. def saveloop(dummy):
  39.     while 1:
  40.         time.sleep(120)
  41.         writedatabase()
  42.  
  43. ####################################### COMMUNICATION ##########################
  44.  
  45. def acceptconthread(dummy):
  46.     soc = socket(AF_INET, SOCK_STREAM)
  47.     soc.bind((HostIP, Port))
  48.     soc.listen(100)
  49.     while 1:
  50.         conn, addr = soc.accept()
  51.         thread.start_new_thread(passwordthread, (conn, addr))
  52.  
  53. def passwordthread(conn, addr):
  54.     while 1:
  55.         name = conn.recv(1024)
  56.         name2 = name.split()
  57.         name, password = name2[0], name2[1]
  58.         aaa = name in Clients
  59.         if aaa == True:
  60.             if password == Clients[name][0][1]:
  61.                 Clients[name] = [[name, password], [conn, addr], ["conn2", "addr2"]]
  62.                 print "%s connected with password %s" %(name, password)
  63.                 conn.send("welcome")
  64.                 Clientsonline.append(name)
  65.                 Worktube.append([name, "upd"])
  66.                 break
  67.             if password != Clients[name][0][1]:
  68.                 conn.send("wrong password")
  69.         if aaa == False:
  70.             Clients[name] = [[name, password], [conn, addr], ["conn2", "addr2"]]
  71.             print "%s connected with password %s" %(name, password)
  72.             conn.send("welcome")
  73.             Clientsonline.append(name)
  74.             Worktube.append([name, "upd"])
  75.             break
  76.     soc2 = socket(AF_INET, SOCK_STREAM)
  77.     soc2.bind((HostIP, Port2))
  78.     soc2.listen(100)
  79.     conn2, addr2 = soc2.accept()
  80.     Clients[name][2]=[conn2, addr2]
  81.     thread.start_new_thread(sendloop, (name, conn, addr))
  82.     thread.start_new_thread(recvloop, (name, conn2, addr2))
  83.  
  84. def sendloop(name, conn, addr):
  85.     try:
  86.         while 1:
  87.             if len(Outtube) > 0:
  88.                 if Outtube[0][0] == name:
  89.                     sendstring = "%s %s" % (Outtube[0][1], Outtube[0][2])
  90.                     conn.send(sendstring)
  91.                     Outtube.pop(0)
  92.     except:
  93.         bbb = Clientsonline.remove(name)
  94.  
  95. def recvloop(name, conn2, addr2):
  96.     try:
  97.         while 1:
  98.             data = conn2.recv(1024)
  99.             data2 = data.split()
  100.             jobname = data2[0]
  101.             data2.pop(0)
  102.             Intube.append([name, jobname, data2])
  103.     except:
  104.         print name, "has disconnected"
  105.  
  106. def workthrough():
  107.     while 1:
  108.         if len(Worktube) > 0:
  109.             if Worktube[0][1] == "upd":
  110.                 newlist = Clientsonline[:]
  111.                 newlist2 = " ".join(newlist)
  112.                 Outtube.append([Worktube[0][0], "upd", newlist2])
  113.                 Worktube.pop(0)
  114.         if len(Worktube) > 0:
  115.             if Worktube[0][1] == "say":
  116.                 Worktube[0][2] = "%s %s" % (Worktube[0][0], Worktube[0][2])
  117.                 for client in Clientsonline:
  118.                     Outtube.append([client, "say", Worktube[0][2]])
  119.                 Worktube.pop(0)
  120.         if len(Intube) > 0:
  121.             if Intube[0][1] == "say":
  122.                 naostring = " ".join(Intube[0][2])
  123.                 Worktube.append([Intube[0][0], "say", naostring])
  124.                 Intube.pop(0)
  125.  
  126. #################################### ACTION ####################################
  127.  
  128. readdatabase()
  129. thread.start_new_thread(acceptconthread, (dummy, ))
  130. thread.start_new_thread(saveloop, (dummy, ))
  131. ####thread.start_new_thread(workthrough, (dummy, ))
  132. while 1:
  133.     workthrough()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement