Whenever i try to run my flask application I get this error:
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'))
My steps for running the application are: set FLASK_APP=hello
flask run
Can anyone help?
this is my view.py file
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')
CodePudding user response:
This could happen for various reasons. Maybe the port you are trying to run flask is being used from another application. Try the structure below to change the port that the flask is running.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'something'
app.run(host='127.0.0.1', port=5000)
Try to use different ports like 5000 or 8080.