Python I'm a beginner studying.
I am writing a program to download hundreds of megabytes to several gigabytes of data and MD5 from a web server with basic authentication for study and calculate its hash value.
As I am a beginner, I wrote with emphasis on readability without using regular expressions.
MD5 also extracts 32 characters from the beginning because it contains a filename after 32 characters of hash value.
It takes up to 1200 seconds to complete the program download, but it didn't seem like it was actually running after 1200 seconds.
How can I fix the error or improve the program?
import urlib.request
import base64
user='abc'
password='123'
basic_user_and_password=base64.b64encode('{}:{}.format(user, password).encode('utf-8')))
url="http://nanashi/abcde.iso"
request = urllib.request.Request(url,
headers = {"Authorization": "Basic" + basic_user_and_password.decode('utf-8')})
with urllib.request.urlopen(request) as:
data=res.read()
with open('abcde', 'wb') as f:
f.write(data)
url="http://nanashi/abcde.iso.md5"
request = urllib.request.Request(url,
headers = {"Authorization": "Basic" + basic_user_and_password.decode('utf-8')})
with urllib.request.urlopen(request) as:
data=res.read()
with open('abcde.iso.md5', 'wb') as f:
f.write(data)
import time
time.sleep(1200)
import hashlib
with open("abcde.iso", "rb") as f:
file_hash=hashlib.md5()
while chunk: = f.read (8192):
file_hash.update(chunk)
print(file_hash.digest())
print(file_hash.hexdigest())
path1 = "abcde.iso.md5"
file1 = open (path1, 'r', encoding = 'utf-8')
f1 = file1.read()
f2 = print (f1 [0:32])
file1.close()
print(f2==filechecksum)
Error Contents
while chunk:=f.read(8192):
^
SyntaxError: invalid syntax
As @metropolis commented, this notation is supported for Python versions 3.8 or higher. Check out Python versions.
Also, as stated in the document, it may be better to clearly state whether the read data is empty or not.
This article describes the new features of Python 3.8 compared to 3.7.Please check the changelog for full details.
Overview -- Release Highlights
New Features
As part of the larger syntax, the new syntax for assigning values to variables: = was added. This syntax is known by the nickname "Seach operator" because it resembles the walrus eyes and tusks.
This operator is also useful for while statements that use the generated value in both loop termination determination and loop body:
#Loop over fixed length blocks
while(block:=f.read(256))!=':
process(block)
© 2024 OneMinuteCode. All rights reserved.