可以查看win32和linux redhat 5下的cpu型号,平台是Python2.6+Django1.1.4
1、新建一个django项目,
django-admin.py startproject mysite;
2、修改urls.py,加上
-
urlpatterns = patterns('',
-
# Example:
-
# (r'^mysite/', include('mysite.foo.urls')),
-
-
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
-
# to INSTALLED_APPS to enable admin documentation:
-
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
-
-
# Uncomment the next line to enable the admin:
-
# (r'^admin/', include(admin.site.urls)),
-
url(r'^$','mysite.views.look_page'),
-
)
3、建立一个views.py,代码如下
-
# -*- coding: utf-8 -*-
-
import time
-
import os,sys,platform
-
from django.http import HttpResponse
-
-
def first_page(request):
-
return HttpResponse('
Hello World,你好
')
-
-
def look_page(request):
-
return HttpResponse('
cpu 型号是: '+ getcpu()+'
')
-
-
def getcpu():
-
sysname=platform.system()
-
# if sysname== 'Windows':
-
# return getwincpu()
-
# else:
-
return getlinuxcpu()
-
# return
-
-
#def getwincpu():
-
# import win32com.client
-
# s=""
-
# com=win32com.client.Dispatch("WbemScripting.SWbemRefresher")
-
# obj=win32com.client.GetObject("winmgmts:\\root\cimv2")
-
-
# processorItems=com.AddEnum(obj,"Win32_Processor").objectSet
-
-
# com.Refresh()
-
# for item in processorItems:
-
# s +=item.Name+"\r\n"
-
# return s
-
-
-
def read_cpu():
-
try:
-
fd=open("/proc/cpuinfo",'r')
-
lines=fd.readlines()
-
s=""
-
k=0
-
finally:
-
if fd:
-
fd.close()
-
for line in lines:
-
-
li=line.split(':')
-
if len(li) > 2:
-
continue
-
if li[0].startswith('model name'):
-
k+=1
-
s+="第"+ str(k) +"个cpu:"+ li[1]
-
return s
-
-
def getlinuxcpu():
-
cpustr=read_cpu()
-
if not cpustr:
-
return "no cpu info"
-
else:
-
return cpustr
4、进入django下的项目mysite,运行 python manage.py runserver 8000
5、打开浏览器 即可看到cpu 型号
阅读(2229) | 评论(0) | 转发(0) |