FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kbengine-httpserver/httpserver/app.py at master · kenny67/kbengine-httpserver · GitHub
kenny67
/
kbengine-httpserver
Public
forked from
asynchronoust/kbengine-httpserver
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kbengine-httpserver
/
httpserver
/
app.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
129 lines (96 loc) · 3.42 KB
Breadcrumbs
kbengine-httpserver
/
httpserver
/
app.py
Copy path
File metadata and controls
129 lines (96 loc) · 3.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# -*- coding: utf-8 -*-
"""
FileName: app
Author: Tao Hao
@contact: taohaohust@outlook.com
Created time: 2019/5/29
Description:
Changelog:
"""
from
httpserver
.
exceptions
import
MethodNotAllowed
import
json
from
httpserver
.
response
import
HTTPResponse
from
httpserver
.
router
import
Router
from
httpserver
.
server
import
Server
try
:
from
ujson
import
dumps
as
json_dumps
except
ImportError
:
json_dumps
=
json
.
dumps
class
Application
(
object
):
def
__init__
(
self
,
url_handler_list
):
self
.
router
=
None
self
.
server
=
None
self
.
create_router
(
url_handler_list
)
def
create_router
(
self
,
url_handler_list
):
if
self
.
router
:
raise
Exception
(
"route has exist"
)
self
.
router
=
Router
()
for
uri
,
handler
in
url_handler_list
:
if
not
issubclass
(
handler
,
RequestHandler
):
raise
Exception
(
"handler type is error"
)
self
.
router
.
add
(
uri
,
handler
)
def
run
(
self
,
host
,
port
,
backlog
=
128
):
self
.
server
=
Server
(
self
)
self
.
server
.
run
(
host
,
port
,
backlog
)
def
stop
(
self
):
self
.
server
.
stop
()
self
.
router
=
None
class
RequestHandler
(
object
):
def
__init__
(
self
,
request
):
self
.
request
=
request
def
execute_handler
(
self
,
*
args
,
**
kwargs
):
method
=
self
.
request
.
method
if
method
==
"HEAD"
:
self
.
head
(
*
args
,
**
kwargs
)
elif
method
==
"GET"
:
self
.
get
(
*
args
,
**
kwargs
)
elif
method
==
"POST"
:
self
.
post
(
*
args
,
**
kwargs
)
elif
method
==
"DELETE"
:
self
.
delete
(
*
args
,
**
kwargs
)
elif
method
==
"PATCH"
:
self
.
patch
(
*
args
,
**
kwargs
)
elif
method
==
"PUT"
:
self
.
put
(
*
args
,
**
kwargs
)
elif
method
==
"OPTIONS"
:
self
.
options
(
*
args
,
**
kwargs
)
def
head
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"HEAD"
)
def
get
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"GET"
)
def
post
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"POST"
)
def
delete
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"DELETE"
)
def
patch
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"PATCH"
)
def
put
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"PUT"
)
def
options
(
self
,
*
args
,
**
kwargs
):
raise
MethodNotAllowed
(
"OPTIONS"
)
def
write_text
(
self
,
text
,
status
=
200
,
headers
=
None
,
content_type
=
"text/plain; charset=utf-8"
):
http_response
=
HTTPResponse
(
body
=
text
,
status
=
status
,
headers
=
headers
,
content_type
=
content_type
)
self
.
request
.
connection
.
write_response
(
http_response
)
self
.
_cleanup
()
def
write_json
(
self
,
body
,
status
=
200
,
headers
=
None
,
content_type
=
"application/json"
,
dumps
=
json_dumps
):
http_response
=
HTTPResponse
(
body
=
dumps
(
body
),
status
=
status
,
headers
=
headers
,
content_type
=
content_type
)
self
.
request
.
connection
.
write_response
(
http_response
)
self
.
_cleanup
()
def
write_raw
(
self
,
body
,
status
=
200
,
headers
=
None
,
content_type
=
"application/octet-stream"
):
http_response
=
HTTPResponse
(
body_bytes
=
body
,
status
=
status
,
headers
=
headers
,
content_type
=
content_type
)
self
.
request
.
connection
.
write_response
(
http_response
)
self
.
_cleanup
()
def
_cleanup
(
self
):
self
.
request
=
None
Back
|
FazBrowse Home
|
New Git URL