2014-03-07 57 views
2

我正在運行一個簡單的python腳本來使用squid url_rewriter_program記錄訪問的url。 但是每次運行時,重寫器都會在sys.stdout.flush()處發生故障,併發生故障。魷魚python腳本中的管道故障

請提出具體的解決方案。

Python代碼是:

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() 
+0

請問你能發表錯誤嗎? – durga

回答

0

這是魷魚重定向文件,寫在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() 

我希望能幫到你;-)