[英]how to send message from client to server socket
hi i am trying to detect if usb connect to client side.嗨,我正在尝试检测 USB 是否连接到客户端。 what my logic is that to get the total number of disks if new disk added means new devices is added on client if usb remove from client means devices is remove from client so i keep checking in loop the client show total = 1 or total = 2 , 1 means only 1 disk is available 2 means new device is added and so on.
我的逻辑是,如果添加新磁盘意味着在客户端上添加新设备,如果从客户端移除 USB 意味着从客户端移除设备,则获取磁盘总数,所以我一直在循环检查客户端显示总数 = 1 或总数 = 2 , 1 表示只有 1 个磁盘可用 2 表示添加了新设备,依此类推。 It working fine but the
sock.send("Device Added in " + username) sock.send("Device remove in " + username)
these command not working i dont get these message on server side if i plug or plug out the usb from client side.它工作正常,但
sock.send("Device Added in " + username) sock.send("Device remove in " + username)
这些命令不起作用如果我插入或拔出 USB,我不会在服务器端收到这些消息客户端。 i am not sure if my if else
conditions are correct or not.我不确定我的
if else
条件是否正确。
os linux
OS
linux
this is client side code这是客户端代码
from socket import *
import os, string, time
import getpass
host = 'localhost'
port = 52000
username = getpass.getuser()
sock = socket()
# Connecting to socket
sock.connect((host, port)) # Connect takes tuple of host and port
def detect_device(previous):
total = os.system(' lsblk | grep disk | wc -l')
time.sleep(3)
# if conditon if new device add
if total<previous:
sock.send("Device Added in " + username)
# if no new device add or remove
elif total==previous:
detect_device(previous)
# if device remove
else:
sock.send("Device Removed in " + username)
# Infinite loop to keep client running.
while True:
data = sock.recv(1024)
if (data == 'Hi'):
while True:
detect_device(os.system(' lsblk | grep disk | wc -l'))
sock.close()
output i get on client side is 1 1 1 2 2 2 2 1 1
我在客户端得到的输出是
1 1 1 2 2 2 2 1 1
1 means current number of total disks are 1 if i plug usb then total number of disks is 2 then 1 means i disconnect USB 1 表示当前磁盘总数为 1 如果我插入 USB 则磁盘总数为 2 那么 1 表示我断开 USB
好的,我修复了 os.system 在输出中返回 0,所以我用 subprocess.run 尝试它,它运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.