| import sys |
| import requests |
| import mimetypes |
| import os |
|
|
| def main(): |
| if len(sys.argv) < 2: |
| print("Usage: python src/step10_demo_request.py path/to/image.jpg") |
| sys.exit(1) |
|
|
| img_path = sys.argv[1] |
| url = "http://127.0.0.1:8000/predict" |
|
|
| mime_type, _ = mimetypes.guess_type(img_path) |
| if mime_type is None: |
| mime_type = "application/octet-stream" |
|
|
| print(f"Sending: {img_path} -> {url} (mime: {mime_type})") |
|
|
| try: |
| with open(img_path, "rb") as f: |
| files = {"file": (os.path.basename(img_path), f, mime_type)} |
| r = requests.post(url, files=files, timeout=60) |
|
|
| print("Request sent.") |
| print("Status code:", r.status_code) |
| print("Response headers:", dict(r.headers)) |
|
|
| try: |
| print("JSON response:", r.json()) |
| except ValueError: |
| print("Non-JSON response text:", r.text) |
|
|
| |
| |
|
|
| except requests.exceptions.RequestException as e: |
| print("Request failed:", repr(e)) |
| except FileNotFoundError: |
| print("File not found:", img_path) |
| except Exception as e: |
| print("Unexpected error:", repr(e)) |
|
|
| if __name__ == "__main__": |
| main() |
| |
| |
| |
| |