I have added the feature for the BBS to be able to accept user messages and display all messages. Still to be done: asc pages for messages and implementation of that. Yes, I do know that I'm still including my test data in the json files. I do not care.
83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
'''
|
|
BBS.py
|
|
|
|
A BBS that was shamelessly based entirely on the blog post found: https://www.fsxnet.nz/tutorials/bbs/python-bbs/start
|
|
'''
|
|
import configparser
|
|
import sys
|
|
import socket
|
|
import threading
|
|
|
|
|
|
from comms import comms
|
|
|
|
nodes = []
|
|
|
|
class bbsServer():
|
|
def __init__(self, port, host='0.0.0.0', nodemax=1):
|
|
self.port = port
|
|
self.host = host
|
|
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
# try:
|
|
config = configparser.ConfigParser()
|
|
config.read("config.ini")
|
|
self.nodemax = config.getint("Main","MaxConnections")
|
|
# except:
|
|
# self.nodemax = nodemax
|
|
|
|
for index in range(nodemax):
|
|
nodes.append(False)
|
|
|
|
try:
|
|
self.server.bind((self.host, self.port))
|
|
except socket.error:
|
|
print("Couldn't bind %s" % (socket.error))
|
|
sys.exit()
|
|
|
|
self.server.listen(10)
|
|
|
|
def run_thread(self, conn, addr, node):
|
|
global nodes
|
|
|
|
try:
|
|
comms(conn, addr, node + 1)
|
|
|
|
except (RuntimeError, OSError):
|
|
print("Node %s hung up..." % (node + 1))
|
|
|
|
print("Node %s offline." % (node + 1))
|
|
nodes[node] = False
|
|
conn.close()
|
|
sys.exit()
|
|
|
|
def run(self):
|
|
print("Starting BBS on port %s" % (self.port))
|
|
|
|
while True:
|
|
conn, addr = self.server.accept()
|
|
|
|
for index in range(self.nodemax):
|
|
if nodes[index] == False:
|
|
|
|
nodes[index] = True
|
|
threading.Thread(target=self.run_thread, args=(conn, addr, index)).start()
|
|
break
|
|
else:
|
|
conn.sendall("BUSY\r\n")
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Usage python bbs.py config.ini")
|
|
exit(1)
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(sys.argv[1])
|
|
|
|
server = bbsServer(config.getint("Main", "Port"))
|
|
|
|
server.run()
|
|
|