[Python] Flask Waitress Webapp

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    PS C:\Users\****\OneDrive\Projects> python appv2.0.py
    python : ERROR:appv2.0:Exception on / [POST]
    At line:1 char:1
    + python appv2.0.py
    + ~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (ERROR:appv2.0:Exception on / [POST]:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    Traceback (most recent call last):
    File "C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
    File "C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1823, in full_dispatch_request
    return self.finalize_request(rv)
    File "C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1842, in finalize_request
    response = self.make_response(rv)
    File "C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2135, in make_response
    f"The view function for {request.endpoint!r} did not"
    TypeError: The view function for 'json' did not return a valid response. The function either returned None or ended without a return statement.
    ERROR:appv2.0:Exception on / [POST]


    # import main Flask class and request object
    from flask import Flask, request
    from waitress import serve
    import os
    import json as jsons

    # create the Flask app
    app = Flask(__name__)

    def log(mode, d):
    os.chdir("C:/Users/****/Desktop")
    if mode == "o":
    with open('Trend.txt', 'r+') as f:
    firstLine = f.readline().rstrip('\r\n')
    flDict = jsons.loads(firstLine)
    if d["t"] != flDict["t"]:
    ds = "{'sym': '" + d['sym'] + "', 't': '" + d['t'] + "', 'i': " + str(d['i']) + ", 'tt': " + str(d['tt']) + ", 'tc': " + str(d['tc']) + "}"
    ds = ds.replace("'", '"')
    f.seek(0, 0)
    f.write(ds.rstrip('\r\n') + '\n' + firstLine)
    f.truncate()
    with open('Alert.txt', 'r+') as f:
    firstLine = f.readline().rstrip('\r\n')
    flDict = jsons.loads(firstLine)
    if d["t"] != flDict["t"]:
    d = str(d)
    d = d.replace("'", '"')
    f.seek(0, 0)
    f.write(d.rstrip('\r\n'))
    f.truncate()
    with open('Alert_History.txt', 'r+') as f:
    content = f.read()
    f.seek(0, 0)
    f.write(firstLine + '\n' + content)

    elif mode == "t":
    with open('Trend.txt', 'r+') as f:
    firstLine = f.readline().rstrip('\r\n')
    flDict = jsons.loads(firstLine)
    if d["t"] != flDict["t"]:
    d = str(d)
    d = d.replace("'", '"')
    f.seek(0, 0)
    f.write(d.rstrip('\r\n') + '\n' + firstLine)
    f.truncate()
    elif mode == "m":
    with open('MACD.txt', 'r+') as f:
    firstLine = f.readline().rstrip('\r\n')
    flDict = jsons.loads(firstLine)
    if d["t"] != flDict["t"]:
    d = str(d)
    d = d.replace("'", '"')
    f.seek(0, 0)
    f.write(d.rstrip('\r\n') + '\n' + firstLine)
    f.truncate()
    elif mode == "ham":
    with open('HA-MACD.txt', 'r+') as f:
    firstLine = f.readline().rstrip('\r\n')
    flDict = jsons.loads(firstLine)
    if d["t"] != flDict["t"]:
    d = str(d)
    d = d.replace("'", '"')
    f.seek(0, 0)
    f.write(d.rstrip('\r\n') + '\n' + firstLine)
    f.truncate()
    elif mode == "rza":
    with open('RZ-Alert.txt', 'r+') as f:
    firstLine = f.readline().rstrip('\r\n')
    flDict = jsons.loads(firstLine)
    if d["t"] != flDict["t"]:
    d = str(d)
    d = d.replace("'", '"')
    f.seek(0, 0)
    f.write(d.rstrip('\r\n') + '\n' + firstLine)
    f.truncate()

    @app.route('/', methods=['POST'])
    def json():
    request_data = request.get_json()
    print(request_data)
    if request_data["ty"] == "o":
    log("o", request_data["d"])

    ticker = request_data["d"]["sym"]
    time = request_data["d"]["t"]
    interval = request_data["d"]["i"]
    buy = request_data["d"]["b"]
    sell = request_data["d"]["s"]
    strong_buy = request_data["d"]["sb"]
    strong_sell = request_data["d"]["ss"]

    return "{}, {}, interval: {}, buy: {}, sell: {}, strong buy: {}, strong sell: {}.".format(ticker, time, interval, buy, sell, strong_buy, strong_sell)
    elif request_data["ty"] == "t":
    log("t", request_data["d"])

    ticker = request_data["d"]["sym"]
    time = request_data["d"]["t"]
    interval = request_data["d"]["i"]
    trend_tracer = request_data["d"]["tt"]
    trend_catcher = request_data["d"]["tc"]

    return "{}, {}, interval: {}, trend tracer: {}, trend catcher: {}.".format(ticker, time, interval, trend_tracer, trend_catcher)
    elif request_data["ty"] == "m":
    log("m", request_data["d"])

    ticker = request_data["d"]["sym"]
    time = request_data["d"]["t"]
    interval = request_data["d"]["i"]
    histogram = request_data["d"]["h"]
    umacd = request_data["d"]["m"]
    signal = request_data["d"]["s"]

    return "{}, {}, interval: {}, histogram: {}, umacd: {}, signal: {}.".format(ticker, time, interval, histogram, umacd, signal)
    elif request_data["ty"] == "ham":
    log("ham", request_data["d"])

    ticker = request_data["d"]["sym"]
    time = request_data["d"]["t"]
    interval = request_data["d"]["i"]
    histogram = request_data["d"]["h"]
    umacd = request_data["d"]["m"]
    signal = request_data["d"]["s"]

    return "{}, {}, interval: {}, histogram: {}, umacd: {}, signal: {}.".format(ticker, time, interval, histogram, umacd, signal)
    elif request_data["ty"] == "rza":
    log("rza", request_data["d"])

    ticker = request_data["d"]["sym"]
    time = request_data["d"]["t"]
    interval = request_data["d"]["i"]
    buy = request_data["d"]["b"]
    sell = request_data["d"]["s"]

    return "{}, {}, interval: {}, buy: {}, sell: {}.".format(ticker, time, interval, buy, sell)

    #if __name__ == '__main__':
    # run app in debug mode on port 8080
    #app.run(debug=True, port=8080)

    serve(app, host='127.0.0.1', port=8080, threads=2)


    I attempted to execute the code via Powershell and this is the error I was given. Please help me figure this one out. I built a webapp using flask and waitress in 2023 and it has been a while since I have updated. It could be a matter of updating some package dependencies but I am not sure. I am a bit rusty with my coding so please excuse me if I am not up to date.

    Continue reading...

Compartilhe esta Página