当前位置: 首页>>代码示例>>Python>>正文


Python CaseInsensitiveDict.strip方法代码示例

本文整理汇总了Python中requests.structures.CaseInsensitiveDict.strip方法的典型用法代码示例。如果您正苦于以下问题:Python CaseInsensitiveDict.strip方法的具体用法?Python CaseInsensitiveDict.strip怎么用?Python CaseInsensitiveDict.strip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在requests.structures.CaseInsensitiveDict的用法示例。


在下文中一共展示了CaseInsensitiveDict.strip方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import strip [as 别名]
def main(args=None,
         stdin=sys.stdin,
         stdin_isatty=sys.stdin.isatty(),
         stdout=sys.stdout,
         stdout_isatty=sys.stdout.isatty()):

    parser = cli.parser

    args = parser.parse_args(args if args is not None else sys.argv[1:])
    do_prettify = (args.prettify is True or
                     (args.prettify == cli.PRETTIFY_STDOUT_TTY_ONLY and stdout_isatty))

    # Parse request headers and data from the command line.
    headers = CaseInsensitiveDict()
    headers['User-Agent'] = DEFAULT_UA
    data = OrderedDict()
    try:
        cli.parse_items(items=args.items, headers=headers, data=data)
    except cli.ParseError as e:
        if args.traceback:
            raise
        parser.error(e.message)

    if not stdin_isatty:
        if data:
            parser.error('Request body (stdin) and request '
                                'data (key=value) cannot be mixed.')
        data = stdin.read()

    # JSON/Form content type.
    if args.json or (not args.form and data):
        if stdin_isatty:
            data = json.dumps(data)
        if 'Content-Type' not in headers and (data or args.json):
            headers['Content-Type'] = TYPE_JSON
    elif 'Content-Type' not in headers:
        headers['Content-Type'] = TYPE_FORM

    # Fire the request.
    try:
        response = requests.request(
            method=args.method.lower(),
            url=args.url if '://' in args.url else 'http://%s' % args.url,
            headers=headers,
            data=data,
            verify=True if args.verify == 'yes' else args.verify,
            timeout=args.timeout,
            auth=(args.auth.key, args.auth.value) if args.auth else None,
            proxies=dict((p.key, p.value) for p in args.proxy),
            files=dict((os.path.basename(f.name), f) for f in args.file),
            allow_redirects=args.allow_redirects,
        )
    except (KeyboardInterrupt, SystemExit):
        sys.stderr.write('\n')
        sys.exit(1)
    except Exception as e:
        if args.traceback:
            raise
        sys.stderr.write(str(e.message) + '\n')
        sys.exit(1)

    # Reconstruct the raw response.
    encoding = response.encoding or 'ISO-8859-1'
    original = response.raw._original_response
    status_line, headers, body = (
        'HTTP/{version} {status} {reason}'.format(
            version='.'.join(str(original.version)),
            status=original.status, reason=original.reason,
        ),
        str(original.msg).decode(encoding),
        response.content.decode(encoding) if response.content else u''
    )

    if do_prettify:
        prettify = pretty.PrettyHttp(args.style)
        if args.print_headers:
            status_line = prettify.headers(status_line)
            headers = prettify.headers(headers)
        if args.print_body and 'Content-Type' in response.headers:
            body = prettify.body(body, response.headers['Content-Type'])

    # Output.
    # TODO: preserve leading/trailing whitespaces in the body.
    #        Some of the Pygments styles add superfluous line breaks.
    if args.print_headers:
        stdout.write(status_line.strip())
        stdout.write('\n')
        stdout.write(headers.strip().encode('utf-8'))
        stdout.write('\n\n')
    if args.print_body:
        stdout.write(body.strip().encode('utf-8'))
        stdout.write('\n')
开发者ID:Rendez,项目名称:httpie,代码行数:94,代码来源:__main__.py


注:本文中的requests.structures.CaseInsensitiveDict.strip方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。