forked from asynchronoust/kbengine-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (60 loc) · 1.97 KB
/
server.py
File metadata and controls
75 lines (60 loc) · 1.97 KB
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
# -*- coding: utf-8 -*-
"""
FileName: server
Author: Tao Hao
@contact: [email protected]
Created time: 2019/5/29
Description:
Changelog:
"""
import KBEngine
from KBEDebug import *
import socket
from httpserver.connection import Connection
class Server(object):
def __init__(self, app):
self._socket = None
self._host = None
self._port = None
self._fd = 0
self.app = app
def run(self, host, port, backlog=128):
INFO_MSG("httpserver run, host[%s], post[%s]..." % (host, port))
try:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
ERROR_MSG("server run socket error: %s" % str(e))
raise
self._socket.setblocking(False)
self._socket.bind((host, port))
self._socket.listen(backlog)
self._set_server_fd()
self._host = host
self._port = port
KBEngine.registerReadFileDescriptor(self._fd, self.accept)
def _set_server_fd(self):
self._fd = self._socket.fileno()
def accept(self, fd):
if fd != self._fd:
ERROR_MSG("Server::accept, fd[%s] is not _fd[%s]" % (fd, self._fd))
return
# addr = (hostaddr, port)
sock, addr = self._socket.accept()
# 设置为unblocking
sock.setblocking(False)
socket_fd = sock.fileno()
new_conn = Connection(sock, addr, self.app)
DEBUG_MSG("Server::accept, new connection[%s], addr[%s], fd[%s]" %
(id(new_conn), addr, socket_fd))
KBEngine.registerReadFileDescriptor(
socket_fd, new_conn.data_received
)
def stop(self):
if self._socket:
INFO_MSG(
"httpserver stop host[%s], port[%s]" % (self._host, self._port)
)
KBEngine.deregisterReadFileDescriptor(self._fd)
self._socket.close()
self._socket = None
self.app = None