-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
60 lines (49 loc) · 1.74 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
import logging
from flask import Flask, request
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
logfile_handler = logging.FileHandler("server.log")
logger.addHandler(console_handler)
logger.addHandler(logfile_handler)
class HttpServer:
def __init__(self, **kwargs):
self.host = kwargs.get("host", "0.0.0.0")
self.port = kwargs.get("port", 8080)
@staticmethod
def server():
app = Flask(__name__)
@app.route("/")
def index():
return "Index"
@app.before_request
def before_request():
info = "\n------\n{} {}\n".format(request.method, request.full_path)
for header in request.headers:
info += "{}: {}\n".format(header[0], header[1])
info += "\n{}\n".format(request.get_data().decode(errors="ignore"))
logger.debug(info)
# @app.after_request
# def after_request(response):
# info ="{}\n".format(response.status)
# for header in response.headers:
# info += "{}: {}\n".format(header[0], header[1])
# info += "\n{}\n------".format(response.get_data().decode(errors="ignore"))
# logger.debug(info)
# return response
return app
def run(self):
app = self.server()
app.run(self.host, self.port)
class TCPServer:
def __init__(self, **kwargs):
self.host = kwargs.get("host", "0.0.0.0")
self.port = kwargs.get("port", 8080)
@staticmethod
def server():
pass
def run(self):
pass
if __name__ == "__main__":
http = HttpServer(port=8081)
http.run()