Skip to content
Permalink
b6f72ecdfe
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
110 lines (90 sloc) 2.49 KB
# imports
import socket
from threading import Thread
import struct, psutil
import subprocess
# The Kivy Design for user interface
# The Client Class
class Client:
def __init__(self,host,port):
'''
host -> String Server Address
port -> integer Server Port
'''
self.socket = socket.socket()
self.host = host
self.port = port
self.connect()
def getSysInfo(self):
Id = subprocess.check_output(['systeminfo']).decode('utf-8').split('\n')
new = []
# arrange the string into clear info
for item in Id:
new.append(str(item.split("\r")[:-1]))
result = ''
for i in new:
result += (i[2:-2] + '\n ')
return result
def connect(self):
'''
Connect the Client to the Server
'''
print('connecting to server')
try:
self.socket.connect((self.host,int(self.port)))
print('Client Connected')
print('waiting for commands from the controller')
#Thread(target=self.getCommand).start()
self.getCommand()
except Exception as e:
print('Error Connnecting to Server', e)
def recvall(self,sock, size):
msg = b""
while len(msg) < size:
packet = sock.recv(size - len(msg))
if not packet:
return None
msg += packet
return msg
def getUsers(self):
users = []
user_list = psutil.users()
for user in user_list:
users.append(user.name)
return ' '+str(users)
def getCommand(self):
while True:
try:
size = struct.unpack("I", self.recvall(self.socket, struct.calcsize("I")))[0]
data = self.recvall(self.socket, size)
if data.decode():
print(f'Command Received: {data.decode()}')
if data.decode() == 'Info':
msg = self.getSysInfo().encode()
size = len(msg)
self.socket.sendall(struct.pack("I", size))
self.socket.sendall(msg)
print('Respond Sent')
if data.decode() == 'Close':
msg = ' Shuttingdown Device'.encode()
size = len(msg)
self.socket.sendall(struct.pack("I", size))
self.socket.sendall(msg)
self.socket.close()
print('Respond Sent')
break
if data.decode() == 'Users':
msg = self.getUsers().encode()
size = len(msg)
self.socket.sendall(struct.pack("I", size))
self.socket.sendall(msg)
print('Respond Sent')
except:
import traceback
trace = traceback.format_exc()
print(trace)
break
if __name__ == '__main__':
host = input('Host: ')
port = input('Port: ')
c = Client(host,port)