Uploads file path to a URL and returns exit code for the program.
(path, url, proxy)
| 715 | |
| 716 | |
| 717 | def do_upload(path, url, proxy): |
| 718 | """ |
| 719 | Uploads file path to a URL and returns exit code for the program. |
| 720 | """ |
| 721 | |
| 722 | with open(path, "rb") as f: |
| 723 | # Get proxies from environment/system |
| 724 | proxy_handler = urllib.request.ProxyHandler(urllib.request.getproxies()) |
| 725 | if proxy != "": |
| 726 | # unless a proxy is explicitly passed, then use that instead |
| 727 | proxy_handler = urllib.request.ProxyHandler({"https": proxy, "http": proxy}) |
| 728 | |
| 729 | opener = urllib.request.build_opener(proxy_handler) |
| 730 | request = urllib.request.Request(url, data=f, method="PUT") |
| 731 | request.add_header(str("Content-Type"), str("application/zip")) |
| 732 | request.add_header("Content-Length", os.fstat(f.fileno()).st_size) |
| 733 | |
| 734 | try: |
| 735 | url = opener.open(request) |
| 736 | if url.getcode() == 200: |
| 737 | log("Done uploading") |
| 738 | else: |
| 739 | raise Exception( |
| 740 | "Error uploading, expected status code 200, got status code: {0}".format( |
| 741 | url.getcode() |
| 742 | ) |
| 743 | ) |
| 744 | except Exception: |
| 745 | log(traceback.format_exc()) |
| 746 | return 1 |
| 747 | |
| 748 | return 0 |
| 749 | |
| 750 | |
| 751 | def parse_host(host): |