2

我正在运行一个简单的 python 脚本来使用 squid url_rewriter_program 记录访问的 url。但是,每次运行时,重写器都会在 sys.stdout.flush() 处因管道损坏错误而崩溃。

请提出具体的解决方案。

蟒蛇代码是:

import sys
import os
import io

line = sys.stdin.readline()
fo=open("/home/linux/Desktop/foo1.txt","a")
fo.write(line)
fo.close()
sys.stdout.write("\n")
sys.stdout.flush()
4

1 回答 1

0

这是一个 squid 重定向器文件,用 python 编写,你可以得到,或者与你的脚本比较:

重定向器:

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

#-----------------------------------------------------------------------------
# Name:        redirector_master.py
# Purpose:     SiSCont checker cuote
#
# Author:      Ernesto Licea Martin
#
# Created:     2011/11/24
# RCS-ID:      $Id: redirector_master.py $
# Copyright:   (c) 2011
# Licence:     GPL
#-----------------------------------------------------------------------------

import sys, string, libSiSCont

__name__= "redirector_master"
query="SELECT accounts_proxyquotatype.name, accounts_proxyaccount.proxy_quota, accounts_proxyaccount.proxy_quota_extra, accounts_proxyaccount.proxy_active, accounts_proxyaccounttype.name FROM accounts_proxyaccount INNER JOIN accounts_proxyaccounttype INNER JOIN accounts_proxyquotatype ON (accounts_proxyaccount.proxy_quota_type_id = accounts_proxyquotatype.id) AND  (accounts_proxyaccount.proxy_account_type_id = accounts_proxyaccounttype.id) WHERE accounts_proxyaccount.proxy_username=%s"



class RedirMaster:

    def __init__(self):
        obj = libSiSCont.ParceConf()
        obj.parcecfgfile()
        self.__listModules = obj.getModList()
        self.__redirDicc = obj.getRedirectURL()
        self.__penalURL = obj.getPenalizedURL()
        self.__confDicc = obj.getConfParam()
        self.__dbDicc = obj.getDBDicc()
        self.__proxyDicc = obj.getProxyCacheParam()
        self.__dbParam = []

    def getProxyTypes(self):
        db=libSiSCont.connectDB(dbDicc=self.__dbDicc)
        c=db.cursor()
        c.execute("SELECT accounts_proxy")

    def run(self):

        modules=[]
        for mod in self.__listModules:
            try:
                m=__import__(mod)
                modules.append(m)

            except Exception, e:
                libSiSCont.reportLogs("%s.run" %__name__, 'Unable to load redirector module %s; the error was: %s' % (mod,str(e)))

        if len(modules) == 0:
            libSiSCont.reportLogs("%s.run" %__name__, 'No usable redirector module found; switching to trasparent behavour')

        while 1:
            try:

                data_redir=raw_input()
                data_redir=data_redir.split()
                url,ip_host,user,method,urlgroup = data_redir[0:5]
                ip=ip_host.split("/")[0]
                host_name=ip_host.split("/")[1]
                uri = url
                mode=""

                #Don't check cache access
                if string.find(url,"cache_object") == 0:
                    sys.stdout.write("%s%s\n" %(mode,uri))
                    sys.stdout.flush()
                    continue


                db=libSiSCont.connectDB(dbDicc=self.__dbDicc)
                c=db.cursor()
                c.execute(query,user)
                cuote_type,cuote, ext_cuote, active, acc_type = c.fetchall()[0]
                self.__dbParam=[cuote_type,int(cuote), int(ext_cuote), active, acc_type]

                for module in modules:
                    try:
                        uri = module.redir(url = url, ip = ip, host_name = host_name, user = user, method = method, urlgroup = urlgroup, redirDicc = self.__redirDicc, penalURL = self.__penalURL, confDicc = self.__confDicc, proxyDicc = self.__proxyDicc, dbParam = self.__dbParam)

                    except Exception, e:
                        libSiSCont.reportLogs("%s.run" %__name__, 'Error while running module: %s -- The error was: %s' % (module,str(e)))


                    if uri != url:
                        mode = "301:"
                        break

                sys.stdout.write("%s%s\n" %(mode,uri))
                sys.stdout.flush()


            except Exception, e:
                if not string.find('%s' % e,'EOF') >= 0:
                    sys.stdout.write('%s\n' % uri)
                    sys.stdout.flush()
                    libSiSCont.reportLogs("%s.run" %__name__, '%s: data received from parent: %s' % (str(e),string.join(data_redir)))
                else:
                    sys.exit()


obj=RedirMaster()
obj.run()

帮手:

#!/usr/bin/env python

import sys, syslog, libSiSCont, string,crypt

__name__ = "Helper"

query = "SELECT accounts_proxyaccount.proxy_username FROM accounts_proxyaccount WHERE accounts_proxyaccount.proxy_username=%s AND accounts_proxyaccount.proxy_password=%s"

class BasicAuth:

    def __init__(self):
        obj = libSiSCont.ParceConf()
        obj.parcecfgfile()
        self.__dbDicc = obj.getDBDicc()

    def run(self):

        while 1:

            try:
                user_pass = string.split(raw_input())
                user = user_pass[0].strip("\n")
                passwd = user_pass[1].strip("\n")
                crypt_passwd = crypt.crypt(passwd,user)

                db = libSiSCont.connectDB(self.__dbDicc)
                c = db.cursor()
        c.execute(query,(user,crypt_passwd))
                if c.fetchone() == None:
                    libSiSCont.reportLogs('%s.run' %__name__,'User Authentication Fail, user = %s password= %s, Access Denied' %(user,passwd) )
                    sys.stdout.write("ERR\n")
                    sys.stdout.flush()
                else:
                    libSiSCont.reportLogs('%s.run' %__name__, 'User Authentication Success, user = %s, Access Granted' %user)
                    sys.stdout.write("OK\n")
                    sys.stdout.flush()


            except Exception, e:
                if not string.find("%s" %e, "EOF") >= 0:
                    sys.stdout.write("ERR\n")
                    sys.stdout.flush()
                    libSiSCont.reportLogs('%s.run' %__name__, 'Authenticator error, user will navigate without authentication: %s' %str(e))
                else:
                    sys.exit()
obj = BasicAuth()
obj.run()

我希望能帮助你;-)

于 2014-03-11T05:04:50.350 回答