Wayne Colin Abel
5 years ago
2 changed files with 37 additions and 23 deletions
-
4conftest.py
-
56src/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): |
def handle(self): |
||||
# self.request - TCP socket connected to the client |
# self.request - TCP socket connected to the client |
||||
|
print("Hl") |
||||
self.data = self.request.recv(1024).strip() |
self.data = self.request.recv(1024).strip() |
||||
print("{} sent:".format(self.client_address[0])) |
print("{} sent:".format(self.client_address[0])) |
||||
print(self.data) |
|
||||
|
print(self.data.decode()) |
||||
# just send back ACK for data arrival confirmation |
# just send back ACK for data arrival confirmation |
||||
self.request.sendall("ACK from TCP Server".encode()) |
self.request.sendall("ACK from TCP Server".encode()) |
||||
|
|
||||
def ping(self, param): |
def ping(self, param): |
||||
return "PONG" |
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 |
return True |
||||
else: |
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__": |
if __name__ == "__main__": |
||||
HOST, PORT = "localhost", 9999 |
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() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue