- Learning Python for Forensics
- Preston Miller Chapin Bryce
- 255字
- 2021-08-20 10:17:14
Understanding the get_address() function
This is an integral, though a potentially error-prone, component of our script because it relies on the user correctly supplying data. The code itself is just a simple data request. However, when working with user supplied arguments, it isn't safe to assume that the user gave the script the correct data. Considering the length and somewhat random-looking sequence of a Bitcoin address, it's entirely possible that the user might supply an incorrect address. We'll catch any instance of URLError from the urllib.error module to handle a malformed input. URLError isn't part of the built-in exceptions we've talked about before and is a custom exception defined by the urrlib module:
053 def get_address(address):
054 """
055 The get_address function uses the blockchain.info Data API
056 to pull pull down account information and transactions for
057 address of interest
058 :param address: The Bitcoin Address to lookup
059 :return: The response of the url request
060 """
On line 62, we insert the user-supplied address into the blockchain.info API call using the string format() method. Then, we try to return the data requested using the urllib.request.urlopen() function. If the user supplies an invalid address or if the user doesn't have an internet connection, URLError will be caught. Once the error has been caught, we notify the user and exit the script, calling sys.exit(1) on line 67:
061 url = 'https://blockchain.info/address/{}?format=json'
062 formatted_url = url.format(address)
063 try:
064 return urllib.request.urlopen(formatted_url)
065 except urllib.error.URLError:
066 print('Received URL Error for {}'.format(formatted_url))
067 sys.exit(1)
- Web漏洞分析與防范實(shí)戰(zhàn):卷1
- 工業(yè)互聯(lián)網(wǎng)安全
- 零信任網(wǎng)絡(luò):在不可信網(wǎng)絡(luò)中構(gòu)建安全系統(tǒng)
- Rootkit和Bootkit:現(xiàn)代惡意軟件逆向分析和下一代威脅
- Web安全與攻防入門(mén)很輕松(實(shí)戰(zhàn)超值版)
- 防火墻技術(shù)與應(yīng)用(第2版)
- Computer Forensics with FTK
- 網(wǎng)絡(luò)運(yùn)維親歷記 (網(wǎng)絡(luò)運(yùn)維紀(jì)實(shí)文學(xué))
- 計(jì)算機(jī)網(wǎng)絡(luò)安全基礎(chǔ)(第5版)
- 物聯(lián)網(wǎng)安全滲透測(cè)試技術(shù)
- End to End GUI Development with Qt5
- 電腦安全與攻防入門(mén)很輕松(實(shí)戰(zhàn)超值版)
- Learning Pentesting for Android Devices
- 持續(xù)集成:軟件質(zhì)量改進(jìn)和風(fēng)險(xiǎn)降低之道
- 交換機(jī)·路由器·防火墻(第2版)