File "collect_info.py", line 109, in <module>

模块导进去了,报这个错,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

帮忙看下。
已邀请:

Not see︶ - IT界的拖后腿者。

赞同来自: 采菊篱下 坏蛋先生yjw

妈呀,终于找到原因了, 原因是我这个脚本里的product与我hostinfo应用模板的 views.py名不一致,导致报内部错误500

Not see︶ - IT界的拖后腿者。

赞同来自:

这块儿应该是没问题的呀

req = urllib2.urlopen('http://192.168.83.135:8000/hostinfo/collect/',d)
 

要回复问题请先登录注册