Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

flask - Python file upload changes checksum

I'm uploading files as so:

...
    for filename in os.listdir(directory):
        path = os.path.join(directory, filename)
        file = open(path, 'rb')

        print(f'file check SENDER sum of {filename}:')
        print(hashlib.md5(file.read()).hexdigest())

        files = {'file': file}
        response = requests.post(f'{url}/file', files=files)
        file.close()

        assert(response.status_code == 200)
        time.sleep(1)

outputs:

file check SENDER sum of sample.tar_4.gz:
ea8273eb50b5b38b5d645862c52f7790

file check SENDER sum of sample.tar_1.gz:
0167aed09f2b2d3704acdc22a93c677c

file check SENDER sum of fs_manifest.csv:
4f179c42200f4fbabeb571922c4096f0

file check SENDER sum of sample.tar_5.gz:
9375178fb5d6d249764d2fffd8be9424

file check SENDER sum of sample.tar_2.gz:
e668fde4a05c8b76ae632c58c36f7eef

file check SENDER sum of sample.tar_3.gz:
3f951beb1bbef05f4bb3f642e7e4849d

and on receiving end in Flask as follows:

@app.route('/file', methods=['POST'])
def post_file():
    """ Upload chunks; instruct merge operation """
    if request.method == 'POST':
        file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join(EXTRACTED_DIR_TEMP, filename))
        
        print(f'file check sum RECEIVER of {filename}:')
        print(hashlib.md5(file.read()).hexdigest())

results in an identical checksum that doesn't match any of the originals:

file check sum RECEIVER of sample.tar_4.gz:
d41d8cd98f00b204e9800998ecf8427e
127.0.0.1 - - [09/Jan/2021 01:39:47] "POST /file HTTP/1.1" 200 -

file check sum RECEIVER of sample.tar_1.gz:
d41d8cd98f00b204e9800998ecf8427e
127.0.0.1 - - [09/Jan/2021 01:39:48] "POST /file HTTP/1.1" 200 -

file check sum RECEIVER of fs_manifest.csv:
d41d8cd98f00b204e9800998ecf8427e
127.0.0.1 - - [09/Jan/2021 01:39:49] "POST /file HTTP/1.1" 200 -

file check sum RECEIVER of sample.tar_5.gz:
d41d8cd98f00b204e9800998ecf8427e
127.0.0.1 - - [09/Jan/2021 01:39:50] "POST /file HTTP/1.1" 200 -

file check sum RECEIVER of sample.tar_2.gz:
d41d8cd98f00b204e9800998ecf8427e
127.0.0.1 - - [09/Jan/2021 01:39:51] "POST /file HTTP/1.1" 200 -

file check sum RECEIVER of sample.tar_3.gz:
d41d8cd98f00b204e9800998ecf8427e
127.0.0.1 - - [09/Jan/2021 01:39:52] "POST /file HTTP/1.1" 200 -

Am I reading checksums correctly on receive?

In the actual implementation the directory contains chunked files from FileSplit. The checksums should match, looking for suggestions please.

question from:https://stackoverflow.com/questions/65641364/python-file-upload-changes-checksum

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

d41d8cd98f00b204e9800998ecf8427e corresponds to b'', noticed client was sending b''! file.read() operates once.

# client
read = base64.b64encode(file.read())
files = {'file': read}
response = requests.post(f'{url}/file', files=files)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...