fstream/modules/servers.py

59 lines
1.4 KiB
Python
Raw Normal View History

2022-05-29 12:31:07 +00:00
global ssl
import ssl
global serverThread
class serverThread(threading.Thread):
def __init__(self,socket,isHttps):
threading.Thread.__init__(self)
self.socket = socket
self.isHttps = isHttps
def run(self):
connection = False
address = False
while True:
try:
connection,address = self.socket.accept()
except:
continue
try:
if self.isHttps:
connection.settimeout(5)
connection.do_handshake()
connection.settimeout(timeout)
if not triggerEvent("onConnection",connection,address): raise excConnectionClosed
except Exception as e:
handleException(e)
try:
connection.close()
except:
pass
global makeServer
2024-01-17 22:01:27 +00:00
def makeServer(host,port,https,sockOpts):
2022-05-29 12:31:07 +00:00
print("Opening " +str(host)+ ":" +str(port)+ " (" +str(https)+ ") ...")
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2024-01-17 22:01:27 +00:00
for sockOpt in sockOpts:
serverSocket.setsockopt(*sockOpt)
2022-05-29 12:31:07 +00:00
serverSocket.bind((host,port))
if https:
proto = False
if sys.version_info >= (3,10):
proto = ssl.PROTOCOL_TLS_SERVER
else:
proto = ssl.PROTOCOL_TLS
ctx = ssl.SSLContext(proto)
ctx.check_hostname = False
ctx.load_cert_chain(https)
serverSocket = ctx.wrap_socket(
serverSocket,
server_side = True,
do_handshake_on_connect = False
)
serverSocket.listen(65535)
thread = serverThread(serverSocket,not (https == False))
serverThreads.append(thread)
2024-01-17 22:01:27 +00:00
thread.start()