Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Owo Sugiana
/
async-web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 8d28ba6e
authored
Mar 09, 2023
by
Owo Sugiana
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Penggunaan class Handler
1 parent
4047472c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
58 deletions
async_web/handlers/default_server.py
async_web/server.py
async_web/handlers/default_server.py
View file @
8d28ba6
...
...
@@ -6,7 +6,6 @@ keys = dict()
ERR_KEY
=
91
ERR_ACTION
=
76
ERR_OTHER
=
76
class
ErrKey
(
BaseError
):
...
...
@@ -19,51 +18,37 @@ class ErrAction(BaseError):
super
()
.
__init__
(
ERR_ACTION
,
message
)
def
ack
(
code
=
0
,
msg
=
'OK'
,
result
=
dict
()):
r
=
dict
(
result
)
r
[
'code'
]
=
code
r
[
'message'
]
=
msg
return
r
def
ack_from_exception
(
e
:
BaseError
):
return
ack
(
e
.
code
,
e
.
message
)
def
ack_other
(
msg
:
str
):
return
ack
(
ERR_OTHER
,
message
=
msg
)
def
login
(
d
:
dict
):
if
'key'
not
in
d
:
raise
ErrKey
(
'Field key tidak ditemukan'
)
if
d
[
'key'
]
not
in
keys
:
raise
ErrKey
(
'key tidak terdaftar'
)
client_id
=
keys
[
d
[
'key'
]]
return
dict
(
action
=
'login'
,
client_id
=
client_id
,
code
=
0
,
message
=
'OK'
)
def
logout
(
client_id
):
pass
def
log_message
(
ip
:
str
,
protocol
:
str
,
method
:
str
,
data
:
dict
,
client_id
=
None
):
pass
def
parse
(
d
:
dict
):
action
=
d
.
get
(
'action'
)
if
not
action
:
raise
ErrAction
(
'Field action tidak ditemukan'
)
if
action
==
'echo'
:
return
ack
()
msg
=
f
'ERROR action {action} tidak dipahami'
raise
ErrAction
(
msg
)
def
get_data
():
pass
class
Handler
:
def
__init__
(
self
,
ip
):
self
.
ip
=
ip
self
.
client_id
=
None
def
login
(
self
,
d
:
dict
):
if
'key'
not
in
d
:
raise
ErrKey
(
'Field key tidak ditemukan'
)
if
d
[
'key'
]
not
in
keys
:
raise
ErrKey
(
'key tidak terdaftar'
)
self
.
client_id
=
keys
[
d
[
'key'
]]
return
dict
(
action
=
'login'
,
client_id
=
self
.
client_id
,
code
=
0
,
message
=
'OK'
)
def
close
(
self
):
pass
def
save_message
(
self
,
method
,
data
):
pass
async
def
parse
(
self
,
d
:
dict
):
action
=
d
.
get
(
'action'
)
if
not
action
:
raise
ErrAction
(
'Field action tidak ditemukan'
)
if
action
==
'echo'
:
return
dict
(
action
=
'echo'
,
code
=
0
,
message
=
'OK'
)
msg
=
f
'ERROR action {action} tidak dipahami'
raise
ErrAction
(
msg
)
def
get_data
(
self
):
return
dict
()
def
init
(
conf
:
dict
):
...
...
async_web/server.py
View file @
8d28ba6
...
...
@@ -107,9 +107,9 @@ def main(argv=sys.argv[1:]):
def
log_unknown_error
():
log_info
(
exception_message
(),
log
.
error
)
def
log
_message
(
method
:
str
,
d
:
dict
):
def
save
_message
(
method
:
str
,
d
:
dict
):
try
:
module
.
log_message
(
ip
,
'websocket'
,
method
,
d
,
client_i
d
)
handler
.
save_message
(
method
,
d
)
except
Exception
:
log_unknown_error
()
...
...
@@ -119,7 +119,7 @@ def main(argv=sys.argv[1:]):
async
def
kirim_pesan
(
d
:
dict
):
text
=
json
.
dumps
(
d
)
log
_message
(
'send'
,
d
)
save
_message
(
'send'
,
d
)
d
=
{
'type'
:
'websocket.send'
,
'text'
:
text
}
await
kirim
(
d
)
...
...
@@ -131,7 +131,7 @@ def main(argv=sys.argv[1:]):
if
q
[
'status'
]
==
'send'
:
await
kirim_pesan
(
q
[
'data'
])
try
:
d
=
module
.
get_data
()
d
=
handler
.
get_data
()
if
d
:
await
kirim_pesan
(
d
)
except
Exception
:
...
...
@@ -140,7 +140,7 @@ def main(argv=sys.argv[1:]):
async
def
login
(
dc
:
dict
):
cid
=
None
try
:
dc
=
module
.
login
(
dc
)
dc
=
handler
.
login
(
dc
)
cid
=
dc
[
'client_id'
]
if
cid
in
ws_data
:
old_mem_id
=
mem_clients
[
cid
]
...
...
@@ -164,6 +164,7 @@ def main(argv=sys.argv[1:]):
ip
=
scope
[
'client'
][
0
]
mem_id
=
id
(
scope
)
client_id
=
None
handler
=
module
.
Handler
(
ip
)
start_time
=
time
()
while
True
:
if
not
client_id
and
time
()
-
start_time
>
login_timeout
:
...
...
@@ -187,7 +188,7 @@ def main(argv=sys.argv[1:]):
text
=
message
.
get
(
'text'
)
d
=
json
.
loads
(
text
)
log_info
(
f
'Decode JSON {d}'
)
log
_message
(
'receive'
,
d
)
save
_message
(
'receive'
,
d
)
if
first
:
first
=
False
client_id
=
await
login
(
d
)
...
...
@@ -195,7 +196,7 @@ def main(argv=sys.argv[1:]):
break
else
:
try
:
await
module
.
parse
(
d
)
await
handler
.
parse
(
d
)
except
BaseError
as
e
:
log_info
(
e
.
message
,
log
.
error
)
except
Exception
as
e
:
...
...
@@ -204,11 +205,10 @@ def main(argv=sys.argv[1:]):
if
client_id
in
ws_data
:
del
ws_data
[
client_id
]
break
if
client_id
:
try
:
module
.
logout
(
client_id
)
except
Exception
:
log_unknown_error
()
try
:
handler
.
close
()
except
Exception
:
log_unknown_error
()
log
.
info
(
f
"Listen {cf['ip']} port {cf['port']}"
)
uvicorn
.
run
(
app
,
host
=
cf
[
'ip'
],
port
=
int
(
cf
[
'port'
]),
log_level
=
'info'
)
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment