-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepoll1.py
More file actions
executable file
·239 lines (229 loc) · 8.4 KB
/
epoll1.py
File metadata and controls
executable file
·239 lines (229 loc) · 8.4 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#-*- coding:utf-8 -*-
import socket, select,ssl,urllib,os
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.load_cert_chain(certfile='cert.pem', keyfile='key.pem')
EOL1 = b'\n\n'
EOL2 = b'\n\r\n'
header = b'HTTP/1.0 200 OK\r\n'
header += b'Content-Type: text/html\r\nConnection:keep-alive\r\ncharset=UTF-8\r\n\r\n'
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('0.0.0.0', 8899))
serversocket.listen(1)
serversocket.setblocking(0)
epoll = select.epoll()
epoll.register(serversocket.fileno(), select.EPOLLIN)
cwd=os.getcwd()
##外部调用php或python文件等各种脚本文件
def dealphp(path,url,method,request,types):
if method=='GET':
if len(url.split('?'))>1:
can=url.split('?')[1]
cann=can.split('&')
a=[i.split('=')[1] for i in cann]
b=' '
i=0
while i<len(a):
b=b+' '+a[i]
i=i+1
if types=='py':
v=os.popen('python '+cwd+path+b)
elif types=='c':
os.system('gcc '+cwd+path+' -o '+cwd+path[0:-2])
v=os.popen(cwd+path[0:-2]+b).read()
os.system('rm '+cwd+path[0:-2]+' -f')
else:
v=os.popen(types+' '+cwd+path+b)
return header+v.read()
else:
if types=='py':
v=os.popen('python '+cwd+path)
elif types=='c':
os.system('gcc '+cwd+path+' -o '+cwd+path[0:-2])
v=os.popen(cwd+path[0:-2]).read()
os.system('rm '+cwd+path[0:-2]+' -f')
else:
v=os.popen(types+' '+cwd+path)
return v.read()
else:
po=request.split('\n\r\n')[1]
if po!='':
print "po"
cann=po.split('&')
print 'cann'
a=[i.split('=')[1] for i in cann]
print a
b=' '
i=0
while i<len(a):
b=b+' '+a[i]
i=i+1
print 'B'
print b
if types=='py':
v=os.popen('python '+cwd+path+b).read()
elif types=='c':
os.system('gcc '+cwd+path+' -o '+cwd+path[0:-2])
v=os.popen(cwd+path[0:-2]+b).read()
os.system('rm '+cwd+path[0:-2]+' -f')
else:
v=os.popen(types+' '+cwd+path+b).read()
v=urllib.unquote(v)
return header+v
##外部调用html文件
def dealhtml(path):
f=open(cwd+path,"rb")
y=f.read()
y=header+y
return y
##外部调用js和css文件
def dealjs(path):
f=open(cwd+path,"rb")
y=f.read()
f.close()
return y
##处理的到的请求
def dealresponse(request):
method=request.split(' ')[0]
url=request.split(' ')[1]
path=url.split('?')[0]
if method=='GET' or method=='POST':
if path=='/':
if method=='POST':
po=request.split('\n\r\n')[1]
print "po"
cann=po.split('&')
print 'cann'
a=[i.split('=')[1] for i in cann]
print a
b=' '
i=0
while i<len(a):
b=b+' '+a[i]
i=i+1
print b
# v=os.popen('python '+cwd+path+b).read()
# os.system('python sub.py'+b)
# v='liaotian'
# return v
f=open("index.html","rb")
x=header+f.read()
f.close()
return x
else:
if os.path.exists(cwd+path):
types=path.split('.')[-1]
b=' '
if len(url.split('?'))>1:
can=url.split('?')[1]
cann=can.split('&')
a=[i.split('=')[1] for i in cann]
i=0
while i<len(a):
b=b+' '+a[i]
i=i+1
if path=='/his.txt':
if a[0]!='undefined':
b=urllib.unquote(b)
v=os.system('python sub.py '+b)
f=open("his.txt","r")
v=f.read()
f.close()
return v
if types=='html':
return dealhtml(path)
elif types=='php' or types=='py' or types=='c' or types=='sh':
return dealphp(path,url,method,request,types)
elif (types=='js' or types=='css'):
return dealjs(path)
else:
f=open(cwd+path,"rb")
x=f.read()
f.close()
return x
else:
f=open("index.html","rb")
x=header+f.read()
f.close()
return x
################################################################################
try:
print serversocket.fileno()
connections = {}; requests = {}; responses = {};connstream={}
while True:
events = epoll.poll(1)
for fileno, event in events:
print events
if fileno == serversocket.fileno():
print ("a")
connection, address = serversocket.accept()
connection.setblocking(0)
print connection.fileno()
connection.setblocking(0)
epoll.register(connection.fileno(), select.EPOLLIN)
connstream[connection.fileno()] = context.wrap_socket(connection, server_side=True,do_handshake_on_connect=False)
# connstream[connection.fileno()].setblocking(0)
connections[connection.fileno()] = connection
requests[connection.fileno()] = b''
responses[connection.fileno()] = b''
elif event & select.EPOLLIN:
print("b")
while True:
try:
connstream[fileno].do_handshake()
requests[fileno] += connstream[fileno].recv(1024)
break
except ssl.SSLWantReadError:
#print("sslerror")
pass
except:
print("f")
epoll.modify(fileno,0)
break
print('yes')
if requests[fileno]=='':
epoll.unregister(fileno)
if EOL1 in requests[fileno] or EOL2 in requests[fileno]:
epoll.modify(fileno, select.EPOLLOUT)
print('-'*40 + '\n' + requests[fileno])
responses[fileno]=dealresponse(requests[fileno])
if responses[fileno]=='liaotian':
epoll.modify(fileno, 0)
elif event & select.EPOLLOUT:
print ("c")
while True:
try:
print ("try")
connstream[fileno].do_handshake()
byteswritten = connstream[fileno].send(responses[fileno])
responses[fileno] = responses[fileno][byteswritten:]
break
except ssl.SSLWantReadError:
print ("wanterror")
pass
except :
epoll.modify(fileno,0)
break
if len(responses[fileno]) == 0:
epoll.modify(fileno, 0)
try:
connstream[fileno].shutdown(socket.SHUT_RDWR)
except:
pass
try:
connections[fileno].shutdown(socket.SHUT_RDWR)
except:
pass
elif event & select.EPOLLHUP:
print ("d")
epoll.unregister(fileno)
connstream[fileno].close()
connections[fileno].close()
del connections[fileno]
del connstream[fileno]
finally:
print ("e")
epoll.unregister(serversocket.fileno())
epoll.close()
serversocket.close()
###############################################################################