-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·166 lines (145 loc) · 4.61 KB
/
server.py
File metadata and controls
executable file
·166 lines (145 loc) · 4.61 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#imports
import socket
import os
import time
import sys
#Variables
port = 4711
lHost = "127.0.0.1"
#Functions
def clear():
os.system('cls' if os.name=='nt' else 'clear')
def send(msg):
msg = str(msg)
clientsocket.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...')
clientsocket.send(l)
l = f.read(1024)
f.close()
send("!")
print("Done Sending")
#s.shutdown(socket.SHUT_WR)
def getFile(filename):
f = open(filename,'wb')
start_ctrl = clientsocket.recv(1)
if start_ctrl == "$":
print("Receiving...")
l = clientsocket.recv(1024)
run = True
while (run):
print("Receiving...")
f.write(l)
l = clientsocket.recv(1024)
if l[-1:] == "!" or l == "":
print("Done Receiving")
run = False
f.close()
print("File saved!")
#clientsocket.send('Thank you for connecting')
#Starting Server
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = socket.gethostname()
#host = lHost
serversocket.bind((host, port))
serversocket.listen(1)
clear()
print("-:-:-:-:-: Server:-:-:-:-:-")
clientsocket, addr = serversocket.accept()
print("Connection from: " + str(addr))
mode = "normal"
filename = ""
filename = ""
while True:
print("Mode: " + mode)
if mode == "normal":
#msg = str.strip(raw_input("Your Instruction: "))
msg = str.strip(raw_input("Your Instruction: "))
if msg == "help":
clear()
print("-+-+-+-+-+HELP+-+-+-+-+-")
print("Test Connection: 'test'")
raw_input("\nPress ENTER to continue")
clear()
print("-:-:-:-:-:Server:-:-:-:-:-")
elif msg == "exit":
mode = "exit"
else:
msg = msg.encode("UTF-8")
mode == "normal"
#send(msg)
#clientsocket.shutdown(socket.SHUT_WR)
print(msg[12:])
if msg[0:11] == "getFileSize":
print(msg[12:])
filename = msg[12:]
send("getFileSize " + filename)
mode = "filesize"
elif msg[0:7] == "getFile":
filename = msg[8:]
print(filename)
send("sendFile " + filename)
mode = "getFile"
print("switched mode to " + mode)
elif msg[0:8] == "sendFile":
filename = msg[9:]
print(filename)
send("getFile " + filename)
mode = "sendFile"
print("switched mode to " + mode)
elif msg == "executeFile":
print(msg[12:])
file = msg[12:]
send("executeFile " + file)
mode = "exef"
elif msg[0:10] == "executeCmd":
print(msg[11:])
cmd = msg[11:]
send("executeCmd " + cmd)
mode = "exec"
else:
send(msg)
answer = clientsocket.recv(4096)
if answer == "WC":
print("REC: WC (Wrong Command)")
else:
print(answer.decode("UTF-8"))
elif mode == "getFile":
getFile(filename);
mode = "normal"
print("switched mode to " + mode)
elif mode == "sendFile":
sleepTime = 1
time.sleep(sleepTime)
sendFile(filename)
mode = "normal"
print("switched mode to " + mode)
elif mode == "exit":
clientsocket.shutdown(socket.SHUT_RDWR)
serversocket.shutdown(socket.SHUT_RDWR)
clientsocket.close()
serversocket.close()
sys.exit()
mode = "normal"
print("switched mode to " + mode)
elif mode == "exec" or mode == "exef":
msg = clientsocket.recv(4096)
print(msg)
mode = "normal"
print("switched mode to " + mode)
elif mode == "filesize":
msg = clientsocket.recv(4096)
print("filesize: " + msg)
mode = "normal"
print("switched mode to " + mode)
else:
msg = clientsocket.recv(4096)
mode = "normal"
print("switched mode to " + mode)