iT邦幫忙

0

python requests.get() 無法取得flask架設的api

目前在練習區塊鏈技術
遇到了一些問題

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
這是很基本的啊, 不是 respons.status_code == 200 , 那就 else , 把 status_code 印出來看啊 .
先把 Python 基本學好啦.
Zoey iT邦新手 5 級 ‧ 2020-07-09 17:58:04 檢舉
使用postman跑了20分鐘左右,畫面一直停在sending request...,所以印不出status_code ..

尚未有邦友回答

立即登入回答