Browse Source

[refactor] Klasse in IRCServer umbenannt, Client und conftest angepasst

develop
Wayne Colin Abel 2 years ago
parent
commit
8e09d59ee4
  1. 4
      conftest.py
  2. 56
      src/server.py

4
conftest.py

@ -9,5 +9,5 @@ def server():
"""
returns a fresh server instance
"""
from src.server import Handler_TCPServer
return Handler_TCPServer
from src.server import RequestHandler
return RequestHandler

56
src/server.py

@ -1,44 +1,58 @@
import socketserver
#!/usr/bin/python
class Handler_TCPServer(socketserver.BaseRequestHandler):
"""
The TCP Server class for demonstration.
import socketserver
Note: We need to implement the Handle method to exchange data
with TCP client.
"""
class RequestHandler(socketserver.BaseRequestHandler):
def handle(self):
# self.request - TCP socket connected to the client
print("Hl")
self.data = self.request.recv(1024).strip()
print("{} sent:".format(self.client_address[0]))
print(self.data)
print(self.data.decode())
# just send back ACK for data arrival confirmation
self.request.sendall("ACK from TCP Server".encode())
def ping(self, param):
return "PONG"
def command_pass(self, param):
return param == self.password
def command_pass(self, desired_password):
return desired_password == self.password
def command_nickname(self, nickname):
if nickname not in self.user_list:
self.user_list.append(nickname)
def command_nickname(self, user, desired_nickname):
if desired_nickname not in self.user_list:
self.user_list.append(desired_nickname)
self.user_list.remove(user)
message = ":" + user + " NICK " + desired_nickname
return True
else:
return False
self.user_list.remove(desired_nickname)
if user in self.user_list:
self.user_list.remove(user)
# issue kill command (?)
return True
class IRCServer(socketserver.TCPServer):
"""
The TCP Server class for demonstration.
Note: We need to implement the Handle method to exchange data
with TCP client.
"""
def __init__(self, server_adress):
self.servername = 'localhost'
self.channels = {}
self.clients = {}
socketserver.TCPServer.__init__(self, server_adress, RequestHandler)
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Init the TCP server object, bind it to the localhost on 9999 port
tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer)
# Activate the TCP server.
# To abort the TCP server, press Ctrl-C.
tcp_server.serve_forever()
ircserver = IRCServer((HOST, PORT))
ircserver.serve_forever()
Loading…
Cancel
Save