Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7612313
  • 博文数量: 1769
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16352
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1769)

文章存档

2024年(15)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: LINUX

2011-08-16 10:49:29

 模块在浏览器里清除nginx缓存
apt-get install python2.5
wget
tar zxvf web.py-0.21.tar.gz
mv web.py-0.21.tar.gz  /usr/local/src/
cd webpy/
python setup.py install
#编辑python脚本 
vim purge.py
-------------------vim purge.py-----------------------------------------------------------------------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import web
import socket
import re
###
HOST = ["122.xxx.xxx.xxx","122.xxx.xxx.xxx"]
PORT = 80
###urls = (
        '/purge','purge',
        '/(.*)', 'purge',
                )
class hello:
        def GET(self, name):
                i = web.input(times=1)
                if not name:
                        name = 'world'
                for c in xrange(int(i.times)):
                        print 'Hello,',name+''
class purge:
        def _request(self):
                render = web.template.render('templates/')
                cache = False
                i = web.input(fname = '')               #取fname的值,默认为空
                if i.fname:
                        fName = i.fname
                        flag = self.purge_page(fName)
                        print render.purge(flag)        ##view 模板
                else:
                        print "在此处填加要清除缓存的URL"
                        fName = ""
                        print render.purge(fName)
        def purge_page(self,fName):
                url = fName
                try:
                        page =  '/'.join(url.split('/')[3:])                                            #page = 'page/20100503/1465789.html'
                        #regex = re.compile("^*\.html$")                                                 #正则表达式
                        regex = re.compile("^*")                                                      #正则表达式
                        domain = url.split('/')[2]
                except:
                        pass                if re.search(regex,fName):
                        for i in HOST:
                                print "服务器IP:"+ i + "  状态:"
                                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                                s.connect((i, PORT))
                                s.send('GET /purge/' + page +' HTTP/1.1\nHost:' + domain + '\n\n')
                                data = s.recv(1024)
                                ###检查是否有“404 Not Found” 或 "Successful purge" 字符串
                                success = "Successful purge"
                                found = "404 Not Found"
                                if re.search(success,data):
                                        print "Successful purge
"
                                elif re.search(found,data):
                                        print "该页面没有被缓存
"
                                else:
                                        print "未知错误...
"
                                s.close()
                else:
                        print "请重新输入URL"
                return ''
        def GET(self):
                self._request() #提交后的GET执行__reguest函数
        def POST(self):
                self._request() #提交后的GET执行__reguest函数if __name__ == "__main__":
    import os
    web.run(urls, globals())
-------------------------------------------------------------------------------------------------------
vim /home/purge/templates
------------------------------------------------------------------------
$def with (fName)"">

       
               

purge


       

                URL:
               
               

               



-------------------------------------------------------------------------
#启动
python purge.py 88
#访问显示如下:

转:
阅读(1045) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~