如何从远程服务器连接到Flask主APP

时间:2020-04-09 18:51:48

标签: python linux flask ssh

 import psutil

STATS_URL = ' http://172.20.10.2:8080/report'
SERVER_NAME="test_local_server"

def get_health(): 

    print('generating health report')

    cpu_percent = psutil.cpu_percent(interval=2.0)
    ctime = psutil.cpu_times()
    disk_usage = psutil.disk_usage("/")
    net_io_counters = psutil.net_io_counters()
    virtual_memory = psutil.virtual_memory()    

    # The keys in this dict should match the db cols
    report = dict (
        USER_NAME="USER",
        SERVER_NAME="Test_Server",
        cpupercent = cpu_percent,
        cpu_total = ctime.user + ctime.system,
        free_Percnt=((disk_usage.free/disk_usage.used)*100),
        bytes_sent = net_io_counters.bytes_sent,
        bytes_received = net_io_counters.bytes_recv,
        packets_sent = net_io_counters.packets_sent,
        packets_received = net_io_counters.packets_recv,
        memory_Free = virtual_memory.free,
        )

    return report

if __name__=='__main__':


    print(f'starting health report stream for server :\t{SERVER_NAME}')

    while True:
        report = get_health()
        r = requests.post(STATS_URL, json=report)
        time.sleep(20)

我需要将此AWS远程服务器上的脚本的值发送回我的Flask APP,后者是Windows笔记本电脑上的主服务器。

@app.route("/report",methods=['POST'])
def report():
    if request.method=='POST':
        time_epoch=time.time()
        incoming_report = request.get_json()
        print("Generating Health report")
        username=incoming_report["USER_NAME"]
        server_name=incoming_report["SERVER_NAME"]
        disk_free=incoming_report["free_Percnt"]
        bytes_sent=incoming_report["bytes_sent"]
        bytes_received=incoming_report["bytes_received"]
        packets_sent=incoming_report["packets_sent"]
        packets_received=incoming_report["packets_received"]
        memory_free=incoming_report["memory_Free"]
        cpu_percent=incoming_report["cpupercent"]
        cpu_total=incoming_report["cpu_total"]
        conn=sqlite3.connect("Health.db")
        conn.execute(f"create table if not exists {username}_{server_name} 
 (HEALTH_ID integer primary key AUTOINCREMENT,Time_Epoch integer,Disk_Free varchar(80),Bytes_Sent varchar(80),Bytes_Received varchar(80),Packets_Sent varchar(80),Packets_Received varchar(80),Memory_Free varchar(80),Cpu_Usage_Percent varchar(80),Cpu_Time varchar(80));")
        conn.execute(f'insert into {username}_{server_name} (Time_Epoch,Disk_Free,Bytes_Sent,Bytes_Received,Packets_Sent,Packets_Received,Memory_Free,Cpu_Usage_Percent,Cpu_Time) values {time_epoch,disk_free,bytes_sent,bytes_received,packets_sent,packets_received,memory_free,cpu_percent,cpu_total}')
        conn.commit()
        return {'message': 'success'}



if __name__ ==("__main__"):
    app.run(host='0.0.0.0',port=8080)

所以我将主机更改为“ 0.0.0.0”,原因是当我在线检查时发现我将烧瓶应用程序公开,但是脚本的连接显示连接超时。有人可以帮我从python发送值脚本返回到我的烧瓶应用程序?

1 个答案:

答案 0 :(得分:0)

您需要转发路由器上的端口8080。现在,您的家用路由器也是防火墙,不允许任何启动与LAN机器(您的PC)的连接。需要告知“如果有人尝试通过端口8080连接到我们的家庭IP,请将其通过内部IP x.x.x.x连接到我的PC”

相关问题