-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
230 lines (198 loc) · 7.85 KB
/
application.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
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
import time
import socket
import threading
import ssl
import asyncio
from subprocess import Popen, PIPE
from response import HttpResponse
from request import HttpRequest
# WSGI服务器
WEB_ROOT = "./webroot"
LOG_ROOT = "./webroot/log"
connectNum = 0
lock = threading.Lock()
class WSGIServer():
def __init__(self, host='0.0.0.0', port=14001, connectSize=10, enable_https=False):
'''
:param port: 服务器的端口号
:param connectSize: 默认的并发数量
'''
self.__host = host
self.__port = port
self.__connectSize = connectSize
self.enable_https = enable_https
if enable_https:
self.load_cert_chain(certfile='./webroot/cert/server.crt',
keyfile='./webroot/cert/server_nopass.key')
pass
def load_cert_chain(self, certfile, keyfile):
'''
:certfile: 证书公钥
:keyfile: 证书私钥
'''
self.__context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
self.__context.load_cert_chain(certfile, keyfile)
async def __server(self):
global connectNum
global lock
'''
服务的主要实现
:return:
'''
server = None
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((self.__host, self.__port))
server.listen(self.__connectSize)
print("======服务器启动成功:https://" +
self.__host + ":" + str(self.__port)+"======")
if self.enable_https:
with self.__context.wrap_socket(server, server_side=True) as ssl_server:
while True:
try:
if connectNum < self.__connectSize:
clientConn, clientAddr = ssl_server.accept() # 等待客户端请求
# 启动独立的线程,处理每一次用户请求
wt = WorkCoroutine(clientConn, clientAddr)
asyncio.ensure_future(wt.handle())
# 交出执行权!!
await asyncio.sleep(0)
print('handle a client. connect number:',
connectNum)
else:
clientConn, clientAddr = ssl_server.accept() # 等待客户端请求
# 启动独立的线程,处理每一次用户请求
wt = WorkCoroutine(clientConn, clientAddr)
asyncio.ensure_future(wt.handle("Full"))
# 交出执行权!!
await asyncio.sleep(0)
print(
'client list full. connect number:', connectNum)
except Exception:
pass
else:
while True:
clientConn, clientAddr = server.accept()
lock.acquire()
connectNum += 1
lock.release()
if connectNum < self.__connectSize:
# 启动独立的线程,处理每一次用户请求
wt = WorkCoroutine(clientConn, clientAddr)
asyncio.ensure_future(wt.handle())
# 交出执行权!!
await asyncio.sleep(0)
print('handle a client. connect number:', connectNum)
else:
# 达到最大协程数
wt = WorkCoroutine(clientConn, clientAddr)
asyncio.ensure_future(wt.handle("Full"))
# 交出执行权!!
await asyncio.sleep(0)
print('client list full. connect number:', connectNum)
except Exception as e:
print(str(e.args))
finally:
if server:
server.close()
def startServer(self):
'''
启动异步服务
:return:
'''
asyncio.run(self.__server())
class WorkCoroutine:
def __init__(self, connection, addr, bufferSize=8096):
self.__connection = connection
self.__addr = addr
self.__bufferSize = bufferSize
pass
async def handle(self, msg=""):
global connectNum
global lock
try:
# 1.解析请求
receiveMsg = self.__connection.recv(self.__bufferSize)
receiveMsg = receiveMsg.decode("utf-8")
self.log(self.__addr, receiveMsg) # 记录日志
print(receiveMsg)
request = HttpRequest()
request = request.parseRequest(receiveMsg)
response = HttpResponse(self.__connection)
# 达到最大协程,直接返回
if msg == "Full":
response = HttpResponse(self.__connection)
response.setCode(404)
response.setData_From_Url(WEB_ROOT+'/ERROR.html')
response.response()
lock.acquire()
connectNum -= 1
lock.release()
return
if request == {}:
response.setCode(404)
response.setData_From_Url(WEB_ROOT+"/404.html")
response.response()
lock.acquire()
connectNum -= 1
lock.release()
return
# 2.url
if(request['url'] == '/'):
request['url'] = '/index.html'
elif(request['url'] == '/favicon.ico'):
request['url'] = '/pic/favicon.ico'
url = request['url']
# 3.method
responseText = ""
if request['method'] == "GET":
try:
response.setCode(200)
response.setData_From_Url(WEB_ROOT+url)
except Exception:
response.setCode(404)
response.setData_From_Url(WEB_ROOT+"/404.html")
elif request['method'] == "POST":
if url[0:8] == "/cgi-bin":
appName = url.split("/")[2]
argvList = []
for k in request['params']:
argvList.append(request['params'][k])
cmd = ["python", WEB_ROOT+"/cgi-bin/{}.py".format(appName)]
for a in argvList:
cmd.append(a)
cmd.append(request['body'])
process = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
response.setCode(200)
response.setData(stdout)
else:
response.setCode(404)
response.setData_From_Url(WEB_ROOT+"/404.html")
elif request['method'] == "HEAD":
pass
else:
pass
# 4.response
response.response()
except Exception as e:
print(str(e.args))
lock.acquire()
connectNum -= 1
lock.release()
return
def log(self, addr, data):
date = time.strftime("%Y-%m-%d")
path = LOG_ROOT+'/' + date+'.log'
date = time.strftime("%Y-%m-%d %H:%M:%S")
d = data.replace('\r\n', ' ')
lock.acquire()
s = "[\'" + date + "\'] " + \
str(addr) + "(connNum:"+str(connectNum)+") " + d+"\n"
lock.release()
f = open(path, 'a')
f.write(s)
f.close()