亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4762 | 回復(fù): 1
打印 上一主題 下一主題

python socketserver [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-08-22 17:34 |只看該作者 |倒序?yàn)g覽
童鞋們幫忙看下代碼,找不到錯誤了!。!

論壇徽章:
0
2 [報告]
發(fā)表于 2013-08-22 17:38 |只看該作者
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#使用/root/iplist.txt作為ip列表請?zhí)顚懞迷撐募啃幸粋ip
#使用/root/urllist.txt作為ip列表請?zhí)顚懞迷撐募,每行一個url
客戶端
import socket,sys,os,time,re
#判斷IP列表是否存在
if os.path.exists('/root/iplist.txt')==False:
        print 'IPlist文件不存在!'
        sys.exit()
if os.path.exists('/root/urllist.txt')==False:
        print 'URLlist文件不存在!'
        sys.exit()
pattern= re.compile(r'.*\.(com|cn|com\.cn|net|org|org\.cn|net\.cn|edu|cc|biz|co|gov\.cn|net\.cn|so)$')
urllist=file('/root/urllist.txt','r')
try:
        s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error,msg:
        print '端口創(chuàng)建失敗錯誤代碼: ' + str(msg[0]) + ' , 錯誤信息 : ' + msg[1]
        sys.exit();
s.bind(('',8111))
port=8111
if os.path.exists('/var/log/push/')==False:
        os.system('mkdir /var/log/push')
                print pushurl
target='/var/log/push/push-'+time.strftime('%Y%m%d')+'.log'
#向IP列表中每一個ip發(fā)送要推送的url
s.settimeout(5)
iplist=open('/root/iplist.txt')
while True:
        usrlist=open('/root/urllist.txt')
        line=iplist.readline()
        if len(line)==0:
                break
        ip=line.split()
        s.connect((ip[0],port))
        for url in urllist:
                if url.strip()[:7]=='http://':
                        pushurl=url.strip()
                else:
                        pushurl='http://'+url.strip()
                if pushurl.count('/')<3:
                        match=pattern.match(pushurl)
                        if match:
                                pushurl=match.group()
                                pushurl=pushurl+'/'
                print pushurl
                s.send(pushurl)
#接收返回日志
                try:
                        while True:
                                data=s.recv(1024)
                                if not data:break
                                if data.split()[2][0:4]=='HTTP':
                                        print data
#寫入日志
                                        writing=open('%s'%target,'a+')
                                        writing.writelines(data+'\n')
                                        writing.close()
                except socket.timeout:
                        pass
        urllist.close()
iplist.close()



服務(wù)器端
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#此程序要求squid訪問日志保存在/cdn/squid/var/logs/access.log并且按照固定格式保存
#日志路徑為/var/log/push/
#建立監(jiān)聽端口
import subprocess,fcntl,struct,time,os,sys,socket
from SocketServer import BaseRequestHandler, TCPServer
#獲取本機(jī)ip的函數(shù)
def get_ip_address(ifname):
        b = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(
        b.fileno(),
        0x8915, # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
)[20:24])
if os.path.exists('/var/log/push/')==False:
        os.system('mkdir /var/log/push')
target='/var/log/push/push-'+time.strftime('%Y%m%d')+'.log'
class EchoHandler(BaseRequestHandler):
        def handle(self):
                message=self.request.recv(65536)
                print message
                logfile=file('/cdn/squid/var/logs/access.log','r')
#接收推送url
                while True:
                        line=logfile.readline()
                        if len(line)==0:
                                break
                        log=line.split()
                        if log[7]==message and log[4]=='200':
                                han = subprocess.Popen('/cdn/squid/bin/squidclient -p 80 -m PURGE %s'%message, shell=True, stdout=subprocess.PIPE)
                                ss=han.communicate()[0]
#創(chuàng)建日志字符串
                                writestr=get_ip_address('eth0')+'\t'+log[7]+'\t'+ss[0:ss.find('Server:')].strip()+'\t\t'+ss[ss.find('Date:')+5:ss.find('Content-Length')].strip()+'\t'+time.strftime('%Y-%m-%d %H:%M:%S')
                                print writestr
#寫入日志
                                self.request.sendall(writestr)
                                writing=open('%s'%target,'a+')
                                writing.writelines(writestr+'\n')
                                writing.close()
                                logfile.close()
                                self.request.close()
                                break
TCPServer(('',8111), EchoHandler).serve_forever()



客戶端執(zhí)行結(jié)果
./purge.py
http://www.a.com/as.html
10.10.52.3      http://www.a.com/as.html        HTTP/1.0 404 Not Found          Thu, 22 Aug 2013 09:40:42 GMT2013-08-22 17:40:42
http://www.a.com/index.html
http://img1.iche.com/201302/20/1361344063_45604600.jpg
http://www.a.com/
只有第一次循環(huán)執(zhí)行了

服務(wù)器端結(jié)果
[root@squid-52-3 peter]# ./purgeclient.py
http://www.a.com/as.html
10.10.52.3      http://www.a.com/as.html        HTTP/1.0 404 Not Found          Thu, 22 Aug 2013 09:40:42 GMT2013-08-22 17:40:42
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP