python web py官网_python web.py
web.py
1.安裝:
wget http://webpy.org/static/web.py-0.37.tar.gz
tar?xvfz?web.py-0.37.tar.gz
cd?web.py-0.37
sudo?python?setup.py?install
2.需求:
先說下需求,http://10.75.7.237:8080/?ipaddress=10.75.7.29&count=100?,最終得到這個ip的日志的前100行
3.講解:
#!/usr/bin/python
import web
render = web.template.render(‘templates/‘) 這是告訴你的web展現頁面放在那個位置
urls = (
‘/‘, ‘index‘
)
這是告訴你訪問/路徑時所調用的類是index
class index:
def reset_sigpipe(self):
signal.signal(signal.SIGPIPE,signal.SIG_DFL)
def GET(self):
ip=web.input(ipaddress=None,count=None) ?讓url可以傳輸多個值名為ipaddress和count
print ip[‘ipaddress‘]
print ip[‘count‘]
a=ip[‘ipaddress‘]
address_dict={‘10.13.144.‘:‘bx‘,‘10.73.26.‘:‘tc‘,‘10.75.7.‘:‘yf‘,‘10.67.15.‘:‘yq‘}
aa=a.split(".")
b=aa[0:3]
d=aa[-1]
e=".".join(b)
c=e+"."
address_1=address_dict[c]
f=str(d)
g=address_1+f
time_day=time.strftime(‘%Y-%m-%d‘)
#h_1=subprocess.Popen(‘grep -R %s /data1/saelog/%s/error/‘%(g,time_day),shell=True,preexec_fn=self.reset_sigpipe(),stdout=subprocess.PIPE)
#h=h_1.stdout.read()
h = os.popen(‘grep -R %s /data1/saelog/%s/error/‘ % (g,time_day))
print h
head_list=[]
sum=0
dict={}
head_lines=ip[‘count‘]
print head_lines
if head_lines:
for i in h:
j=i.split(":")[1:]
head_list.append(j)
for kk in head_list:
dict[sum]=head_list[sum]
sum+=1
if sum == int(head_lines):
break;
dict_json=json.dumps(dict)
else:
for i in h:
j=i.split(":")[1:]
head_list.append(j)
for kk in head_list:
dict[sum]=head_list[sum]
sum+=1
dict_json=json.dumps(dict)
print dict_json
return render.index(dict_json) ? ? ? 傳給頁面展示的值
#return "Hello, world!"
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
templates目錄下
index.html
$def with (name)
$if name:
$name ? ?name為代碼傳過來的值
4.啟動web.py
python test.py ?后面不跟端口默認就是8080
5.訪問http://10.75.7.237:8080/?ipaddress=10.75.7.29&count=100
總結
以上是生活随笔為你收集整理的python web py官网_python web.py的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot任务——JSoup+
- 下一篇: python concat去除重复值语句