packet:codedumps
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| packet:codedumps [2023/07/16 18:09] – m0ukf | packet:codedumps [2024/08/24 11:25] (current) – removed m0lte | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | === Simple Chat Application that uses port 33000 and can be connected to BPQ32=== | ||
| - | < | ||
| - | import socket | ||
| - | import threading | ||
| - | |||
| - | class ChatServer: | ||
| - | def __init__(self): | ||
| - | self.host = ' | ||
| - | self.port = 33000 | ||
| - | self.server = None | ||
| - | self.clients = {} | ||
| - | self.lock = threading.Lock() | ||
| - | |||
| - | def start(self): | ||
| - | self.server = socket.socket(socket.AF_INET, | ||
| - | self.server.bind((self.host, | ||
| - | self.server.listen(5) | ||
| - | print(" | ||
| - | |||
| - | while True: | ||
| - | client_socket, | ||
| - | print(" | ||
| - | threading.Thread(target=self.handle_client, | ||
| - | |||
| - | def handle_client(self, | ||
| - | username = client_socket.recv(1024).decode(' | ||
| - | self.add_client(client_socket, | ||
| - | |||
| - | while True: | ||
| - | try: | ||
| - | message = client_socket.recv(1024).decode(' | ||
| - | if not message: | ||
| - | self.remove_client(client_socket) | ||
| - | break | ||
| - | self.broadcast(message) | ||
| - | except ConnectionResetError: | ||
| - | self.remove_client(client_socket) | ||
| - | break | ||
| - | |||
| - | def add_client(self, | ||
| - | self.lock.acquire() | ||
| - | self.clients[client_socket] = username | ||
| - | self.lock.release() | ||
| - | print(" | ||
| - | |||
| - | def remove_client(self, | ||
| - | self.lock.acquire() | ||
| - | username = self.clients[client_socket] | ||
| - | del self.clients[client_socket] | ||
| - | self.lock.release() | ||
| - | client_socket.close() | ||
| - | print(" | ||
| - | |||
| - | def broadcast(self, | ||
| - | self.lock.acquire() | ||
| - | for client_socket in self.clients: | ||
| - | client_socket.send(message.encode(' | ||
| - | self.lock.release() | ||
| - | |||
| - | if __name__ == " | ||
| - | chat_server = ChatServer() | ||
| - | chat_server.start() | ||
| - | </ | ||
| - | |||
| - | === Simple Chat Application that uses telnet to connect to bpq and connect to that chat server above=== | ||
| - | |||
| - | < | ||
| - | import socket | ||
| - | import sys | ||
| - | import threading | ||
| - | import time | ||
| - | import os | ||
| - | |||
| - | HOST = ' | ||
| - | PORT = 8010 | ||
| - | USERNAME = ' | ||
| - | PASSWORD = ' | ||
| - | print(' | ||
| - | # Create a socket and connect to the server | ||
| - | try: | ||
| - | sock = socket.create_connection((HOST, | ||
| - | except ConnectionRefusedError: | ||
| - | print(f" | ||
| - | sys.exit(1) | ||
| - | |||
| - | # Receive and print the login prompt | ||
| - | login_prompt = sock.recv(1024).decode(' | ||
| - | print(login_prompt) | ||
| - | |||
| - | # Send the username | ||
| - | sock.sendall((USERNAME + " | ||
| - | |||
| - | # Receive and print the password prompt | ||
| - | time.sleep(1) | ||
| - | password_prompt = sock.recv(1024).decode(' | ||
| - | print(password_prompt) | ||
| - | |||
| - | # Send the password | ||
| - | sock.sendall((PASSWORD + " | ||
| - | |||
| - | # Function to continuously receive and display server responses | ||
| - | def receive_responses(): | ||
| - | while True: | ||
| - | response = sock.recv(1024).decode(' | ||
| - | print(response) | ||
| - | |||
| - | # Create and start the thread for receiving responses | ||
| - | response_thread = threading.Thread(target=receive_responses) | ||
| - | response_thread.start() | ||
| - | |||
| - | # Wait for 3 seconds | ||
| - | time.sleep(3) | ||
| - | |||
| - | # Send the command "c oukdev" | ||
| - | sock.sendall((" | ||
| - | os.system(' | ||
| - | print(' | ||
| - | # Main thread for sending commands | ||
| - | while True: | ||
| - | command = input() | ||
| - | |||
| - | # Send the command to the server | ||
| - | sock.sendall((command + " | ||
| - | |||
| - | if command.lower() == " | ||
| - | break | ||
| - | # Wait for the response thread to finish and close the connection | ||
| - | response_thread.join() | ||
| - | sock.close() | ||
| - | </ | ||
packet/codedumps.1689530993.txt.gz · Last modified: by m0ukf
