BINARY FILE UPLOAD - Uploading files
Python
#!/usr/bin/env python3
"""BINARY FILE UPLOAD - Uploading files"""
import urllib.request
import tempfile
import os
print("Binary File Upload:")
# Create test file
temp_dir = tempfile.gettempdir()
test_file = os.path.join(temp_dir, "upload_test.bin")
with open(test_file, 'wb') as f:
f.write(b'\x00\x01\x02\x03\x04')
# Read and upload
with open(test_file, 'rb') as f:
file_data = f.read()
url = "http://httpbin.org/post"
req = urllib.request.Request(url, data=file_data)
req.add_header('Content-Type', 'application/octet-stream')
try:
with urllib.request.urlopen(req) as response:
print(f" Uploaded {len(file_data)} bytes")
print(f" Status: {response.status}")
except Exception as e:
print(f" Error: {e}")
finally:
os.remove(test_file)