Last active
March 13, 2025 23:15
-
-
Save sovietscout/43ebf85a6df40a437d903ad67e89e75c to your computer and use it in GitHub Desktop.
Barebones File Transfer Server in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import socket | |
| from tkinter import Tk, filedialog | |
| from http.server import HTTPServer, SimpleHTTPRequestHandler | |
| print('[Press Ctrl+C to stop]') | |
| # Gets directory to serve | |
| print('Select folder to serve') | |
| Tk().withdraw() | |
| dirPath = filedialog.askdirectory() | |
| class Handler(SimpleHTTPRequestHandler): | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, directory=dirPath, **kwargs) | |
| def getIP() -> str: | |
| # Gets local IP address | |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| try: | |
| s.connect(('10.255.255.255', 1)) | |
| IP = s.getsockname()[0] | |
| except Exception: | |
| IP = '127.0.0.1' | |
| finally: | |
| s.close() | |
| return IP | |
| try: | |
| IPAddr = getIP() | |
| Port = 8000 | |
| serverAddr = (IPAddr, Port) | |
| print(f'Serving at http://{IPAddr}:{Port}') | |
| httpd = HTTPServer(serverAddr, Handler) | |
| httpd.serve_forever() | |
| except KeyboardInterrupt: | |
| print('Exiting...') | |
| httpd.socket.close() | |
| sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment