def delete_database(database_name): mysql_obj = mysql_method.MysqlObject(operate_user_name, operate_password) mysql_obj.connect() cur = mysql_obj.show('DATABASES', like=database_name) for result in cur: mysql_obj_ = mysql_method.MysqlObject(operate_user_name, operate_password) mysql_obj_.connect() mysql_obj_.delete_database(result[0])
def do_post_share2(handler, image_save_dir): user_id = handler.headers['user_id'] # Parse the form data posted form = cgi.FieldStorage(fp=handler.rfile, headers=handler.headers, environ={ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': handler.headers['Content-Type'], }) image_name, image = _extract_image(form) to_user_id_list = [int(i) for i in form['to_user_id'].value.split(',')] image_ext = image_name.split('.')[1] num_save_dir = len(os.listdir(image_save_dir)) image_id = num_save_dir image_save_path = image_save_dir + '/' + str(image_id) + '.' + image_ext image.save(image_save_path) mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() table_name = 'images' table = '(image_id, image_name, user_id, upload_time)' values = '({}, \'{}\', {}, now())'.format(image_id, image_name, user_id) mysql_obj.insert_into(table_name, table, values) mysql_obj.disconnect() mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() to_user_id_str = '' for to_user_id in to_user_id_list: to_user_id_str += str(to_user_id) + ',' table_name = 'share_images' table = '(image_id, from_user_id, to_user_id, share_start_time, is_shared)' values = '({}, \'{}\', \'{}\', now(), \'false\')'.format( image_id, user_id, to_user_id) mysql_obj.insert_into(table_name, table, values) mysql_obj.disconnect() to_user_id_str = to_user_id_str[:-1] # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:server saved and shared your image,') handler.wfile.write('image_id:{},'.format(image_id)) handler.wfile.write('image_name:{}'.format(image_name)) handler.wfile.write('to_user_id:{}'.format(to_user_id_str)) return
def delete_mysql_user(user_name): mysql_obj = mysql_method.MysqlObject(operate_user_name, operate_password) mysql_obj.connect() cur, count = mysql_obj.select('host, user', 'mysql.user', 'user=\'{}\''.format(user_name)) for i in range(count): result = cur.next() mysql_obj_ = mysql_method.MysqlObject(operate_user_name, operate_password) mysql_obj_.connect() mysql_obj_.delete_user(result[1], result[0])
def do_get_login(handler): user_id = handler.headers['user_id'] mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() column = 'user_name, event' table = 'users' where = 'user_id=\'{}\''.format(user_id) cursor, count = mysql_obj.select(column, table, where) user_name = '' if count == 0: body_msg = 'message:not found your account' else: result = cursor.next() user_name = result[0] event_name = result[1] if event_name == None: body_msg = 'message:found your account,user_id:{},user_name:{}'.format( user_id, user_name) else: body_msg = 'message:found your account,user_id:{},user_name:{},event_name:{}'.format( user_id, user_name, event_name) # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write(body_msg) return
def create_tables(user_name, password, database_name): mysql_obj = mysql_method.MysqlObject(user_name, password) mysql_obj.connect() users_table = 'users' users_elements = ['user_id', 'user_name', 'event'] users_types = ['int', 'varchar(100)', 'varchar(100)'] mysql_obj.create_table(database_name, users_table, users_elements, users_types) images_table = 'images' images_elements = ['image_id', 'image_name', 'user_id', 'upload_time'] images_types = ['int', 'varchar(100)', 'int', 'datetime'] mysql_obj.create_table(database_name, images_table, images_elements, images_types) share_images_table = 'share_images' share_images_elements = [ 'image_id', 'from_user_id', 'to_user_id', 'share_start_time', 'is_shared' ] share_images_types = ['int', 'int', 'int', 'datetime', 'varchar(10)'] mysql_obj.create_table(database_name, share_images_table, share_images_elements, share_images_types) train_images_table = 'train_images' train_images_elements = [ 'image_id', 'image_name', 'user_id', 'upload_time' ] train_images_types = ['int', 'varchar(100)', 'int', 'datetime'] mysql_obj.create_table(database_name, train_images_table, train_images_elements, train_images_types)
def do_get_user_register(handler): param = handler.path.split('?', 1) if len(param) < 2: return user_name = param[1] mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() user_id = mysql_obj.table_row_count('users') table_name = 'users' table = '(user_id, user_name, event)' values = '(\'{}\', \'{}\', NULL)'.format(user_id, user_name) mysql_obj.insert_into(table_name, table, values) # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:server registered your account successfully,') handler.wfile.write('user_id:{},'.format(user_id)) handler.wfile.write('user_name:{}'.format(user_name)) return
def do_get_event_info(handler): param = handler.path.split('?', 1) if len(param) < 2: return event_name = param[1] user_id = handler.headers['user_id'] mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() column = 'user_id, user_name' table = 'users' where = 'event = \'{}\''.format(event_name) cursor, count = mysql_obj.select(column, table, where) user_id_str = '' user_name_str = '' for i in range(count): result = cursor.next() user_id_str += str(result[0]) + '_' user_name_str += str(result[1]) + '_' user_id_str = user_id_str.rsplit('_', 1)[0] user_name_str = user_name_str.rsplit('_', 1)[0] # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:user_id_list registered to event is shown,') handler.wfile.write('user_id_list:{},'.format(user_id_str)) handler.wfile.write('user_name_list:{}'.format(user_name_str)) return
def do_get_event_register(handler): param = handler.path.split('?', 1) if len(param) < 2: return event_name = param[1] user_id = handler.headers['user_id'] mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() table = 'users' column = 'event' value = '\'{}\''.format(event_name) where = 'user_id = {}'.format(user_id) mysql_obj.update(table, column, value, where) # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:server registered event to your account,') handler.wfile.write('user_id:{},'.format(user_id)) handler.wfile.write('event_name:{}'.format(event_name)) return
def do_post_share(handler): # Parse the form data posted form = cgi.FieldStorage(fp=handler.rfile, headers=handler.headers, environ={ 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': handler.headers['Content-Type'], }) image_id = form['image_id'].value to_user_id_list = [int(i) for i in form['to_user_id'].value.split(',')] from_user_id = handler.headers['user_id'] mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() for to_user_id in to_user_id_list: table_name = 'share_images' table = '(image_id, from_user_id, to_user_id, share_start_time, is_shared)' values = '({}, \'{}\', \'{}\', now(), \'false\')'.format( image_id, from_user_id, to_user_id) mysql_obj.insert_into(table_name, table, values) mysql_obj.disconnect() # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:server shared image,') handler.wfile.write('image_id:{}'.format(image_id)) return
def update_is_shared_false(to_user_id): mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() table = 'share_images' column = 'is_shared' value = '\'false\'' where = 'to_user_id = {}'.format(to_user_id) mysql_obj.update(table, column, value, where)
def import_image_from_id(image_id, image_save_dir): mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() column = 'image_name' table = 'images' where = 'image_id = {}'.format(image_id) cursor, _ = mysql_obj.select(column, table, where) image_name = cursor.next()[0] ext = image_name.split('.')[1] f_image = open(image_save_dir + '/' + str(image_id) + '.' + ext, 'rb') return image_name, f_image
def select_download_image(to_user_id): mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() column = 'image_id' table = 'share_images' where = '(to_user_id = {} AND is_shared = \'false\')'.format(to_user_id) cursor, count = mysql_obj.select(column, table, where) if count <= 0: return None, count image_id = cursor.next()[0] #mysql_obj.disconnect() return image_id, count
def do_post_upload(handler): image_save_dir = '../images' user_id = handler.headers['user_id'] # Parse the form data posted form = cgi.FieldStorage( fp=handler.rfile, headers=handler.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':handler.headers['Content-Type'], }) image_name, image = _extract_image(form) #face_rec_list = _extract_rec_list(form) #face_name_list = extract_name_list(image, face_rec_list) image_ext = image_name.split('.')[1] num_save_dir = len(os.listdir(image_save_dir)) image_id = num_save_dir + 1 image_save_path = image_save_dir + '/' + str(image_id) + '.' + image_ext image.save(image_save_path) mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() table_name = 'images' table = '(image_id, image_name, user_id, upload_time)' values = '({}, \'{}\', {}, now())'.format(image_id, image_name, user_id) mysql_obj.insert_into(table_name, table, values) mysql_obj.disconnect() # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:server got and saved your image,') handler.wfile.write('image_id:{},'.format(image_id)) handler.wfile.write('image_name:{}'.format(image_name)) return
def do_get_event_exit(handler): user_id = handler.headers['user_id'] mysql_obj = mysql_method.MysqlObject(database='photo_share_app') mysql_obj.connect() table = 'users' column = 'event' value = 'NULL' where = 'user_id = {}'.format(user_id) mysql_obj.update(table, column, value, where) # Response -- header -- handler.send_response(200) handler.send_header("Content-type", "text/plain") handler.end_headers() # Response -- body -- handler.wfile.write('message:server registered your event exit,') handler.wfile.write('user_id:{}'.format(user_id)) return
def create_database(user_name, password, database_name): mysql_obj = mysql_method.MysqlObject(user_name, password) mysql_obj.connect() mysql_obj.create_database(database_name)
def create_mysql_user(user_name, password): mysql_obj = mysql_method.MysqlObject(operate_user_name, operate_password) mysql_obj.connect() mysql_obj.create_user(user_name, password, grant_all=True)