手机远程实现电脑关机

非常非常小白的一个小玩意儿哈 用的flask实现手机和电脑的局域网连接

文件目录:

只有一个html文件和python的flask主程序哈

首先导入我们需要的库

from flask import Flask,render_template,request
import os
import socket

分别是flask 和系统库os 还有为了获取ip的socket

name = socket.gethostname()
ip = socket.gethostbyname(name)

然后获取主机名和主机ip


app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def hello():
    if request.method == 'POST':
        print("我要关机了")
        os.system("shutdown /s /t 10")
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True,host=ip)

然后再看看前端页面的编写:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    body{
        margin: 0;
        padding: 0;
    }
    a{
        text-decoration: none;
    }
    button{
        width: 80%;
        padding: 10% 0% 10% 0%;
        text-align: center;
        font-size: 36px;
        background-color: pink;
        margin: auto;
        margin-top: 200px;
        border-radius: 10px;
        margin-left: 40px;
    }
</style>
<body>
   <form method="POST">
    <a href="" type="submit">
        <button type="submit" name="button">
            <div class="guan">
                关机
            </div>
        </button>
    </a>
   </form>
</body>
</html>

完整的后端:

from flask import Flask,render_template,request
import os
import socket
name = socket.gethostname()
ip = socket.gethostbyname(name)

app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def hello():
    if request.method == 'POST':
        print("我要关机了")
        os.system("shutdown /s /t 10")
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True,host=ip)

最后页面长这样:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搞事儿布

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值