免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4752 | 回复: 1
打印 上一主题 下一主题

python socketserver [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-22 17:34 |只看该作者 |倒序浏览
童鞋们帮忙看下代码,找不到错误了!!!!

论坛徽章:
0
2 [报告]
发表于 2013-08-22 17:38 |只看该作者
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#使用/root/iplist.txt作为ip列表请填写好该文件,每行一个ip
#使用/root/urllist.txt作为ip列表请填写好该文件,每行一个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 '端口创建失败错误代码: ' + 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发送要推送的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()



服务器端
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#此程序要求squid访问日志保存在/cdn/squid/var/logs/access.log并且按照固定格式保存
#日志路径为/var/log/push/
#建立监听端口
import subprocess,fcntl,struct,time,os,sys,socket
from SocketServer import BaseRequestHandler, TCPServer
#获取本机ip的函数
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]
#创建日志字符串
                                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()



客户端执行结果
./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/
只有第一次循环执行了

服务器端结果
[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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP