(curl_str)
| 21 | |
| 22 | |
| 23 | def parse_curl(curl_str): |
| 24 | parser = argparse.ArgumentParser(description="") |
| 25 | parser.add_argument("target_url", type=str, nargs="?") |
| 26 | parser.add_argument("-X", "--request", type=str, nargs=1, default="") |
| 27 | parser.add_argument("-H", "--header", nargs=1, action="append", default=[]) |
| 28 | parser.add_argument("-d", "--data", nargs=1, action="append", default=[]) |
| 29 | parser.add_argument("--data-ascii", nargs=1, action="append", default=[]) |
| 30 | parser.add_argument("--data-binary", nargs=1, action="append", default=[]) |
| 31 | parser.add_argument("--data-urlencode", nargs=1, action="append", default=[]) |
| 32 | parser.add_argument("--data-raw", nargs=1, action="append", default=[]) |
| 33 | parser.add_argument("-F", "--form", nargs=1, action="append", default=[]) |
| 34 | parser.add_argument("--digest", action="store_true") |
| 35 | parser.add_argument("--ntlm", action="store_true") |
| 36 | parser.add_argument("--anyauth", action="store_true") |
| 37 | parser.add_argument("-e", "--referer", type=str) |
| 38 | parser.add_argument("-G", "--get", action="store_true", default=False) |
| 39 | parser.add_argument("-I", "--head", action="store_true") |
| 40 | parser.add_argument("-k", "--insecure", action="store_true") |
| 41 | parser.add_argument("-o", "--output", type=str) |
| 42 | parser.add_argument("-O", "--remote_name", action="store_true") |
| 43 | parser.add_argument("-r", "--range", type=str) |
| 44 | parser.add_argument("-u", "--user", type=str) |
| 45 | parser.add_argument("--url", type=str) |
| 46 | parser.add_argument("-A", "--user-agent", type=str) |
| 47 | parser.add_argument("--compressed", action="store_true", default=False) |
| 48 | |
| 49 | curl_split = shlex.split(curl_str) |
| 50 | try: |
| 51 | args = parser.parse_known_args(curl_split[1:])[0] |
| 52 | except: |
| 53 | raise ValueError("Could not parse arguments.") |
| 54 | |
| 55 | # 请求地址 |
| 56 | url = args.target_url |
| 57 | |
| 58 | # # 请求方法 |
| 59 | # try: |
| 60 | # method = args.request.lower() |
| 61 | # except AttributeError: |
| 62 | # method = args.request[0].lower() |
| 63 | |
| 64 | # 请求头 |
| 65 | headers = { |
| 66 | h[0].split(":", 1)[0]: ("".join(h[0].split(":", 1)[1]).strip()) |
| 67 | for h in args.header |
| 68 | } |
| 69 | if args.user_agent: |
| 70 | headers["User-Agent"] = args.user_agent |
| 71 | if args.referer: |
| 72 | headers["Referer"] = args.referer |
| 73 | if args.range: |
| 74 | headers["Range"] = args.range |
| 75 | |
| 76 | # Cookie |
| 77 | cookie_str = headers.pop("Cookie", "") or headers.pop("cookie", "") |
| 78 | cookies = tools.get_cookies_from_str(cookie_str) if cookie_str else {} |
| 79 | |
| 80 | # params |
no test coverage detected