app_client_blocking.py - shortening main and other DRY fixes app_server.py - adding lsock close to cleanup base line secure socket framework
60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Sun Mar 22 10:43:35 2020
|
|
|
|
@author: cpan
|
|
"""
|
|
|
|
import sys
|
|
import socket
|
|
import selectors
|
|
import traceback
|
|
import libserver
|
|
|
|
|
|
sel = selectors.DefaultSelector()
|
|
|
|
|
|
def accept_wrapper(sock):
|
|
conn, addr = sock.accept() # Should be ready to read
|
|
print("accepted connection from", addr)
|
|
conn.setblocking(False)
|
|
message = libserver.Message(sel, conn, addr)
|
|
sel.register(conn, selectors.EVENT_READ, data=message)
|
|
|
|
|
|
if len(sys.argv) != 3:
|
|
print("usage:", sys.argv[0], "<host> <port>")
|
|
sys.exit(1)
|
|
|
|
host, port = sys.argv[1], int(sys.argv[2])
|
|
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
# Avoid bind() exception: OSError: [Errno 48] Address already in use
|
|
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
lsock.bind((host, port))
|
|
lsock.listen()
|
|
print("listening on", (host, port))
|
|
lsock.setblocking(False)
|
|
sel.register(lsock, selectors.EVENT_READ, data=None)
|
|
|
|
try:
|
|
while True:
|
|
events = sel.select(timeout=None)
|
|
for key, mask in events:
|
|
if key.data is None:
|
|
accept_wrapper(key.fileobj)
|
|
else:
|
|
message = key.data
|
|
try:
|
|
message.process_events(mask)
|
|
except Exception:
|
|
print(
|
|
"main: error: exception for",
|
|
f"{message.addr}:\n{traceback.format_exc()}",
|
|
)
|
|
message.close()
|
|
except KeyboardInterrupt:
|
|
print("caught keyboard interrupt, exiting")
|
|
finally:
|
|
sel.close()
|
|
lsock.close() |