Skip to content

Commit

Permalink
Change structure and more error handling
Browse files Browse the repository at this point in the history
- Change the module install workflow
- Separated setup proccess from the __init__ function to have a cleaner code
- set a default port in the __init__ function
- Change __port__ to self.port
- Add except when user stopped the server
- Change __server__ to server
- Remove chdir(".")
- Change entire run() function
- Add DocString
- Add comments
  • Loading branch information
Mehran-Seifalinia committed Aug 2, 2023
1 parent 6983636 commit a990f12
Showing 1 changed file with 69 additions and 25 deletions.
94 changes: 69 additions & 25 deletions Linux/ShareFolder.py
Expand Up @@ -3,41 +3,85 @@
from os import chdir, getcwd, system
from http.server import HTTPServer, CGIHTTPRequestHandler

# Import external modules
try:
from FindMyIP import internal
except ModuleNotFoundError:
system("pip install FindMyIP")
from FindMyIP import internal

try:
from colorama import init, Fore
init(autoreset=True)
except ModuleNotFoundError:
system("pip install colorama")
from colorama import init, Fore
init(autoreset=True)
# Function to install required modules
def install_module(module_name):
try:
from importlib import import_module
import_module(module_name)
return True
except ModuleNotFoundError:
try:
from pip import main
main(["install", module_name])
return True
except:
print(f"Failed to install {module_name}. Please install it manually.")
return False

# List of external modules required for the program
reuired_modules = ["FindMyIP", "colorama"]

# Install required modules if not already installed
for module_name in reuired_modules:
if not install_module(module_name):
exit(1)

# Import required modules after successful installation
from FindMyIP import internal
from colorama import init, Fore
init(autoreset=True)


class ShareFile:
def __init__(self):
self.current_directory = getcwd()
self.__port__ = int(input(f"[!] You Are sharing \"{self.current_directory}\" on your local network.\nEnter Your port here: "))
"""
A simple file-sharing server for sharing files over the local network.
Attributes:
port (int): The port number on which the server will listen.
current_directory (str): The current working directory for file sharing.
green (str): ANSI escape code for green text color.
red (str): ANSI escape code for red text color.
reset (str): ANSI escape code to reset text color.
Methods:
setup(): Set up the file-sharing server by configuring the port.
server(): Start the HTTP server to share files.
run(): Run the file-sharing server.
"""

def __init__(self, port=8080):
# Initialize instance attributes
self.current_directory = getcwd()
self.port = port
self.green = Fore.GREEN
self.red = Fore.RED
self.reset = Fore.RESET

def __server__(self):
chdir(".")
self.server = HTTPServer(("", self.__port__), CGIHTTPRequestHandler)
print(f"Server is online on {self.green}{internal()}{self.reset}:{self.red}{self.__port__}")
def setup(self):
try:
# Allow user to set up the port for the server
self.port = int(input(f"{self.red}[!] You Are sharing \"{self.current_directory}\" on your local network.{self.reset}\nEnter Your port here (default: {self.port}): "))
except KeyboardInterrupt:
# Handle KeyboardInterrupt to gracefully stop the server
print(f"{self.red}\n[-] Server stopped by the user{self.reset}")
exit(1)
def server(self):
try:
# Set up the server and start serving files
server_address = ("", self.port)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
print(f"{self.green}[+] Server is running on http://{internal()}:{self.port}")
httpd.serve_forever()
except KeyboardInterrupt:
# Handle KeyboardInterrupt to gracefully stop the server
print(f"{self.red}\n[-] Server stopped by the user{self.reset}")
exit(1)

def run(self):
self.__server__()
self.server.serve_forever()
def run(self):
# Run the file-sharing server
self.setup()
self.server()

if __name__ == '__main__':
app = ShareFile()
app.run()

0 comments on commit a990f12

Please sign in to comment.