Created
January 9, 2026 21:44
-
-
Save VictorNS69/eac3d29c18e8f1590b88b1092f505933 to your computer and use it in GitHub Desktop.
Simple HTTP Post 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
| #!/usr/bin/env python3 | |
| # Simple HTTP Server with Upload in Python3, to be run on Attacker Machine: | |
| import http.server | |
| import socketserver | |
| import os | |
| import sys | |
| from datetime import datetime | |
| class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): | |
| def do_POST(self): | |
| content_length = int(self.headers['Content-Length']) | |
| post_data = self.rfile.read(content_length) | |
| # Get the filename from the path (use the requested URL or a default) | |
| timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | |
| filename = os.path.basename(self.path) or f'{timestamp}_upload' | |
| # Write the data to a file | |
| with open(filename, 'wb') as f: | |
| f.write(post_data) | |
| # Send a response back to the client | |
| self.send_response(200) | |
| self.end_headers() | |
| self.wfile.write(b'File uploaded successfully!') | |
| print(f"\n[+] File received and saved as: {filename}") | |
| def get_port(): | |
| """Get port from command line arguments or user input, default to 80""" | |
| # Check command line arguments | |
| if len(sys.argv) > 1: | |
| try: | |
| port = int(sys.argv[1]) | |
| if 1 <= port <= 65535: | |
| return port | |
| else: | |
| print(f"[-] Port {port} is out of range (1-65535). Using default port 80.") | |
| except ValueError: | |
| print(f"[-] Invalid port number: {sys.argv[1]}. Using default port 80.") | |
| else: | |
| # If no valid argument, port 80 | |
| return 80 | |
| # Get port from user/arguments | |
| PORT = get_port() | |
| Handler = MyHttpRequestHandler | |
| try: | |
| with socketserver.TCPServer(("", PORT), Handler) as httpd: | |
| print(f"[+] Server started on port {PORT}") | |
| print(f"[+] Server accessible at: http://0.0.0.0:{PORT}") | |
| print("[+] Waiting for file upload from Windows client...") | |
| print("[+] Press Ctrl+C to stop the server") | |
| httpd.serve_forever() | |
| except PermissionError: | |
| print(f"[-] Error: Permission denied to bind to port {PORT}") | |
| print("[-] Try running with sudo or choose a port > 1024") | |
| sys.exit(1) | |
| except OSError as e: | |
| if "Address already in use" in str(e): | |
| print(f"[-] Error: Port {PORT} is already in use") | |
| print("[-] Try a different port or kill the process using it") | |
| else: | |
| print(f"[-] Error: {e}") | |
| sys.exit(1) | |
| except KeyboardInterrupt: | |
| print("\n[+] Server stopped by user") | |
| sys.exit(0) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In windows, you can use powershell to upload files to the server
If no path (
/file.txt) provided, the uploaded file will be named{timestamp}_upload.