Changed creation and made stoppable
This commit is contained in:
parent
953dd37978
commit
424e012651
1 changed files with 8 additions and 2 deletions
|
@ -6,22 +6,28 @@ import _thread
|
|||
|
||||
|
||||
class Server:
|
||||
def __init__(self, host_port_pair, handler, handler_args=[]):
|
||||
def __init__(self, host_port_pair):
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.bind(host_port_pair)
|
||||
self.sock.listen(2)
|
||||
|
||||
|
||||
def run(self, handler, handler_args=[]):
|
||||
self.handler = handler
|
||||
while 1:
|
||||
sock, info = self.sock.accept()
|
||||
_thread.start_new_thread(self.controller, (sock, info, handler_args))
|
||||
|
||||
|
||||
def stop(self):
|
||||
self.sock.close()
|
||||
|
||||
def controller(self, sock, info, handler_args):
|
||||
sock = ESock(sock)
|
||||
handler = self.handler(sock, info, **handler_args)
|
||||
while 1:
|
||||
try:
|
||||
handler.handle(sock.recv())
|
||||
handler.handle(*sock.recv())
|
||||
except (socket.error, OSError):
|
||||
handler.finish()
|
||||
handler.sock.close()
|
||||
|
|
Reference in a new issue