简体   繁体   English

如何从客户端向服务器套接字发送消息

[英]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.

相关问题 如何在 web 套接字中从服务器向客户端发送消息 - How to send a message from the server to the client in a web socket 如何使用 Python(套接字)从客户端向客户端发送和接收消息? - How to send and receive message from client to client with Python (socket)? 如何在从客户端接收消息的同时从服务器向客户端发送连续消息(同时使用socket.listen和socket.send - how to send continuous message from server to client while receiving message from client(using socket.listen and socket.send at the same time 如何使用Flask-Socket IO从服务器向客户端发送消息 - How to send message from server to client using Flask-Socket IO 如何使用 Python 将数据从客户端套接字发送到服务器套接字? - How to send data from client socket to server socket using Python? 如何在python中将消息从客户端发送到服务器 - How to send a message from client to server in python python 套接字将数据从客户端发送到服务器 - python socket send data from client to server 如何将 RabbitMQ 队列连接到 Socket IO 服务器并同时从 Socket IO 服务器向客户端发送消息 - How to connect RabbitMQ Queue into Socket IO server and send messages from Socket IO server to client concurrently 如何使用套接字从服务器向客户端发送消息 - How to send a message from the server to a client using sockets Python:如何随时从服务器向客户端发送消息? - Python: How to send message to client from server at any time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM