Skip to content

Instantly share code, notes, and snippets.

@A750gixr
Last active April 28, 2025 14:55
Show Gist options
  • Save A750gixr/7c40a5ff2d3edf3e6cf77f219d2c5018 to your computer and use it in GitHub Desktop.
Save A750gixr/7c40a5ff2d3edf3e6cf77f219d2c5018 to your computer and use it in GitHub Desktop.
server for info_borrower
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
from datetime import datetime
class DataHandler(BaseHTTPRequestHandler):
def do_POST(self):
try:
# Get the content length
content_length = int(self.headers.get('Content-Length', 0))
# Add basic size limit (e.g., 10MB)
if content_length > 10 * 1024 * 1024:
self.send_error(413, "Request entity too large")
return
# Read the data
data = self.rfile.read(content_length)
# Create timestamp for unique filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"data_{timestamp}.txt"
# Make sure the directory exists
save_dir = r"D:\Temp"
os.makedirs(save_dir, exist_ok=True)
filepath = os.path.join(save_dir, filename)
# Save the data to file
with open(filepath, 'wb') as f:
f.write(data)
# Send response
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"Data received and saved successfully")
except Exception as e:
print(f"Error: {str(e)}") # Log the error
self.send_error(500, f"Internal server error: {str(e)}")
def run_server():
try:
# Using 0.0.0.0 allows connections from any IP
server_address = ('0.0.0.0', 8080)
httpd = HTTPServer(server_address, DataHandler)
print(f"Server running on port 8080...")
httpd.serve_forever()
except Exception as e:
print(f"Failed to start server: {e}")
except KeyboardInterrupt:
print("\nShutting down server...")
httpd.shutdown()
if __name__ == '__main__':
run_server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment