flask 应用程序带来错误 requests.exceptions.ConnectionError requests.exceptions.ConnectionError Max retries exceeded with url: /chain

flask application brings error requests.exceptions.ConnectionError requests.exceptions.ConnectionError Max retries exceeded with url: /chain

每当我尝试 运行 我的烧瓶应用程序时,我都会收到此错误:

requests.exceptions.ConnectionError
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): 
Max retries exceeded with url: //chain (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001EECEBA9E10>: 
Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

我 运行 申请的步骤是: 设置 FLASK_APP=你好

flask run

有人能帮忙吗?

这是我的 view.py 文件

import datetime
import json

import requests
from flask import render_template, redirect, request

from app import app

# The node with which our application interacts, there can be multiple
# such nodes as well.
CONNECTED_NODE_ADDRESS = "http://127.0.0.1:8000/"

posts = []


def fetch_posts():
    """
    Function to fetch the chain from a blockchain node, parse the
    data and store it locally.
    """
    get_chain_address = "{}/chain".format(CONNECTED_NODE_ADDRESS)
    response = requests.get(get_chain_address)
    if response.status_code == 200:
        content = []
        chain = json.loads(response.content)
        for block in chain["chain"]:
            for tx in block["transactions"]:
                tx["index"] = block["index"]
                tx["hash"] = block["previous_hash"]
                content.append(tx)

        global posts
        posts = sorted(content, key=lambda k: k['timestamp'],
                       reverse=True)


@app.route('/')
def index():
    fetch_posts()
    return render_template('index.html',
                           title='YourNet: Decentralized '
                                 'content sharing',
                           posts=posts,
                           node_address=CONNECTED_NODE_ADDRESS,
                           readable_time=timestamp_to_string)


@app.route('/submit', methods=['POST'])
def submit_textarea():
    """
    Endpoint to create a new transaction via our application.
    """
    post_content = request.form["content"]
    author = request.form["author"]

    post_object = {
        'author': author,
        'content': post_content,
    }

    # Submit a transaction
    new_tx_address = "{}/new_transaction".format(CONNECTED_NODE_ADDRESS)

    requests.post(new_tx_address,
                  json=post_object,
                  headers={'Content-type': 'application/json'})

    return redirect('/')


def timestamp_to_string(epoch_time):
    return datetime.datetime.fromtimestamp(epoch_time).strftime('%H:%M')

发生这种情况的原因有多种。也许您尝试 运行 flask 的端口正被另一个应用程序使用。尝试下面的结构来更改烧瓶 运行ning.

的端口
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'something'

app.run(host='127.0.0.1', port=5000)

尝试使用不同的端口,例如 5000 或 8080。