2021-2022-1 20211418 《信息安全专业导论》第九周学习总结

作业信息

[2021-2022-1信息安全专业导论](https://edu.cnblogs.com/campus/besti/2021-2022-1fois)
[2021-2022-1信息安全专业导论第九周作业](https://www.cnblogs.com/rocedu/p/9577842.html#WEEK09)

教材学习内容总结

  1. 阅读《计算机科学概论》第10,11章(操作系统,文件系统和目录),了解了操作系统的角色、内存管理、进程管理、CPU调度、文件系统的概念、目录、磁盘调度等内容。
  2. 阅读《看漫画学Python》第12,16章(文件读写、多线程),了解了如何运用Python实现打开文件、关闭文件、读写文本文件、复制文本文件、读写二进制文件、复制二进制文件,学习了线程相关的知识,并了解了线程模块,学会了创建子线程、线程管理、下载图片示例等内容。

教材学习中的问题和解决过程

问题1:有时候读《看漫画学Python》,它给出的示例会包含一些我不明白的函数的引用。
问题1解决方案:因为学习不是按照教材编写的顺序写的,所以有的内容可以参考前面还没有看过的章节,先进行自学,或者在网上查找相关资料。

代码调试过程中的问题和解决过程

问题1: 在运行16章下载图片示例的代码时,我无法正常运行,总是弹出错误:
Traceback (most recent call last):
File "E:\学习\编程\Python\下载图片示例.py", line 3, in
import urllib.request
File "C:\Users\matsuri\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 84, in
import base64
File "E:\学习\编程\Python\base64.py", line 2, in
import requests
ModuleNotFoundError: No module named 'requests'
问题1解决方案: 一开始我以为是我打错了单词。但是经检查我的代码没有问题。通过查找资料理解代码的含义,我认为可能是因为前面章节中的实现14章的网络通信还没有实现。目前还没有学习完14章,其内容我也还没能成功运行。等我学习完14章内容,我应该就可以正常运行了。

代码托管

f = open('test.txt','w+')
f.write('World')
print('1创建test。txt文件,World写入文件')

f = open('test.txt','r+')
f.write('Hello')
print('2打开test.txt文件。Hello覆盖文件内容')

f = open('test.txt','a')
f.write(' ')
print(r'3打开test.txt文件,在文件尾部追加空格" "')

fname = 'E:/学习/编程/Python/test.txt'
f = open(fname,'a+')
print('4打开test.txt文件,在文件尾部追加World')

f_name = 'test.txt'
f = None
try:
f = open(f_name)
print('Done successfully')
content = f.read()
print(content)
except FileNotFoundError as e:
print('The file does not exist,please use program ch12_.1py establish one')
except OSError as e:
print ('Deal with OSError')
finally:
if f is not None:
f.close()
print('File closed successfully')

f_name = 'test.txt'
with open(f_name) as f:
content = f.read()
print(content)

f_name = 'src_file.txt'

with open(f_name,'r',encoding='gbk') as f:
lines = f.readlines()
copy_f_name = 'dest_file.txt'
with open(copy_f_name,'w',encoding = 'utf-8') as copy_f:
copy_f.writelines(lines)
print('File copied successfully')

f_name = 'logo.png'

with open(f_name,'rb') as f:
b = f.read()
copy_f_name = 'logo2.png'
with open(copy_f_name,'wb') as copy_f:
copy_f.write(b)
print('File copied successfully')

import threading

t = threading.current_thread()
print(t.name)
print(threading.active_count())
t = threading.main_thread()
print(t.name)

import threading
import time

def thread_body():
t = threading.current_thread()
for n in range(5):
print('第{0}次执行线程{1}'.format(n,t.name))
time.sleep(2)

print('Done successfully')

t1 = threading.Thread(target = thread_body)
t2 = threading.Thread(target = thread_body,name = 'MyThread')
t1.start()
t2.start()

import threading
import time

class SmallThread(threading.Thread):
def init(self,name = None):
super().init(name=name)
def run(self):
t = threading.current_thread()
for n in range(5):
print('第{0}次执行线程{1}'.format(n,t.name))
time.sleep(2)
print("Done successfully".format(t.name))

t1 = SmallThread()
t2 = SmallThread(name='MyThread')
t1.start()
t2.start()

import threading
import time

value = []

def thread_body():
print('t1子线程开始...')
for n in range(2):
print('t1子线程执行...')
value.append(n)
time.sleep(2)
print('t1子线程结束.')

print('主线程开始执行...')
t1 = threading.Thread(target= thread_body)
t1.start()
t1.join()
print('value = {0}'.format(value))
print('主线程继续执行...')

import threading
import time

isrunning = True

def workthread_body():
while isrunning:
print("working thread is running...")
time.sleep(5)
print("working thread is done.")

def controlthread_body():
global isrunning
while isrunning:
command = input("input the command to stop:")
if command == 'exit':
isrunning = False
print("working thread is done.")

workthread = threading.Thread(target = workthread_body)
workthread.start()

controlthread = threading.Thread(target=controlthread_body)
controlthread.start()

import threading
import time
import urllib.request

isrunning = True

def workthread_body():
while isrunning:
print("工作线程执行下载任务...")
download()
time.sleep(5)
print("工作线程结束。")

def controlthread_body():
global isrunning
while isrunning:
command = input("请输入停止指令: ")
if command == 'exit':
isrunning = False
print("控制线程结束。")

def download():
url = 'http://localhost:8080/NoteWebService/logo.png'
req = urllib.request.Request(url)
with urllib.request.urlopen(url) as response:
data = response.read()
f_name = 'download.png'
with open(f_name,'wb') as f:
f.write(data)
print("下载文件成功")

workthread = threading.Thread(target = workthread_body)
workthread.start()
controlthread = threading.Thread(target=controlthread_body)
controlthread.start()

上周考试错题总结

暂无。

其他

无。

学习进度条

| | 代码行数(新增/累积)| 博客量(新增/累积)|学习时间(新增/累积)|重要成长|
| 第一周 | 200/200 | 2/2 | 20/20 | |
| 第二周 | 200/400 | 2/4 | 20/40 | |
| 第三周 | 200/600 | 2/6 | 20/60 | |
| 第四周 | 200/800 | 2/8 | 20/80 | |
| 第五周 | 200/1000 | 2/10 | 20/100 | |
| 第六周 | 200/1200 | 1/11 | 20/120 | |
| 第七周 | 200/1400 | 1/12 | 20/140 | |
| 第八周 | 200/1600 | 1/13 | 20/160 | |
| 第九周 | 200/1800 | 3/16 | 20/180 | |

参考资料

《计算机科学概论》《看漫画学Python》

 posted on 2021-11-20 15:36  20211418  阅读(9)  评论(0编辑  收藏  举报