phython
ClientServer/.DS_Store
ClientServer/chatclient.py
""" File: chatclient.py Client for a chat room. """ from socket import * from codecs import decode HOST = "localhost" PORT = 5000 BUFSIZE = 1024 ADDRESS = (HOST, PORT) CODE = "ascii" server = socket(AF_INET, SOCK_STREAM) server.connect(ADDRESS) print(decode(server.recv(BUFSIZE), CODE)) # The server's greeting while True: message = input("> ") # Get my reply or quit if not message: break server.send(bytes(message, CODE)) # Send my reply to the server reply = decode(server.recv(BUFSIZE), CODE) # Get the server's reply if not reply: print("Server disconnected") break print(reply) # Display the server's reply server.close()
ClientServer/chatserver.py
""" File: chatserver.py Server for a chat room. Handles one client at a time and participates in the conversation. """ from socket import * from codecs import decode HOST = "localhost" PORT = 5000 ADDRESS = (HOST, PORT) BUFSIZE = 1024 CODE = "ascii" server = socket(AF_INET, SOCK_STREAM) server.bind(ADDRESS) server.listen(5) while True: print("Waiting for connection . . .") client, address = server.accept() print("... connected from: ", address) client.send(bytes("Welcome to my chat room!", CODE)) # Send greeting while True: message = decode(client.recv(BUFSIZE), CODE) # Reply from client if not message: print("Client disconnected") client.close() break else: print(message) client.send(bytes(input('> '), CODE)) # Reply to client
ClientServer/timeclient.py
""" File: timeclient.py Client for obtaining the day and time. """ from socket import * from codecs import decode HOST = "localhost" PORT = 5000 BUFSIZE = 1024 ADDRESS = (HOST, PORT) server = socket(AF_INET, SOCK_STREAM) # Create a socket server.connect(ADDRESS) # Connect it to a host dayAndTime = decode(server.recv(BUFSIZE), "ascii") # Read a string from it print(dayAndTime) server.close() # Close the connection
ClientServer/timeclienthandler.py
""" File: timeclienthandler.py Client handler for providing the day and time. """ from time import ctime from threading import Thread class TimeClientHandler(Thread): """Handles a client request.""" def __init__(self, client): Thread.__init__(self) self.client = client def run(self): self.client.send(bytes(ctime() + \ "\nHave a nice day!", "ascii")) self.client.close()
ClientServer/timeserver1.py
""" File: timeserver1.py Server for providing the day and time. """ from socket import * from time import ctime HOST = "localhost" PORT = 5000 ADDRESS = (HOST, PORT) server = socket(AF_INET, SOCK_STREAM) server.bind(ADDRESS) server.listen(5) while True: print("Waiting for connection . . .") client, address = server.accept() print("... connected from: ", address) client.send(bytes(ctime() + "\nHave a nice day!", "ascii")) client.close()
ClientServer/timeserver2.py
""" File: timeserver2.py Server for providing the day and time. Uses client handlers to handle clients' requests. """ from socket import * from timeclienthandler import TimeClientHandler HOST = "localhost" PORT = 5000 ADDRESS = (HOST, PORT) server = socket(AF_INET, SOCK_STREAM) server.bind(ADDRESS) server.listen(5) # The server now just waits for connections from clients # and hands sockets off to client handlers while True: print("Waiting for connection . . .") client, address = server.accept() print("... connected from: ", address) handler = TimeClientHandler(client) handler.start()