From 9fc46200888631eaa948f4aedda5d0737da5b0e2 Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Sun, 5 May 2024 23:30:08 -0400 Subject: [PATCH] First commit --- pychat0.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ server.py | 23 +++++++++++++++++++++++ server2.py | 22 ++++++++++++++++++++++ teapotd.py | 24 ++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 pychat0.py create mode 100644 server.py create mode 100644 server2.py create mode 100644 teapotd.py diff --git a/pychat0.py b/pychat0.py new file mode 100644 index 0000000..ce5452e --- /dev/null +++ b/pychat0.py @@ -0,0 +1,47 @@ +from socket import * +from _thread import start_new_thread + +VERSION = "PYCHAT_0.0" +PORT = 6783 + +s = socket(AF_INET, SOCK_DGRAM) +s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) +s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) +s.bind(('', PORT)) + +def recieveThread(): + while True: + b, a = s.recvfrom(4096) + if len(b) < 1: + print(a) + break + print(b.decode("utf-8")) + + +name = '' +while name == '': + print("Enter username:", end="") + name = input() + if(len(name) > 20): + print("Error: username must be less than 20 characters") + name = None + name = name.lstrip() +name = name.rjust(19) + +def sendThread(): + while True: + #print(name + ' > ', end='') + msg = name + ' | ' + input() + s.sendto(msg.encode('utf-8'), ('255.255.255.255', PORT)) + +start_new_thread(recieveThread, ()) +#start_new_thread(sendThread, ()) +try: + sendThread() +except KeyboardInterrupt: + print("Closing socket") + s.close() +except EOFError: + print("Closing socket") + s.close() + diff --git a/server.py b/server.py new file mode 100644 index 0000000..2738c50 --- /dev/null +++ b/server.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import socket # Import socket module +import sys + +s = socket.socket() # Create a socket object +host = socket.gethostname() # Get local machine name +port = 12345 # Reserve a port for your service. +try: + s.bind((host, port)) # Bind to the port + + s.listen(5) # Now wait for client connection. + while True: + c, addr = s.accept() # Establish connection with client. + print('Got connection from', addr) + c.send(b'Thank you for connecting\n') + c.close() # Close the connection +except KeyboardInterrupt: + s.close() + print("Exiting Server") + sys.exit() +finally: + s.close() diff --git a/server2.py b/server2.py new file mode 100644 index 0000000..ba58aea --- /dev/null +++ b/server2.py @@ -0,0 +1,22 @@ +import os +import socket +from multiprocessing import Process + +def handle_client(cli: socket, addr): + # For now just print out the received bytes and close + print("Received connection from", addr) + recv_data = cli.recv(4096) + print(recv_data.decode("utf-8")) + cli.close() + + +if __name__ == '__main__': + address = '' + port = 8080 + so = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + so.bind((address, port)) + so.listen(5) + while True: + (client_socket, client_address) = so.accept() + print("Client connected") + Process(target=handle_client, args=(client_socket, client_address)).start() diff --git a/teapotd.py b/teapotd.py new file mode 100644 index 0000000..33495ca --- /dev/null +++ b/teapotd.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +import socket # Import socket module +import sys + +s = socket.socket() # Create a socket object +host = socket.gethostname() # Get local machine name +port = 8080 # Reserve a port for your service. +try: + s.bind((host, port)) # Bind to the port + + s.listen(5) # Now wait for client connection. + while True: + c, addr = s.accept() # Establish connection with client. + print('Got connection from', addr) + print(c.recv(1024)) + c.send(b"HTTP/1.0 418 I'm a Teapot\r\nConnection: close\r\nServer: Teapot\r\n\r\nError 418 I'm a teapot") + c.close() # Close the connection +except KeyboardInterrupt: + s.close() + print("Exiting Server") + sys.exit() +finally: + s.close()