from\u0020flask\u0020import\u0020Flask,\u0020request\nfrom\u0020subprocess\u0020import\u0020Popen,\u0020PIPE\n\napp\u0020=\u0020Flask(name) \nprocess\u0020=\u0020None\n\n@app.route('/start',\u0020methods=['POST'])\ndef\u0020start():\n\u0020\u0020global\u0020process\n\u0020\u0020times\u0020=\u0020request.form.get('times')\n\u0020\u0020command\u0020=\u0020f"ping\u00201.1.1.1\u0020-n\u0020{times}"\n\u0020\u0020process\u0020=\u0020Popen(command,\u0020shell=True,\u0020stdout=PIPE,\u0020stderr=PIPE)\n\u0020\u0020return\u0020'Started',\u0020200\n\n@app.route('/stop',\u0020methods=['POST'])\ndef\u0020stop():\n\u0020\u0020global\u0020process\n\u0020\u0020if\u0020process:\n\u0020\u0020\u0020\u0020process.terminate()\n\u0020\u0020\u0020\u0020process\u0020=\u0020None\n\u0020\u0020return\u0020'Stopped',\u0020200\n\n@app.route('/')\ndef\u0020index():\n\u0020\u0020return\u0020'Welcome\u0020to\u0020the\u0020Ping\u0020API!'\n\nif\u0020__name__\u0020==\u0020"main":\n\u0020\u0020app.run(port=5000)

Flask Ping API: Start, Stop and Welcome Route

原文地址: https://www.cveoy.top/t/topic/qz1q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录