我已经建立了一个从Stackoverflow收集的HTTPS多线程代码,看起来工作正常。
但是,在高峰时间在27秒内收到93个请求时,它停止工作。它停止了get_input_img
使用Websocket的类似Python3代码完全可以正常工作。
与此同时,我在syslog
中找到了此条目:
Aug 3 12:00:15 ubuntu-s-4vcpu-8gb-blr1-01 kernel: [131481.449818] TCP: request_sock_TCP: Possible SYN flooding on port 443. Sending cookies. Check SNMP counters.
在无响应后,我在日志文件中只能找到“已终止”。
它获取一些图像数据,然后将其写入临时文件中,并调用python模块,该模块返回一些代码,然后将其发送回客户端。
但是当那里有太多请求时,它就停止了。
怎么了?
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
import threading
import urllib.parse
import cgi
import tempfile
import base64
from common import *
from datetime import datetime
print( datetime.now());
gg_hashmap = getHash()
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
class PostHandler(BaseHTTPRequestHandler):
def handle(self):
try:
BaseHTTPRequestHandler.handle(self)
except :
pass
def do_POST(self):
try:
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
for field in form.keys():
field_item = form[field]
if field_item.filename:
file_data = field_item.file.read()
file_len = len(file_data)
del file_data
self.wfile.write('\tUploaded %s as "%s" (%d bytes)\n' % \
(field, field_item.filename, file_len))
else:
pass
if ('base64' in form and 'license' in form):
print("license=",form['license'].value);
global gg_hashmap
file_content = form['base64'].value
try:
_, temp_file_path = tempfile.mkstemp(prefix='photo', suffix='jpg')
with open(temp_file_path, 'wb') as w:
w.write(base64.b64decode (file_content))
input_hashes = get_input_img(temp_file_path)
all_letters = ""
if input_hashes != None:
for inp_hash in input_hashes:
lowest = 1000
lowest_letter = ''
for letter, arr in gg_hashmap.items():
for hashval in arr:
if int(inp_hash - hashval) < lowest:
lowest = int(inp_hash - hashval)
lowest_letter = letter
all_letters += lowest_letter
self.wfile.write(bytes(all_letters, "utf8"))
except Exception as e:
print("exception3 caught")
print(e)
print(str(e))
return
except:
print("caught unknown exception")
def do_GET(self):
self.send_response(200)
self.end_headers()
message = threading.currentThread().getName()
self.wfile.write(bytes(message,'utf-8'))
self.wfile.write('\n')
return
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
def run():
port=443
server = ThreadingSimpleServer(('0.0.0.0', port), PostHandler)
import ssl
server.socket = ssl.wrap_socket(server.socket, keyfile='./ssl/key.pem', certfile='./ssl/public.pem'
, ca_certs="./ssl/bund.ca-bundle" , server_side=True)
server.serve_forever()
if __name__ == '__main__':
run()