我在家无聊,所以编了一个简简单单的模拟微信。
1:工具
- python3.8.4
- 记事本
2:需要插件
- web.py
- os
3 : 下载插件
打开cmd,输入:
python -m pip install web.py
即可下载web.py.
注意:os为python自带,无需下载
4:代码解说(用备注)
import os #为class index的持续刷新做清屏准备
import web #整个文件的重中之重
urls = (
'/dhl1/(.*)', 'dhl1',
'/dhl2/(.*)', 'dhl2',
'/index/(.*)', 'index',
'/xx/(.*)', 'xx'
) # 左边代表网址,右边代表打开此网页后所运行的class
app = web.application(urls, globals()) #创建一个应用,globals()会返回一个类似字典的对象,包含当前空间所有变量、函数、类以及模块,键是这些东西的名称,值是响应对象,这样就可以通过名字来获取对象。
a = '' #把所有信息存储起来
class dhl1:
def GET(self, name):
return """<!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>
<body>
<div class="div">
<div class="dtod1">
</div>
<div class="dtod2"></div>
<div class="dtod3">
<input type="text" id="input"><button οnclick="ok()">发送</button>
</div>
</div>
</body>
<style>
.div {
width: 50%;
margin-left: 25%;
margin-right: 25%;
}
.dtod1,
.dtod3 {
height: 5%;
}
.dtod2 {
height: 90%;
overflow: auto;
width: 100%;
}
.t1 {
text-align: right;
}
.f1 {
text-align: left;
}
</style>
<script>
document.querySelector('div').style.height = `${innerHeight - 16}px`; //定义div的高
function a() { //针对dtod2里的聊天内容
for (var i = 0; i < window.ds.length; i++) {
var div = document.createElement('div');
var p = document.createElement('span');
p.innerText = window.ds[i].text;
div.appendChild(p);
document.getElementsByClassName('dtod2')[0].appendChild(div)
}
}
open('/index/') //为了读取消息
window.set = setInterval(function () {
document.getElementsByClassName('dtod2')[0].innerHTML=''
eval('window.ds=' + localStorage.getItem('index'));
a();
}, 1000); //每1000毫秒执行一次
function ok() {
window.open('/xx/{r:1,text:"dhl1:' + document.getElementById('input').value + '"}', 'newwindow') //发送消息
document.getElementById('input').value = '' //清空消息发送框
}
</script>
</html>""" #dhl1.html
class dhl2:
def GET(self, name):
return """<!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>
<body>
<div class="div">
<div class="dtod1">
</div>
<div class="dtod2"></div>
<div class="dtod3">
<input type="text" id="input"><button οnclick="ok()">发送</button>
</div>
</div>
</body>
<style>
.div {
width: 50%;
margin-left: 25%;
margin-right: 25%;
}
.dtod1,
.dtod3 {
height: 5%;
}
.dtod2 {
height: 90%;
overflow: auto;
width: 100%;
}
body .f1,
body .t1 {
display: hidden;
}
.t1 {
text-align: right;
}
.f1 {
text-align: left;
}
</style>
<script>
document.querySelector('div').style.height = `${innerHeight - 16}px`;
function a() {
for (var i = 0; i < window.ds.length; i++) {
var div = document.createElement('div');
var p = document.createElement('span');
p.innerText = window.ds[i].text;
div.appendChild(p);
document.getElementsByClassName('dtod2')[0].appendChild(div)
}
}
open('/index/')
window.set = setInterval(function () {
document.getElementsByClassName('dtod2')[0].innerHTML=''
eval('window.ds=' + localStorage.getItem('index'));
a();
}, 1000);
function ok() {
window.open('/xx/{r:2,text:"dhl2:' + document.getElementById('input').value + '"}', 'newwindow')
document.getElementById('input').value = ''
}
</script>
</html>""" #dhl2.html,和dhl1.html没什么区别,所以请看dhl1.html
class xx:
def GET(self, name):
global a #为了执行以下代码
a += name+','
return '<script>window.close()</script>'#xx.html,主要负责信息增加到a变量
class index:
def GET(self, name):
os.system('cls') #因为一秒钟就会打开一次,所以要清屏
return '<script>localStorage.index=\'['+a+']\';setInterval(function () {location.reload()},1000)</script>'#index.html,用localStorage向dhl1.html,dhl2.html发送a里的信息
if __name__ == "__main__":
import socket
print(u'id:'+socket.gethostbyname(socket.gethostname()) +
u':8080+/dhl1/或/dhl2/(看哪个先到,不要两位一起,不用管下面的网址)') #为了提醒网址页面
app.run()