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'),
- )
点击(此处)折叠或打开
- # -*- 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
5、打开浏览器 http://127.0.0.1:8000 即可看到cpu 型号