2024-03-09 14:28:24 +08:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
# 多线程TCP服务器
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
import pyautogui
|
|
|
|
|
import socket
|
|
|
|
|
import threading
|
|
|
|
|
|
2024-03-09 14:36:35 +08:00
|
|
|
|
from black import black_list
|
|
|
|
|
|
2024-03-09 14:28:24 +08:00
|
|
|
|
|
|
|
|
|
def click_it(x, y):
|
|
|
|
|
pyautogui.moveTo(x, y)
|
|
|
|
|
pyautogui.click(x, y)
|
|
|
|
|
|
|
|
|
|
|
2024-03-09 14:47:48 +08:00
|
|
|
|
def is_black(black):
|
2024-03-09 15:10:58 +08:00
|
|
|
|
for v in black_list:
|
|
|
|
|
if dict_equal(v, black):
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dict_equal(dict1, dict2):
|
2024-03-09 15:15:42 +08:00
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
if len(dict2) != len(dict1):
|
2024-03-09 15:10:58 +08:00
|
|
|
|
return False
|
2024-03-09 15:15:42 +08:00
|
|
|
|
for k, v1 in dict1.items():
|
|
|
|
|
v2 = dict2.get(k)
|
|
|
|
|
if v1 != v2:
|
2024-03-09 15:10:58 +08:00
|
|
|
|
return False
|
2024-03-09 15:15:42 +08:00
|
|
|
|
if k == "Point":
|
|
|
|
|
if len(v1[0]) != len(v2[0]):
|
|
|
|
|
return False
|
|
|
|
|
except:
|
|
|
|
|
return False
|
2024-03-09 14:36:35 +08:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2024-03-09 14:28:24 +08:00
|
|
|
|
bind_ip = "127.0.0.1" # 监听的IP 地址
|
|
|
|
|
bind_port = 9999 # 监听的端口
|
|
|
|
|
|
|
|
|
|
# 建立一个socket对象
|
|
|
|
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
|
|
# 绑定监听的地址,创建的对象是AF_INET的形式,所以地址(ip, port)的元组形式来表示
|
|
|
|
|
server.bind((bind_ip, bind_port))
|
|
|
|
|
|
|
|
|
|
# 启动监听并设置连接数为5
|
|
|
|
|
server.listen(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 这是客户处理线程,也是一个回调函数,创建一个新的进程对象,将客户端套接字对象作为一个句柄传递给它。
|
|
|
|
|
def handle_client(client_socket):
|
|
|
|
|
# 打印处客户端发送得到的内容
|
|
|
|
|
data = client_socket.recv(1024)
|
|
|
|
|
try:
|
|
|
|
|
data = data.decode("utf-8")
|
2024-03-09 14:47:48 +08:00
|
|
|
|
data = json.loads(data)
|
|
|
|
|
print(data)
|
2024-03-09 15:10:58 +08:00
|
|
|
|
if is_black(data):
|
|
|
|
|
print("黑名单")
|
|
|
|
|
client_socket.send(b"ACK!")
|
|
|
|
|
client_socket.close()
|
|
|
|
|
return
|
2024-03-09 14:47:48 +08:00
|
|
|
|
Point = data["Point"]
|
2024-03-09 14:51:26 +08:00
|
|
|
|
x = Point[0]["X"]
|
|
|
|
|
y = Point[0]["Y"]
|
2024-03-09 14:31:07 +08:00
|
|
|
|
click_it(x, y)
|
2024-03-09 14:28:24 +08:00
|
|
|
|
except Exception as e:
|
2024-03-09 14:36:35 +08:00
|
|
|
|
pass
|
2024-03-09 15:10:58 +08:00
|
|
|
|
client_socket.send(b"ACK!")
|
|
|
|
|
client_socket.close()
|
2024-03-09 14:28:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 等待连接,这里必定进入循环
|
|
|
|
|
while True:
|
|
|
|
|
# 一个客户端成功建立连接的时候,我们将收到的客户端套接字对象保存到client变量中,将远程连接的细节保存到addr变量中。
|
|
|
|
|
# 返回的client是一个套接字,是表示专属客户的一个新的套接字 | 而addr则是一个tuple元组,抓了某次数据返回的就是('127.0.0.1', 62549)
|
|
|
|
|
# 运行到下面就被当作套接字传递给上面自定义的 handle_client 函数
|
|
|
|
|
client, addr = server.accept()
|
|
|
|
|
|
|
|
|
|
# 当调用start函数时,就回去执行这个函数
|
|
|
|
|
client_handler = threading.Thread(target=handle_client, args=(client,))
|
|
|
|
|
|
|
|
|
|
# 启动线程开始处理客户端连接。handle_client 函数执行recv()函数之后将一段信息发送给客户端
|
|
|
|
|
client_handler.start()
|