服务器代码优化:更正MD5部分
以下是经过修改的服务器代码,其中包含更正的MD5部分:
import hashlib
from socket import *
import json
import os
from os.path import join, getsize
import argparse
from threading import Thread
import struct
import time
import logging
from logging.handlers import TimedRotatingFileHandler
import base64
import uuid
import math
import shutil
MAX_PACKET_SIZE = 20480
# Const Value
OP_SAVE, OP_DELETE, OP_GET, OP_UPLOAD, OP_DOWNLOAD, OP_BYE, OP_LOGIN, OP_ERROR = 'SAVE', 'DELETE', 'GET', 'UPLOAD', 'DOWNLOAD', 'BYE', 'LOGIN', 'ERROR'
TYPE_FILE, TYPE_DATA, TYPE_AUTH, DIR_EARTH = 'FILE', 'DATA', 'AUTH', 'EARTH'
FIELD_OPERATION, FIELD_DIRECTION, FIELD_TYPE, FIELD_USERNAME, FIELD_PASSWORD, FIELD_TOKEN = 'operation', 'direction', 'type', 'username', 'password', 'token'
FIELD_KEY, FIELD_SIZE, FIELD_TOTAL_BLOCK, FIELD_MD5, FIELD_BLOCK_SIZE = 'key', 'size', 'total_block', 'md5', 'block_size'
FIELD_STATUS, FIELD_STATUS_MSG, FIELD_BLOCK_INDEX = 'status', 'status_msg', 'block_index'
DIR_REQUEST, DIR_RESPONSE = 'REQUEST', 'RESPONSE'
logger = logging.getLogger('')
def get_file_md5(filename):
'''
Get MD5 value for big file
:param filename:
:return:
'''
m = hashlib.md5()
with open(filename, 'rb') as fid:
while True:
d = fid.read(2048)
if not d:
break
m.update(d)
return m.hexdigest()
def data_process(username, request_operation, json_data, connection_socket):
'''
Data Process
:param username:
:param request_operation:
:param json_data:
:param connection_socket:
:return: None
'''
global logger
if request_operation == OP_GET:
if FIELD_KEY not in json_data.keys():
logger.info(f'<-- Get data without key.')
logger.error(f'<-- Field 'key' is missing for DATA GET.')
connection_socket.send(
make_response_packet(OP_GET, 410, TYPE_DATA, f'Field 'key' is missing for DATA GET.', {}))
return
logger.info(f'--> Get data {json_data[FIELD_KEY]}')
if os.path.exists(join('data', username, json_data[FIELD_KEY])) is False:
logger.error(f'<-- The key {json_data[FIELD_KEY]} is not existing.')
connection_socket.send(
make_response_packet(OP_GET, 404, TYPE_DATA, f'The key {json_data[FIELD_KEY]} is not existing.', {}))
return
try:
with open(join('data', username, json_data[FIELD_KEY]), 'r') as fid:
data_from_file = json.load(fid)
logger.info(f'<-- Find the data and return to client.')
connection_socket.send(
make_response_packet(OP_GET, 200, TYPE_DATA, f'OK', data_from_file))
except Exception as ex:
logger.error(f'{str(ex)}@{ex.__traceback__.tb_lineno}')
if request_operation == OP_SAVE:
key = str(uuid.uuid4())
if FIELD_KEY in json_data.keys():
key = json_data[FIELD_KEY]
logger.info(f'--> Save data with key '{key}'')
if os.path.exists(join('data', username, key)) is True:
logger.error(f'<-- This key '{key}' is existing.')
connection_socket.send(make_response_packet(OP_SAVE, 402, TYPE_DATA, f'This key '{key}' is existing.', {}))
return
try:
with open(join('data', username, key), 'w') as fid:
json.dump(json_data, fid)
logger.info(f'<-- Data is saved with key '{key}'')
connection_socket.send(
make_response_packet(OP_SAVE, 200, TYPE_DATA, f'Data is saved with key '{key}'', {FIELD_KEY: key}))
except Exception as ex:
logger.error(f'{str(ex)}@{ex.__traceback__.tb_lineno}')
if request_operation == OP_DELETE:
if FIELD_KEY not in json_data.keys():
logger.info(f'--> Delete data without any key.')
logger.error(f'<-- Field 'key' is missing for DATA delete.')
connection_socket.send(
make_response_packet(OP_DELETE, 410, TYPE_DATA, f'Field 'key' is missing for DATA delete.', {}))
return
if os.path.exists(join('data', username, json_data[FIELD_KEY])) is False:
logger.error(f'<-- The 'key' {json_data[FIELD_KEY]} is not existing.')
connection_socket.send(
make_response_packet(OP_DELETE, 404, TYPE_DATA, f'The 'key' {json_data[FIELD_KEY]} is not existing.',
{}))
return
try:
os.remove(join('data', username, json_data[FIELD_KEY]))
logger.error(f'<-- The 'key' {json_data[FIELD_KEY]} is deleted.')
connection_socket.send(
make_response_packet(OP_DELETE, 200, TYPE_DATA, f'The 'key' {json_data[FIELD_KEY]} is deleted.',
{FIELD_KEY: json_data[FIELD_KEY]}))
原文地址: https://www.cveoy.top/t/topic/bfV5 著作权归作者所有。请勿转载和采集!