-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinecraft-Server-Maintaining.py
64 lines (51 loc) · 1.55 KB
/
Minecraft-Server-Maintaining.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
# Using MIT License.
# https://github.com/bddjr/Minecraft-Server-Maintaining
#
# Tested version:
# - Python: 3.12.5
# - Minecraft: 1.21.4 1.12.2 1.8.9
# ------
# Config
ADDR = ("", 25565)
DESCRIPTION_TEXT = "§c服务器正在维护\nServer Maintaining"
DESCRIPTION_VERSION = "Server Maintaining"
JOIN_ERROR = "§c服务器正在维护\nServer Maintaining"
# ------
import socket, socketserver, json
print(f"127.0.0.1:{ADDR[1]}\nPress Ctrl+C to stop.")
description = json.dumps(
{
"description": {"text": DESCRIPTION_TEXT},
"players": {"max": 0, "online": 0},
"version": {"name": DESCRIPTION_VERSION, "protocol": 0},
}
).encode("utf-8")
description = bytes([len(description) + 3, 1, 0, len(description), 1]) + description
joinError = json.dumps(JOIN_ERROR).encode("utf-8")
joinError = bytes([len(joinError) + 2, 0, len(joinError)]) + joinError
class Handler(socketserver.BaseRequestHandler):
def handle(self):
c: socket.socket = self.request
c.settimeout(5)
packetLen = c.recv(1)[0]
data = c.recv(packetLen)
socketType = data[packetLen - 1]
if socketType == 1:
# ping
c.recv(2)
c.send(description)
try:
data = c.recv(10)
except:
return
c.send(data)
elif socketType == 2:
# join
c.recv(256)
c.send(joinError)
srv = socketserver.ThreadingTCPServer(ADDR, Handler)
try:
srv.serve_forever()
except KeyboardInterrupt:
pass