【问题标题】:How to connect to main Flask APP from a remote server如何从远程服务器连接到主 Flask APP
【发布时间】:2020-04-09 18:51:48
【问题描述】:
 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 的值吗脚本回到我的烧瓶应用程序?

【问题讨论】:

    标签: python linux flask ssh


    【解决方案1】:

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

    【讨论】:

    • 如何查看所有端口都打开了?
    • @MathewPius 最好的选择是从您的家庭路由器 Web UI 开始。您还可以在线搜索“检查开放端口”网络服务。最后,为了测试更接近您的特定应用程序的东西,您可以在您的 AWS 机器上安装 nc(netcat - 一个出色的网络工具)尝试打开端口 8080 与您的家用机器 nc -v <IP> 8080 的连接。这可以快速测试您的 AWS 机器和家庭机器之间的连接。您还可以使用 nmap 进行实际扫描,但这太过分了 (nmap.org/book/port-scanning-tutorial.html)。
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多