目前在練習區塊鏈技術
遇到了一些問題
gitlab https://gitlab.com/jsbo12551a/blockchaintestproject
參考資料 https://www.chainnews.com/zh-hant/articles/610523687679.htm
當我透過瀏覽器開啟API http://127.0.0.1:5000/chain 時沒有問題
但是當我透過python 執行
respons = requests.get(f'http://127.0.0.1:5000/chain')
卻無法取回..
python部分
def resolve_conflicts(self):
neighbours = self.nodes
new_chain = None
max_length = len(self.chain)
for node in neighbours:
respons = requests.get(f'http://{node}/chain')
if respons.status_code == 200:
length = respons.json()['length']
chain = respons.json()['chain']
if length>max_length and self.valid_chain(chain):
max_length = length
new_chain = chain
if new_chain:
self.chain = new_chain
return True
return False
flask部分
@app.route('/nodes/resolve',methods = ['GET'])
def consensus():
replace = blockchain.resolve_conflicts()
if replace:
response = {
'messaage':'鏈已被替換',
'new_chain':blockchain.chain
}
else:
response = {
'messaage':'此鏈為最長且有效鏈(最具權威鏈)',
'new_chain':blockchain.chain,
}
return jsonify(response),200