-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
89 lines (64 loc) · 1.56 KB
/
server.py
File metadata and controls
89 lines (64 loc) · 1.56 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
import socket
import os
import _thread as thread
import time
# settings
python_cmd = 'python'
exit_cmd = 'exit()'
address = ('127.0.0.1', 8888)
# socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
print(s)
print("Server Started!")
s.listen(10)
# thread
def client_accept():
while True:
print("Listening...")
try:
conn, addr = s.accept()
print('[+] Connected with', addr)
thread.start_new_thread(client_handle, (conn, addr))
except:
break
def client_handle(conn, addr):
while True:
try:
data = conn.recv(4096)
data = data.decode('utf-8')
except:
break
if not data:
break
# exit cmd
if(data == exit_cmd):
break
cmd = python_cmd + " " + data
print(addr, '[CMD]', cmd)
# run a command
try:
f = os.popen(cmd, 'r')
send = f.read()
f.close()
if(len(send) == 0):
print("Result:")
print("[NULL]")
conn.sendall("[NULL]".encode('utf-8'))
else:
print("Result:")
print(send)
conn.sendall(send.encode('utf-8'))
except:
break
conn.close()
print('[-] Disconnected with', addr)
print()
print()
thread.start_new_thread(client_accept, ())
while True:
cmd = input()
if(cmd == exit_cmd):
break
s.close()
print("Server Stopped!")