JSON

info.py, line 109, in module

字号+ 作者:H5之家 来源:H5之家 2017-05-04 10:00 我要评论( )

File collect_info.py, line 109, in module - 模块导进去了,报这个错,Python版本:Python 2.6.6程序编码#!/usr/bin/env pythonimport urllib,urllib2from sub

模块导进去了,报这个错,Python版本:Python 2.6.6

程序编码
#!/usr/bin/env python

import urllib,urllib2

from subprocess import Popen, PIPE

def getIfconfig():
p = Popen(['ifconfig'], stdout=PIPE)
data = p.stdout.read()
return data

def getDmi():
p = Popen(['dmidecode'], stdout=PIPE)
data = p.stdout.read()
return data

def parseData(data):
parsed_data = []
new_line = ''
data = [i for i in data.split('\n') if i]
for line in data:
if line[0].strip():
parsed_data.append(new_line)
new_line = line+'\n'
else:
new_line += line+'\n'
parsed_data.append(new_line)
return [i for i in parsed_data if i]

def parseIfconfig(parsed_data):
dic = {}
parsed_data = [i for i in parsed_data if not i.startswith('lo')]
for lines in parsed_data:
line_list = lines.split('\n')
devname = line_list[0].split()[0]
macaddr = line_list[0].split()[-1]
ipaddr = line_list[1].split()[1].split(':')[1]
break
dic['ip'] = ipaddr
return dic

def parseDmi(parsed_data):
dic = {}
parsed_data = [i for i in parsed_data if i.startswith('System Information')]
parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i]
dmi_dic = dict([i.strip().split(':') for i in parsed_data])
dic['vendor'] = dmi_dic['Manufacturer'].strip()
dic['produ'] = dmi_dic['Product Name'].strip()
dic['sn'] = dmi_dic['Serial Number'].strip()[:10]
return dic

def getHostname(f):
with open(f) as fd:
for line in fd:
if line.startswith('HOSTNAME'):
hostname = line.split('=')[1].strip()
break
return {'hostname':hostname}

def getOSver(f):
with open(f) as fd:
for line in fd:
osver = line.strip()
break
return {'osver':osver}

def getCpu(f):
num = 0
with open(f) as fd:
for line in fd:
if line.startswith('processor'):
num += 1
if line.startswith('model name'):
# print line
cpu_model = line.split(':')[1].split()
cpu_model = cpu_model[0]+' '+cpu_model[-1]
return {'cpu_num':num, 'cpu_model':cpu_model}

def getMemory(f):
with open(f) as fd:
for line in fd:
if line.startswith('MemTotal'):
mem = int(line.split()[1].strip())
break
mem = "%s" % int(mem/1024.0)+'M'
return {'memory':mem}


if __name__ == '__main__':
dic = {}
data_ip = getIfconfig()
parsed_data_ip = parseData(data_ip)
ip = parseIfconfig(parsed_data_ip)
data_dmi = getDmi()
parsed_data_dmi = parseData(data_dmi)
dmi = parseDmi(parsed_data_dmi)
hostname = getHostname('/etc/sysconfig/network')
osver = getOSver('/etc/issue')
cpu = getCpu('/proc/cpuinfo')
mem = getMemory('/proc/meminfo')
dic.update(ip)
dic.update(dmi)
dic.update(hostname)
dic.update(osver)
dic.update(cpu)
dic.update(mem)
print dic
d = urllib.urlencode(dic)
req = urllib2.urlopen('http://192.168.83.135:8000/hostinfo/collect/',d)
print req.read()报错:
[root@localhost devops]# python collect_info.py
{'osver': 'CentOS release 6.4 (Final)', 'vendor': 'VMware, Inc.', 'sn': 'VMware-56 ', 'memory': '981M', 'cpu_num': 1, 'ip': '192.168.83.135', 'hostname': 'localhost.localdomain', 'produ': 'VMware Virtual Platform', 'cpu_model': 'Intel(R) 2.50GHz'}
Traceback (most recent call last):
File "collect_info.py", line 109, in <module>
req = urllib2.urlopen('http://192.168.83.135:8000/hostinfo/collect/',d)
File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib64/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
帮忙看下。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • fatal error:jsoncpp/json/json.h:No such file or directory

    fatal error:jsoncpp/json/json.h:No such file or directory

    2015-11-24 10:13

  • Linux下修复“fatal error: jsoncpp/json/json.h: No such file

    Linux下修复“fatal error: jsoncpp/json/json.h: No such file

    2015-10-10 14:05

网友点评
s