开发手册 欢迎您!
软件开发者资料库

Python 获取解析curl命令行字符串中参数转换成字典(Dictionary)

本文主要介绍Python中,解析获取curl命令行参数字符串,并且将获取到的参数转换成字典(Dictionary)方法,以及相关的示例代码。

1、curl示例命令及参数

  curl_command = (            "curl 'http://httpbin.org/post' -X POST -H 'Cookie: _gauges_unique"            "_year=1; _gauges_unique=1; _gauges_unique_month=1; _gauges_unique"            "_hour=1; _gauges_unique_day=1' -H 'Origin: http://httpbin.org' -H"            " 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q"            "=0.9,ru;q=0.8,es;q=0.7' -H 'Upgrade-Insecure-Requests: 1' -H 'Use"            "r-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTM"            "L, like Gecko) Ubuntu Chromium/62.0.3202.75 Chrome/62.0.3202.75 S"            "afari/537.36' -H 'Content-Type: application/x-www-form-urlencoded"            "' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0"            ".9,image/webp,image/apng,*/*;q=0.8' -H 'Cache-Control: max-age=0'"            " -H 'Referer: http://httpbin.org/forms/post' -H 'Connection: keep"            "-alive' --data 'custname=John+Smith&custtel=500&custemail=jsmith%"            "40example.org&size=small&topping=cheese&topping=onion&delivery=12"            "%3A15&comments=' --compressed"        )

2、获取解析curl命令行字符串及转换字典(Dictionary)代码

安装引用w3lib

pip install w3lib

示例代码是针对curl命令的,如果需要获取解析其它命令字符串,也可以参考如下代码:

import argparseimport warningsfrom shlex import splitfrom http.cookies import SimpleCookiefrom urllib.parse import urlparsefrom w3lib.http import basic_auth_headerclass CurlParser(argparse.ArgumentParser):    def error(self, message):        error_msg = \            'There was an error parsing the curl command: {}'.format(message)        raise ValueError(error_msg)curl_parser = CurlParser()curl_parser.add_argument('url')curl_parser.add_argument('-H', '--header', dest='headers', action='append')curl_parser.add_argument('-X', '--request', dest='method', default='get')curl_parser.add_argument('-d', '--data', dest='data')curl_parser.add_argument('-u', '--user', dest='auth')safe_to_ignore_arguments = [    ['--compressed'],    # `--compressed` argument is not safe to ignore, but it's included here    # because the `HttpCompressionMiddleware` is enabled by default    ['-s', '--silent'],    ['-v', '--verbose'],    ['-#', '--progress-bar']]for argument in safe_to_ignore_arguments:    curl_parser.add_argument(*argument, action='store_true')def curl_to_request_kwargs(curl_command, ignore_unknown_options=True):    """Convert a cURL command syntax to Request kwargs.    :param str curl_command: string containing the curl command    :param bool ignore_unknown_options: If true, only a warning is emitted when    cURL options are unknown. Otherwise raises an error. (default: True)    :return: dictionary of Request kwargs    """    curl_args = split(curl_command)    if curl_args[0] != 'curl':        raise ValueError('A curl command must start with "curl"')    parsed_args, argv = curl_parser.parse_known_args(curl_args[1:])    if argv:        msg = 'Unrecognized options: {}'.format(', '.join(argv))        if ignore_unknown_options:            warnings.warn(msg)        else:            raise ValueError(msg)    url = parsed_args.url    # curl automatically prepends 'http' if the scheme is missing, but Request    # needs the scheme to work    parsed_url = urlparse(url)    if not parsed_url.scheme:        url = 'http://' + url    result = {'method': parsed_args.method.upper(), 'url': url}    headers = []    cookies = {}    for header in parsed_args.headers or ():        name, val = header.split(':', 1)        name = name.strip()        val = val.strip()        if name.title() == 'Cookie':            for name, morsel in SimpleCookie(val).items():                cookies[name] = morsel.value        else:            headers.append((name, val))    if parsed_args.auth:        user, password = parsed_args.auth.split(':', 1)        headers.append(('Authorization', basic_auth_header(user, password)))    if headers:        result['headers'] = headers    if cookies:        result['cookies'] = cookies    if parsed_args.data:        result['body'] = parsed_args.data    return result

调用方法:

import curl_to_request_kwargs#忽略未知参数result = curl_to_request_kwargs(curl_command)#不忽略未知参数result = curl_to_request_kwargs(curl_command,False)