fhttpy/modules/servers.py

45 lines
1.0 KiB
Python
Raw Normal View History

2022-02-14 21:43:12 +00:00
global ssl
import ssl
global serverThread
class serverThread(threading.Thread):
def __init__(self,socket):
threading.Thread.__init__(self)
self.socket = socket
def run(self):
connection = False
address = False
while True:
try:
connection,address = self.socket.accept()
except:
continue
try:
connection.settimeout(timeout)
if not triggerEvent("onConnection",connection,address): raise excConnectionClosed
except Exception as e:
handleException(e)
try:
connection.close()
except:
pass
global makeServer
def makeServer(host,port,https):
print("Opening " +str(host)+ ":" +str(port)+ " (" +str(https)+ ") ...")
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind((host,port))
serverSocket.settimeout(5)
if https:
serverSocket = ssl.wrap_socket(
serverSocket,
server_side = True,
certfile = https,
ssl_version = ssl.PROTOCOL_TLS
)
serverSocket.listen(65535)
thread = serverThread(serverSocket)
serverThreads.append(thread)
thread.start()