Skip to content

Commit 459e9b6

Browse files
committed
优化代码
1 parent c71a85a commit 459e9b6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

TCP-server.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
BS = 8192 # 接收数据缓冲大小
1515
ADDR = (HOST, PORT)
1616
glnr = "zabbix"
17+
COD = 'utf-8'
1718

1819
tcpSerSock = socket(AF_INET, SOCK_STREAM) # 创建TCP服务器套接字
1920
tcpSerSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
@@ -31,10 +32,10 @@
3132
try:
3233
data = tcpCliSock.recv(BS) # 连续接收指定字节的数据,接收到的是字节>数组
3334
if data: # 如果数据空白,则表示客户端退出,所以退出接收
34-
print("客户端发送的内容:",data.decode('utf-8'))
35+
print("客户端发送的内容:",data.decode(COD))
3536
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
36-
if glnr.encode('utf8') in data:
37-
dd = "./dingding.py {}".format(data.decode(encoding='UTF-8'), 'ignore')
37+
if glnr.encode(COD) in data:
38+
dd = "./dingding.py {}".format(data.decode(encoding='UTF-8'), errors='ignore')
3839
p = subprocess.call(dd, shell=True)
3940
if p == 0:
4041
time.sleep(4)
@@ -45,7 +46,7 @@
4546
else:
4647
print("忽略发送:{}".format(date))
4748
msg = "YES"
48-
tcpCliSock.send(msg.encode('utf8')) # 向客户端状态数据
49+
tcpCliSock.send(msg.encode(COD)) # 向客户端状态数据
4950
print("=" * 30)
5051
else:
5152
break

0 commit comments

Comments
 (0)