- Learning Python for Forensics
- Preston Miller Chapin Bryce
- 318字
- 2021-08-20 10:17:13
Enhancing the print_output() function
In this function, we have made some adjustments to improve the output to the console. With the addition of the separator defined on 178, we now have a line of 15 dashes visually breaking each entry from the output. As we can see, we have borrowed the same format string from the first iteration to add this break:
171 def print_output(usb_information):
172 """
173 Print formatted information about USB Device
174 :param usb_information: dictionary containing key/value
175 data about each device or tuple of device information
176 :return: None
177 """
178 print('{:-^15}'.format(''))
We have also modified the code to allow additional output for flexible fields. In this function, we need to handle two different data types, tuples and dictionaries, since some entries do not have a resolved vendor or product name. To handle this divide in formats, we must use the isinstance() function on line 180 to test the usb_information variable data type. If the value is a dictionary, we will print each of the keys and values to the console to display one key-value pair per line on line 182. This is possible through the combination of the for loop on line 181, which uses the items() method on a dictionary. This method returns a list of tuples, where the first tuple element is the key and the second is the value. Using this method, we can quickly extract the key-value pairs, as shown on lines 181 and 182:
180 if isinstance(usb_information, dict):
181 for key_name, value_name in usb_information.items():
182 print('{}: {}'.format(key_name, value_name))
If we need to print a tuple, we use two print statements, similar to the output from the prior iteration. Because this data is from a device that could not be parsed, it has a fixed format that is the same as our previous iteration. See the following lines:
183 elif isinstance(usb_information, tuple):
184 print('Device: {}'.format(usb_information[0]))
185 print('Date: {}'.format(usb_information[1]))
- DevSecOps敏捷安全
- Web漏洞分析與防范實(shí)戰(zhàn):卷1
- 網(wǎng)絡(luò)安全應(yīng)急管理與技術(shù)實(shí)踐
- .NET安全攻防指南(上冊)
- Computer Forensics with FTK
- 數(shù)據(jù)安全領(lǐng)域指南
- 網(wǎng)絡(luò)安全與攻防入門很輕松(實(shí)戰(zhàn)超值版)
- 網(wǎng)絡(luò)安全態(tài)勢感知
- 數(shù)據(jù)安全架構(gòu)設(shè)計(jì)與實(shí)戰(zhàn)
- 復(fù)雜信息系統(tǒng)網(wǎng)絡(luò)安全體系建設(shè)指南
- 網(wǎng)絡(luò)空間安全導(dǎo)論
- 云計(jì)算安全技術(shù)與應(yīng)用
- Kali Linux無線網(wǎng)絡(luò)滲透測試詳解
- 網(wǎng)絡(luò)安全與維護(hù)
- 應(yīng)用密碼學(xué):原理、分析與Python實(shí)現(xiàn)