Skip to content

Instantly share code, notes, and snippets.

@joshfinley
Last active November 7, 2024 17:48
Show Gist options
  • Save joshfinley/65b8ac0d358af9c4379a384820b7d232 to your computer and use it in GitHub Desktop.
Save joshfinley/65b8ac0d358af9c4379a384820b7d232 to your computer and use it in GitHub Desktop.
Python GET upload server and client to use for it
import os
from http.server import BaseHTTPRequestHandler, HTTPServer

class UploadHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        filename = os.path.basename(self.path)
        content_length = int(self.headers['Content-Length'])
        data = self.rfile.read(content_length)
        with open(filename, 'wb') as f:
            f.write(data)
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"File uploaded successfully")

server_address = ('', 8000)
httpd = HTTPServer(server_address, UploadHandler)
print("Server running on port 8000...")
httpd.serve_forever()
import requests

# Configuration
url = 'http://localhost:8000/test_data'
file_path = 'test_data'
proxy = None  # Set to 'http://your_proxy_address:port' if needed, otherwise None
use_https = False  # Set to True if the server requires HTTPS

# Set up proxies if needed
proxies = {
    'http': proxy,
    'https': proxy,
} if proxy else None

try:
    with open(file_path, 'rb') as file:
        response = requests.get(
            url,
            data=file,
            proxies=proxies,
            verify=not use_https  # Disable SSL verification if necessary
        )

    # Print response details for debugging
    print("Status Code:", response.status_code)
    print("Response Text:", response.text)
    print("Response Headers:", response.headers)

    # Raise an error for bad status codes
    response.raise_for_status()

except requests.exceptions.RequestException as e:
    print("Request failed:", e)

except FileNotFoundError:
    print(f"File not found: {file_path}")

except Exception as e:
    print("An unexpected error occurred:", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment