def chat_msg_handle(con, content_json): obj = {} res_obj = {} f_con = None global user_dict global mq global outputs uid = content_json["uid"] fid = content_json["fid"] msg = content_json["msg"] obj["send_id"] = uid obj["rcv_id"] = fid obj["msg"] = msg data = json.dumps(obj, indent=4) if user_dict.has_key(fid): f_con = user_dict[fid] print "found friend id in user dict", fid if f_con is None: res_obj["status"] = "fail" res_obj["reson"] = "friend is not online" else: msg_packet = wrap_packet(f_con, 0, 20, data, len(data)) mq[f_con].put(msg_packet) if f_con not in outputs: outputs.append(f_con) res_obj["status"] = "success" res_data = json.dumps(res_obj, indent=4) res_packet = wrap_packet(con, 0, 21, res_data, len(res_data)) return res_packet
def friend_list_request_handle(con, content_json): obj = {} friend_list = [] num = 0 user = User(content_json["uid"]) friend_list = user.get_friend_list() print "friend_list=", friend_list obj["list"] = [] for i, friend_id in enumerate(friend_list): friend_obj = {} num += 1 friend = User(friend_id) friend_obj["loginid"] = i friend_obj["nickname"] = friend.nickname friend_obj["address"] = friend.address friend_obj["phone_num"] = friend.phone_num friend_obj["sex"] = friend.sex friend_obj["signature"] = friend.signature friend_obj["mail"] = friend.mail friend_obj["online"] = friend.online friend_obj["age"] = friend.age obj["list"].append(friend_obj) obj["friend_num"] = num data = json.dumps(obj, indent=4) print data # send_packet(con,0,4,data,len(data)) packet = wrap_packet(con, 0, 4, data, len(data)) return packet
def user_image_request_handle(con, content_json): obj = {} res_obj = {} uid = content_json["uid"] fd = open("images/%d.png" % uid, "rb") img_data = fd.read() encoded_img_data = base64.b64encode(img_data) fd.close() res_obj["img"] = encoded_img_data res_data = json.dumps(res_obj, indent=4) res_packet = wrap_packet(con, 0, 8, res_data, len(res_data)) return res_packet
def upload_user_image_handle(con, content_json): obj = {} res_obj = {} uid = content_json["uid"] img_data = content_json["img"] fd = open("images/%d.png" % uid, "wb") decoded_img_data = base64.b64decode(img_data) fd.write(decoded_img_data) fd.close() res_obj["status"] = "success" res_data = json.dumps(res_obj, indent=4) res_packet = wrap_packet(con, 0, 6, res_data, len(res_data)) return res_packet
def login_request_handle(con, content_json): global user_dict obj = {} user = User.login(content_json["username"], content_json["password"]) if user == None: obj["login_status"] = "fail" else: obj["login_status"] = "success" user_dict[user.loginid] = con print obj print "user dict=", user_dict data = json.dumps(obj) packet = wrap_packet(con, 0, 2, data, len(data)) return packet
def user_info_request_handle(con, content_json): obj = {} res_obj = {} uid = content_json["uid"] user = User(uid) res_obj["loginid"] = user.loginid res_obj["nickname"] = user.nickname res_obj["address"] = user.address res_obj["phone_num"] = user.phone_num res_obj["sex"] = user.sex res_obj["signature"] = user.signature res_obj["mail"] = user.mail res_obj["online"] = user.online res_obj["age"] = user.age res_data = json.dumps(res_obj, indent=4) res_packet = wrap_packet(con, 0, 10, res_data, len(res_data)) return res_packet