写一个远程控制代码,要求,多并发,实现功能,打开文件,关闭文件,上传文件,下载文件,远程监控,
时间: 2024-05-14 14:16:18 浏览: 10
以下是一个简单的远程控制代码,实现了打开、关闭、上传、下载文件,以及远程监控的功能。代码使用 Python 3 编写,使用了 socket 和 threading 库。请注意,此代码仅供学习和参考,不应用于非法用途。
服务端代码:
```python
import socket
import threading
import os
HOST = '0.0.0.0'
PORT = 8888
def handle_client(conn, addr):
print(f'Connected by {addr}')
while True:
data = conn.recv(1024).decode('utf-8')
if not data:
break
cmd, *args = data.split()
if cmd == 'open':
try:
os.startfile(args[0])
conn.send('Opened\n'.encode('utf-8'))
except:
conn.send('Failed\n'.encode('utf-8'))
elif cmd == 'close':
try:
os.system(f'taskkill /f /im {args[0]}')
conn.send('Closed\n'.encode('utf-8'))
except:
conn.send('Failed\n'.encode('utf-8'))
elif cmd == 'upload':
try:
with open(args[1], 'wb') as f:
f.write(conn.recv(1024))
conn.send('Uploaded\n'.encode('utf-8'))
except:
conn.send('Failed\n'.encode('utf-8'))
elif cmd == 'download':
try:
with open(args[0], 'rb') as f:
conn.sendall(f.read())
conn.send('Downloaded\n'.encode('utf-8'))
except:
conn.send('Failed\n'.encode('utf-8'))
elif cmd == 'monitor':
try:
while True:
data = conn.recv(1024)
if not data:
break
print(f'{addr}: {data.decode("utf-8")}')
except:
pass
conn.close()
print(f'Connection closed by {addr}')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f'Server started at {HOST}:{PORT}')
while True:
conn, addr = s.accept()
t = threading.Thread(target=handle_client, args=(conn, addr))
t.start()
```
客户端代码:
```python
import socket
HOST = '127.0.0.1'
PORT = 8888
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
cmd = input('> ').strip()
if not cmd:
continue
if cmd == 'exit':
break
s.send(cmd.encode('utf-8'))
data = s.recv(1024).decode('utf-8')
print(data)
if cmd.startswith('download') and data == 'Downloaded\n':
with open(cmd.split()[1], 'wb') as f:
while True:
data = s.recv(1024)
if not data:
break
f.write(data)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)