Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
11
README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Python Network Communication | ||
Python Implementation of Network Communication | ||
|
||
The tools is written using Python3 | ||
|
||
|
||
## Requirements | ||
- Kivy | ||
- kivymd | ||
- psutil | ||
|
110
client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# 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) | ||
|
||
|
BIN
+4.13 KB
home.cpython-37.pyc
Binary file not shown.
158
home.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
from kivy.lang import Builder | ||
from kivy.uix.screenmanager import Screen | ||
from kivy.properties import ObjectProperty,StringProperty | ||
from kivymd.uix.boxlayout import MDBoxLayout | ||
from kivy.clock import mainthread | ||
Builder.load_string(''' | ||
<Home>: | ||
name:'home' | ||
BoxLayout: | ||
orientation:'vertical' | ||
padding:dp(20) | ||
spacing:dp(20) | ||
MDLabel: | ||
text: ' Python Network Connection Services' | ||
font_style:'H4' | ||
adaptive_height:True | ||
halign:'center' | ||
MDBoxLayout: | ||
adaptive_height:True | ||
size_hint_y:None | ||
height:dp(60) | ||
Button: | ||
text:'Home' | ||
id:home | ||
background_color:0,0,1,1 | ||
on_release:app.go('home',self) | ||
Button: | ||
text:'Responses' | ||
background_color:.5,.5,.5,.5 | ||
on_release:app.go('responses',self) | ||
id:res | ||
Button: | ||
text:'Logs' | ||
background_color:.5,.5,.5,.5 | ||
on_release:app.go('logs',self) | ||
id:log | ||
ScreenManager: | ||
id:mngr | ||
Screen: | ||
name:'home' | ||
MDBoxLayout: | ||
orientation:'vertical' | ||
MDLabel: | ||
text:'Devices' | ||
adaptive_height:True | ||
halign:'center' | ||
font_style:'H5' | ||
GridLayout: | ||
cols:4 | ||
id:grid | ||
MDLabel: | ||
text:'Apply Commands:' | ||
adaptive_height:True | ||
halign:'center' | ||
font_style:'H5' | ||
Widget: | ||
size_hint_y:.2 | ||
BoxLayout: | ||
size_hint_y:None | ||
height:dp(80) | ||
Button: | ||
text:'Shutdown Client' | ||
on_release:app.sendCommand('Close') | ||
Button: | ||
text:'Get User' | ||
on_release:app.sendCommand('Users') | ||
Button: | ||
text:'Get Client Device Info' | ||
on_release:app.sendCommand('Info') | ||
Screen: | ||
name:'logs' | ||
MDBoxLayout: | ||
orientation:'vertical' | ||
MDBoxLayout: | ||
adaptive_height:True | ||
Label: | ||
adaptive_height:True | ||
MDIconButton: | ||
icon:'delete' | ||
pos_hint:{'right':1} | ||
on_release:logs.text = '' | ||
MDBoxLayout: | ||
md_bg_color:.5,.5,.5,.8 | ||
ScrollView: | ||
MDLabel: | ||
id:logs | ||
adaptive_height:True | ||
font_size:dp(14) | ||
markup:True | ||
Screen: | ||
name:'responses' | ||
MDBoxLayout: | ||
orientation:'vertical' | ||
MDBoxLayout: | ||
adaptive_height:True | ||
Label: | ||
adaptive_height:True | ||
MDIconButton: | ||
icon:'delete' | ||
pos_hint:{'right':1} | ||
on_release:res.text = '' | ||
MDBoxLayout: | ||
md_bg_color:.5,.5,.5,.8 | ||
ScrollView: | ||
MDLabel: | ||
id:res | ||
adaptive_height:True | ||
font_size:dp(14) | ||
markup:True | ||
<MyCheckbox>: | ||
spacing:dp(10) | ||
adaptive_height:True | ||
MDCheckbox: | ||
size_hint: None, None | ||
size: "48dp", "48dp" | ||
pos_hint: {'center_x': .5, 'center_y': .5} | ||
on_active:app.on_checkbox_active(root,*args) | ||
MDLabel: | ||
text:root.text | ||
adaptive_height:True | ||
pos_hint: {'center_x': .5, 'center_y': .5} | ||
''') | ||
class MyCheckbox(MDBoxLayout): | ||
text = StringProperty('') | ||
socket = ObjectProperty() | ||
class Home(Screen): | ||
@mainthread | ||
def addLogs(self,txt): | ||
self.ids.logs.text += txt | ||
@mainthread | ||
def addRes(self,txt): | ||
self.ids.res.text += txt | ||
|
||
if __name__ == '__main__': | ||
from kivymd.app import MDApp | ||
class HomeApp(MDApp): | ||
active = ObjectProperty() | ||
def build(self): | ||
return Home() | ||
|
||
|
||
|
||
|
||
HomeApp().run() |
Oops, something went wrong.