展开全部
python3,使用socket在两台电脑之2113间通信,使用python pyautogui模块5261实现按键。如果不在一个局域网,需要公4102网服务器作做转发1653。需要环境安装环境依赖:# ubuntu
$ sudo apt-get install python3-pip
$ sudo pip3 install python3-xlib
$ sudo apt-get install scrot
$ sudo apt-get install python3-tk
$ sudo apt-get install python3-dev
# mac os
$ sudo pip3 install pyobjc-framework-Quartz
$ sudo pip3 install pyobjc-core
$ sudo pip3 install pyobjc
$ sudo pip3 install pillow
windows不需要安装依赖。安装依赖之后,安装python 包pip3 install pyautogui
以下在python3中简单实现。
server.py (被控制端)import pyautogui
import time
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
tcp_server_socket = socket(AF_INET, SOCK_STREAM)
address = ('', 8000)
tcp_server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
tcp_server_socket.bind(address)
tcp_server_socket.listen(128)
def handle_key(key):
key = key.decode()
if key == "alt":
print("按下alt键盘")
pyautogui.press("alt")
elif key == "enter":
time.sleep(5)
pyautogui.press("enter")
print("按下enter键盘")
elif key == "win":
print("按下窗口键")
pyautogui.hotkey("winleft")
else:
print("按下其它键")
time.sleep(5)
pyautogui.typewrite(key)
while True:
print("等待连接")
tcp_client_socket,addr = tcp_server_socket.accept()
print('...connected from', addr)
while True:
data = tcp_client_socket.recv(1024)
if not data:
break
try:
if data.decode().startswith('exit'):
break
except Exception as e:
print(e)
break
print(data)
handle_key(data)
tcp_client_socket.send('server recved.'.encode())
tcp_client_socket.close()
client.py (发送控制指令端)import socket
HOST = 'localhost'
PORT = 8000
BUFSIZE = 1024
ADDR=(HOST, PORT)
tcp_client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(ADDR)
FLAG = True
while True:
data = input('>')
if not data:
break
tcp_client.send(data.encode())
data=tcp_client.recv(BUFSIZE)
if not data:
FLAG = False
break
print(data)
tcp_client.close()