Skip to content
Snippets Groups Projects
Commit cc64322d authored by Olivier Levillain's avatar Olivier Levillain
Browse files

First version of a running server.

For now, it does not do much...
parent 533a1524
No related branches found
No related tags found
No related merge requests found
import socket
from threading import Thread
from config import *
context_counter = 0
......@@ -12,6 +14,12 @@ class Context:
def check_current_user(self, login):
return self.current_user is not None and self.current_user['login'] == login
def handle_client(server, socket, context):
print("[C%d] Connection closed" % context.id)
socket.close()
pass
class Server:
def __init__(self, config):
self.config = config
......@@ -75,3 +83,35 @@ class Server:
else:
return False, "Invalid command"
def run(self):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
address_and_port = self.config['address'], self.config['port']
server_socket.bind(address_and_port)
server_socket.listen(5)
print("[S] Server listening on %s:%d" % address_and_port)
while True:
context = self.new_context()
try:
s, client_address = server_socket.accept()
print("[C%d] Connection accepted from %s" % (context.id, client_address))
t = Thread(target=handle_client, args=(self, s, context))
t.start()
except Exception as e:
print("[C%d] Failed (Reason: %s)" % (context.id, e))
self.destroy_context(context)
if __name__ == "__main__":
try:
config = load_config_from_file("config")
except Exception:
config = Config()
server = Server(config)
try:
server.run()
except KeyboardInterrupt:
print()
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment