【问题标题】:AttributeError - Namespace object has no attribute usernameAttributeError - 命名空间对象没有属性用户名
【发布时间】:2017-11-30 12:50:58
【问题描述】:

您好,我是 python 新手,尝试在脚本下运行并出现属性错误。

 #!/usr/bin/python
 import json
 import sys
 import argparse

  try:
    import requests
    except ImportError:
    print "Please install the python-requests module."
    sys.exit(-1)

   def get_json(location, devincuser, password):

    # Performs a GET using the passed URL location
    location += "?per_page=10000"
    r = requests.get(location, auth=(devincuser, password),verify=False)
    return r.json()

  def main():

    parser = argparse.ArgumentParser(description="Satellite Errata                
   Reporter")

    # Arguments
    parser.add_argument("-u", "--devincuser", type=str.lower, help="Username  
   to access Satellite", action="store", default='devincuser')
    parser.add_argument("-p", "--password", type=str, help="Password to  
   access Satellite", action="store", default='password')
    parser.add_argument("-n", "--localhost", type=str.lower, help="Satellite     
   server (default: localhost)", default='localhost')
    parser.add_argument("-o", "--organization", type=str.lower, nargs="*",  
  help="Filter on this space-delimited list of organization(s)", default='')
    parser.add_argument("-e", "--environment", type=str.lower, nargs="*",  
  help="Filter on this space-delimited list of lifecycle environments",  
  default='')
    parser.add_argument("-c", "--collection", type=str.lower, nargs="*",  
   help="Filter on and group by this space-delimited list of host  
  collections", default='')
    parser.add_argument("-t", "--type", type=str.lower, nargs="*",  
  help="Filter on this space-delimeted list of errata types (bugfix,  
  enhancement, and/or security)", default='')
    parser.add_argument("-s", "--severity", type=str.lower, nargs="*",  
 help="Filter on this space-delimited list of severities (critical,  
 important, moderate, low)", default='')

    args = parser.parse_args()

    # Check username and password
    if not (args.username and args.password):
            print "No complete account information provided, exiting"
            exit (-1)

    # set up api url
    sat_api = "http://%s/172.17.12.129/api/v2/" % args.server
    katello_api = "http://%s/172.17.12.129/api/" % args.server

    # set up initial stuff
    prefix = "- "
    systems_fetched = False
    system_count = 0
    system_errata_count = 0

    # Loop through organizations and skip the ones we don't want
    orgs = get_json(sat_api + "organizations/", args.username,      
    args.password)
    for org in orgs['results']:
    if args.organization and org['name'].lower not in args.organization:
                    continue

  print "\nErrata for organization '%s':" % org['name']

  if args.collection:

  # Loop through the host collections, skip the one we don't want
    collections = get_json(sat_api + "organizations/" + str(org['id']) +  
 "/host_collections/", args.username, args.password)
    for collection in collections['results']:
   if collection['name'].lower() not in args.collection:
             continue
      print "\n" + prefix + "Errata for Host Collection '%s':" %  
collection['name']
            prefix = "  - "

  # Get the systems in this host collection
    systems = get_json(sat_api + "host_collections/" + str(collection['id']) 
+ "/systems/", args.username, args.password)
    systems_fetched = True

    else:
  # Get the systems in this Organization
  systems = get_json(sat_api + "organizations/" + str(org['id']) + 
"/systems/", args.username, args.password)
  systems_fetched = True

  if not systems_fetched:
  continue

  # loop through the systems fetched from the collection *or* the 
organization
  for system in systems['results']:
  system_errata = get_json(sat_api + "systems/" + system['uuid'] + 
"/errata", args.username, args.password)
  first = True

  # filter on lifecycle environment(s) (if specified)
  environment = system['environment']['name'].lower()
  if args.environment and environment not in args.environment:
  continue

  # Get all available Errata for System
  for system_erratum in system_errata['results']:

  # filter on type(s) (if specified)
   type = system_erratum['type'].lower()
   if args.type and type not in args.type:
    continue

  # filter on severity(s) (if specified)
   if type == "security" and "security" in args.type and args.severity:
   severity = system_erratum['title'].split(":")
[0].encode('ascii','ignore').lower()
   if severity not in args.severity:
   continue

  # We have an erratum, print system if this is the first
   if first:
   system_count += 1
   first = False
   print "\n" + prefix + system['name'], "("+system['environment'] 
['name']+")\n"

 # print the erratum id, type and title 
    print "  " + prefix + "%s: %s: %s" % 
(system_erratum['errata_id'],system_erratum['type'],system_erratum['title'])

  # Count the errata we find
    system_errata_count += 1

    if not first:
     print

 # print statistics
    if system_errata_count:
   print "\nNumber of errata to apply: %s" % system_errata_count
   print "Number of systems affected: %s" % system_count
   else:
    print "\nNo errata found for this selection"
    print
  if __name__ == "__main__":
    main()

并得到打击错误 回溯(最近一次通话最后): 文件“./pyth.py”,第 136 行,在 主要的() 文件“./pyth.py”,第 44 行,在 main 如果不是(args.username 和 args.password): AttributeError:“命名空间”对象没有属性“用户名”

【问题讨论】:

    标签: python json django shell


    【解决方案1】:

    您尚未在 ArgumentParser 中定义 username 参数。您已经定义了一个名为 devincuser 的东西,您是要改用它吗?

    【讨论】:

    • devincuser 是认证用户名
    • 那么“用户名”参数从何而来?你还没有定义它。
    • t要运行这个脚本指令 - "satellite_errata_report (使用 satellite_errata_report -h 或 --help 使用) 请使用只读帐户访问 Satellite API。这些可以设置为默认值在下面的用户名和密码的解析器参数中...下面这些值。或者,可以在命令行上指定用户名和密码“
    • 这些说明来自哪里?我不知道我要说多少次,你的脚本没有定义用户名参数
    猜你喜欢
    • 2015-04-09
    • 2021-11-14
    • 2018-09-03
    • 2020-10-01
    • 2021-03-11
    • 2020-05-17
    • 2021-11-20
    • 2017-01-12
    • 2018-09-23
    相关资源
    最近更新 更多