如何检查是否有可移动媒体连接到远程主机

如何检查是否有可移动媒体连接到远程主机

我需要检查是否有任何 USB 或可移动媒体连接到我的网络或子网。我现在正在做的事情是,我制作了一个简单的客户端/服务器模型,通过运行 lsusb 或任何其他命令,向彼此发送我想要的客户端 USB 信息的消息,然后它会将命令输出发送到服务器,我如何实现此目标或有没有更好的方法来做到这一点。我使用 Linux 作为客户端/服务器。这是我的客户端代码。我对其进行了更改,以便客户端将其日志发送到服务器。

import socket
import threading
import sys

#Wait for incoming data from server
#.decode is used to turn the message in bytes to a string
def receive(socket, signal):
    while signal:
        try:
            data = socket.recv(32)
            print(str(data.decode("utf-8")))
        except:
            print("You have been disconnected from the server")
            signal = False
            break

#Get host and port
host = input("Host: ")
port = int(input("Port: "))

#Attempt connection to server
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
except:
    print("Could not make a connection to the server")
    input("Press enter to quit")
    sys.exit(0)

#Create new thread to wait for data
receiveThread = threading.Thread(target = receive, args = (sock, True))
receiveThread.start()

#Send data to server
#str.encode is used to turn the string message into bytes so it can be sent across the network
while True:
    message = input()
    sock.sendall(str.encode(message))

相关内容