非常非常小白的一个小玩意儿哈 用的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)
最后页面长这样: