def file_send(self, ip, port, checkcode, toname, filepath): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, int(port))) f = open(filepath, "rb") sheet = SHEET() sheet.fill("P2PFILESEND", self.Username, filepath) sheet.headAdd("CheckCode", checkcode) s = f.read(800) while s: sheet.fill_body(s) sock.send(sheet.toStr()) print "[330] file send", sheet.toStr() s = f.read(800) time.sleep(0.1) f.close() sock.close() print "[386]---------------------" self.fileLock.acquire() input = open("./buffer/"+self.Username+"/"+toname, "a") input.write("["+self.Username+"] " + " <finish sending file: " + filepath + ">\n") input.close() self.fileLock.release()
def login_handle(sheet_str, clientsock, clientaddr): global AllSocket sheet = sheet_toForm(sheet_str) # LOGIN ---------------------------------------- if sheet.Cmd=="LOGIN": print "# recv LOGIN request." if sheet.Arg!="" and is_off(sheet.Arg): NameList[sheet.Arg] = [True, clientaddr[0], sheet.Headline["Port"], time.time()] print "# ONLINE. [" + sheet.Arg + "]" status = SHEET() status.fill("STATUS", "1") else: status = SHEET() status.fill("STATUS", "0") if not is_off(sheet.Arg): status.fill_body("@@@ username is online!\n") status.headAdd("Content-Length", str(len(status.Body))) if status.Arg == "1": clientsock.send(status.toStr()) #send status print "# send STATUS reply. [status=" + status.Arg + "]" time.sleep(0.01) getlist_handle(clientsock) update_handle(sheet.Arg, "1") AllSocket[sheet.Arg] = clientsock return True, sheet.Arg else: clientsock.send(status.toStr()) print "# send STATUS reply. [status=" + status.Arg + "]" print "# someone LOGIN FAIL(1)." return False, ""
def getlist_handle(clientsock): print "# recv GETLIST request." list_body = listname() list = SHEET() list.fill("LIST") list.fill_body(list_body) list.headAdd("Content-Length", str(len(list.Body))) clientsock.send(list.toStr()) print "# send LIST reply." print "# current LIST: " print_NameList()
def beat_handle(self, clientsock): global Status, Username cur_time = time.time() while self.Status["login"]: if time.time() - cur_time > self.BEAT_TIME: print "[173] beat_handle" cur_time = time.time() sheet = SHEET() sheet.fill("BEAT", self.Username) clientsock.send(sheet.toStr()) else: time.sleep(1)
def beat_handle(clientsock): global Status, Username cur_time = time.time() while Status["login"]: if time.time() - cur_time > BEAT_TIME: cur_time = time.time() sheet = SHEET() sheet.fill("BEAT", Username) clientsock.send(sheet.toStr()) else: time.sleep(1)
def message_handle(clientsock, username, content_str): global NameList, AllSocket sheet = SHEET() sheet.fill("CSMESSAGE", username) sheet.fill_body(content_str) for name in AllSocket: if NameList.has_key(name) and NameList[name][0] == True: s = AllSocket[name] s.send(sheet.toStr()) print "# send CSMESSAGE to everyone."
def update_handle(username, status): global NameList, AllSocket sheet = SHEET() sheet.fill("UPDATE", username, status) for name in AllSocket: if NameList.has_key(name) and NameList[name][0] == True: s = AllSocket[name] try: s.send(sheet.toStr()) except: continue print "# send UPDATE to everyone. [" + username +"="+ status + "]"
def file_send(ip, port, checkcode, filepath): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, int(port))) f = open(filepath, "rb") sheet = SHEET() sheet.fill("P2PFILESEND", Username, filepath) sheet.headAdd("CheckCode", checkcode) s = f.read(800) while s: sheet.fill_body(s) sock.send(sheet.toStr()) #print "[330] file send", sheet.toStr() s = f.read(800) time.sleep(0.1) f.close() sock.close()