-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·125 lines (108 loc) · 2.99 KB
/
client.py
File metadata and controls
executable file
·125 lines (108 loc) · 2.99 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#Imports
import socket
import time
import random
import subprocess
#Variables
lHost = "127.0.0.1" #Server IP
port = 4711 #Connection Port
filename_inc = ""
filename_out = ""
#Functions
def send(msg):
s.send(msg.encode("UTF-8"))
print("Sent: " + msg)
def sendFile(filename):
f = open(filename,'rb')
print('Sending...')
send("$")
l = f.read(1024)
while (l):
print('Sending...')
s.send(l)
l = f.read(1024)
f.close()
send("!")
print("Done Sending")
#s.shutdown(socket.SHUT_WR)
#print s.recv(1024)
def getFile(filename):
f = open(filename,'wb')
start_ctrl = s.recv(1)
if start_ctrl == "$":
print("Receiving...")
l = s.recv(1024)
run = True
while (run):
print("Receiving...")
f.write(l)
l = s.recv(1024)
if l[-1:] == "!" or l == "":
print("Done Receiving")
run = False
f.close()
print("File saved!")
#clientsocket.send('Thank you for connecting')
def getInstructions():
connected = True
while (connected):
msg = s.recv(4096)
inst = msg.decode("UTF-8")
#Instructions
if inst == "test":
try:
print("REC: test")
send("[OK]Test works!")
except:
pass
elif inst == "ping":
try:
print("REC: ping")
send("pong")
except:
pass
elif inst[0:8] == "sendFile":
print("REC: sendFile")
try:
print(inst[9:])
filename_out = inst[9:]
print("sending file..")
sleepTime = 1
time.sleep(sleepTime)
sendFile(filename_out)
except:
pass
elif inst[0:7] == "getFile":
print("REC: getFile")
try:
print(inst[8:])
filename_inc = inst[8:]
print("receiving file..")
getFile(filename_inc)
except:
pass
elif inst == "executeTop":
print("REC: executeTop")
try:
print("executing top...")
subprocess.Popen("top", shell=True)
except:
pass
elif inst == "executeFile":
print("REC: executeFile")
try:
print("executing file...")
subprocess.Popen("./client.py", shell=True)
except:
pass
elif inst == "":
connected = False
else:
print("wrong command:")
print(msg)
send("WC")
#Connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
#host = lHost
conn