Skip to content

Commit 03940cf

Browse files
authored
改用WebSocket,兼容js页端调用
1 parent ae3c8bd commit 03940cf

1 file changed

Lines changed: 34 additions & 34 deletions

File tree

gather/score_server.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
"""
77

88
from sqlite3 import connect, OperationalError
9-
from socket import socket, timeout
109
from time import localtime
1110
from os import path, system
1211
from sys import exit as sysexit
1312
from myMod import mbox
13+
from websocket_server import WebsocketServer
14+
from urllib.parse import unquote
1415

15-
system("mode con cols=72 lines=50")
16-
sock = socket(2, 1) #socket.AF_INET=2, socket.SOCK_STREAM=1
17-
sock.bind(('0.0.0.0', 8001))
18-
sock.listen(5)
16+
system("mode con cols=72 lines=30")
17+
server = WebsocketServer(8001,'0.0.0.0')
1918
if localtime().tm_mon < 8:
2019
dbpath = "./hwmy"+str(localtime().tm_year-2)[-2:]+".db"
2120
else:
@@ -24,34 +23,35 @@
2423
print('No database file in exe path, check please!')
2524
mbox('错误','未能在程序目录下找到数据库文件{}!\n请查验后重试!'.format(dbpath[2:]),'error')
2625
sysexit(0)
27-
conn = connect(dbpath)
28-
curs = conn.cursor()
29-
while True:
30-
connection,address = sock.accept()
31-
try:
32-
connection.settimeout(5)
33-
sql = connection.recv(1024).decode('utf-8')
34-
if len(sql)>0 and sql[:6]!='UPDATE':
35-
try:
36-
curs.execute(sql)
37-
conn.commit()
38-
datas = curs.fetchall()
39-
connection.send(str(datas).encode('gbk'))
40-
except OperationalError as e:
41-
connection.send(str(e).encode('gbk'))
42-
elif len(sql)>0 and sql[:6]=='UPDATE':
43-
print(sql[7:11])
44-
if sql[7:11] =='list': # 批量更新成绩
45-
scores = eval(sql[11:])
46-
#print(scores)
47-
for i in range(len(scores)):
48-
curs.execute('UPDATE students SET score = '+str(scores[i][1])+' WHERE ID = "'+scores[i][0]+'"')
49-
else:
50-
curs.execute(sql)
26+
27+
def message_received(client, server, sqlcmd):
28+
global dbpath
29+
sqlcmd = unquote(sqlcmd)
30+
print('SQL command:',sqlcmd)
31+
conn = connect(dbpath)
32+
curs = conn.cursor()
33+
if len(sqlcmd)>0 and sqlcmd[:6].upper()=='SELECT':
34+
try:
35+
curs.execute(sqlcmd)
5136
conn.commit()
52-
connection.send('"OK"'.encode('gbk'))
37+
datas = list(map(list,curs.fetchall()))
38+
except OperationalError as e:
39+
datas = str(e)
40+
elif len(sqlcmd)>0 and sqlcmd[:6].upper()=='UPDATE':
41+
if sqlcmd[7:11].lower() =='list': # 批量更新成绩
42+
scores = eval(sqlcmd[11:])
43+
#print(scores)
44+
for i in range(len(scores)):
45+
curs.execute('UPDATE students SET score = '+str(scores[i][1])+' WHERE ID = "'+scores[i][0]+'"')
46+
print('更新{}条记录!'.format(str(len(scores))))
5347
else:
54-
connection.send('"Sql command format error!"'.encode('gbk'))
55-
except timeout:
56-
print('time out')
57-
connection.close()
48+
curs.execute(sqlcmd)
49+
conn.commit()
50+
datas = '"OK"'
51+
else:
52+
datas = '"Sql command format error!"'
53+
server.send_message(client,str(datas))
54+
conn.close()
55+
56+
server.set_fn_message_received(message_received)
57+
server.run_forever()

0 commit comments

Comments
 (0)