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


Python Request.body方法代码示例

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


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

示例1: main

# 需要导入模块: from requests import Request [as 别名]
# 或者: from requests.Request import body [as 别名]
def main():
    parser = argparse.ArgumentParser(prog='cmsRWSHelper')
    parser.add_argument(
        '-v', '--verbose', action='store_true',
        help="tell on stderr what's happening")
    # FIXME It would be nice to use '--rankings' with action='store'
    # and nargs='+' but it doesn't seem to work with subparsers...
    parser.add_argument(
        '-r', '--ranking', dest='rankings', action='append', default=None,
        choices=list(xrange(len(config.rankings_address))), metavar='shard',
        type=int, help="select which RWS to connect to (omit for 'all')")
    subparsers = parser.add_subparsers(
        title='available actions', metavar='action',
        help='what to ask the RWS to do with the entity')

    # Create the parser for the "get" command
    parser_get = subparsers.add_parser('get', help="retrieve the entity")
    parser_get.set_defaults(action='get')

    # Create the parser for the "create" command
    parser_create = subparsers.add_parser('create', help="create the entity")
    parser_create.set_defaults(action='create')
    parser_create.add_argument(
        'file', type=argparse.FileType('rb'),
        help="file holding the entity body to send ('-' for stdin)")

    # Create the parser for the "update" command
    parser_update = subparsers.add_parser('update', help='update the entity')
    parser_update.set_defaults(action='update')
    parser_update.add_argument(
        'file', type=argparse.FileType('rb'),
        help="file holding the entity body to send ('-' for stdin)")

    # Create the parser for the "delete" command
    parser_delete = subparsers.add_parser('delete', help='delete the entity')
    parser_delete.set_defaults(action='delete')

    # Create the group for entity-related arguments
    group = parser.add_argument_group(
        title='entity reference')
    group.add_argument(
        'entity_type', action='store', choices=ENTITY_TYPES, metavar='type',
        help="type of the entity (e.g. contest, user, task, etc.)")
    group.add_argument(
        'entity_id', action='store', type=six.text_type, metavar='id',
        help='ID of the entity (usually a short codename)')

    # Parse the given arguments
    args = parser.parse_args()

    args.entity_id = quote(args.entity_id)

    if args.verbose:
        verb = args.action[:4] + 'ting'
        logger.info("%s entity '%ss/%s'" % (verb.capitalize(),
                                            args.entity_type, args.entity_id))

    if args.rankings is not None:
        shards = args.rankings
    else:
        shards = list(xrange(len(config.rankings_address)))

    s = Session()
    error = False

    for shard in shards:
        url, username, password = get_parameters(
            shard, args.entity_type, args.entity_id)

        if args.verbose:
            logger.info(
                "Preparing %s request to %s (username: %s; password: %s)" %
                (ACTION_METHODS[args.action], url, username, password))

        req = Request(ACTION_METHODS[args.action],
                      url, auth=(username, password)).prepare()

        if hasattr(args, 'file'):
            if args.verbose:
                logger.info("Reading file contents to use as message body")
            req.body = args.file.read()

        if args.verbose:
            logger.info("Sending request")

        try:
            res = s.send(req, verify=config.https_certfile)
        except RequestException as e:
            logger.error("Failed")
            logger.info(repr(e))
            error = True
            continue

        if args.verbose:
            logger.info("Response received")

        if res.status_code != (201 if args.action == "create" else 200):
            logger.error("Unexpected status code: %d" % res.status_code)
            error = True
            continue
#.........这里部分代码省略.........
开发者ID:s546360316,项目名称:cms,代码行数:103,代码来源:RWSHelper.py


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