有段程序报a bytes-like object is required, not 'str'

Davelu 2019-12-31 10:10:44
源程序为


def advanced_info_of_resource(resource, needed_attributes, storwize_connection, *id_of_resource):

""" needed_attributes - list of parameters, that we wont to get
id_of_resource - list of additional parameters, that uniquely determine resource.
Example: for PSU - first element of list is enclosure_id, secondary element of list is PSU_id"""


if resource == 'lsenclosure':
stdin, stdout, stderr = storwize_connection.exec_command('svcinfo {0} {1}'.format(resource, id_of_resource[0]))
elif resource == 'lsenclosurepsu':
stdin, stdout, stderr = storwize_connection.exec_command('svcinfo {0} -psu {1} {2}'.format(resource, id_of_resource[1], id_of_resource[0]))

if len(stderr.read()) > 0:
storwize_logger.info("Error Occurs in advanced info of enclosure - {0}".format(stderr.read()))
storwize_logout(storwize_connection)
sys.exit("1100")
else:
attributes_of_resource = stdout.read() # (variable contain advanced attributes in string)
dict_of_attributes = {} # (will contain advanced attributes in key-value)
try:
for attribute in attributes_of_resource.split('\n'):
if len(attribute) > 0:
temp = attribute.split(' ')
dict_of_attributes[temp[0]] = temp[1]
except Exception as oops:
storwize_logger.error("Error occures in function advanced_info_of_resource - {0}".format(oops))
storwize_logout(storwize_connection)
sys.exit("1100")

# (create dictionary that contain properies of resource)
result = {}
for each_value in needed_attributes:
result[each_value] = dict_of_attributes[each_value]

return result


报错信息为:ERROR - Error occures in function advanced_info_of_resource - a bytes-like object is required, not 'str',我应该怎么调整?


...全文
241 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Davelu 2020-01-01
  • 打赏
  • 举报
回复
已经解决
def advanced_info_of_resource(resource, needed_attributes, storwize_connection, *id_of_resource):

""" needed_attributes - list of parameters, that we wont to get
id_of_resource - list of additional parameters, that uniquely determine resource.
Example: for PSU - first element of list is enclosure_id, secondary element of list is PSU_id"""


if resource == 'lsenclosure':
stdin, stdout, stderr = storwize_connection.exec_command('svcinfo {0} {1}'.format(resource, id_of_resource[0]))
elif resource == 'lsenclosurepsu':
stdin, stdout, stderr = storwize_connection.exec_command('svcinfo {0} -psu {1} {2}'.format(resource, id_of_resource[1], id_of_resource[0]))

if len(stderr.read()) > 0:
storwize_logger.info("Error Occurs in advanced info of enclosure - {0}".format(stderr.read()))
storwize_logout(storwize_connection)
sys.exit("1100")
else:
attributes_of_resource = stdout.read() # variable contain advanced attributes in string
attributes_of_resource = attributes_of_resource.decode()
dict_of_attributes = {} # will contain advanced attributes in key-value
try:
for attribute in attributes_of_resource.split('\n'): # Separate the lines and receive a list from a red attribute
if len(attribute) > 0:
temp = attribute.split(' ')
dict_of_attributes[temp[0]] = temp[1]
except Exception as oops:
storwize_logger.error("Error occures in function advanced_info_of_resource - {0}".format(oops, attributes_of_resource.decode()))
storwize_logout(storwize_connection)
sys.exit("1100")

# create dictionary that contain properies of resource
result = {}
for each_value in needed_attributes:
result[each_value] = dict_of_attributes[each_value]

return result

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧