2012-02-02 143 views
0

獲取環境變量,當我使用一個外部bash腳本對我的代理我得到的所有的環境變量下的這個腳本的蟒蛇從代理

#!/bin/bash 

CAPTURE_FILE=/var/log/capture_data 
env >> ${CAPTURE_FILE} 

# we use exit code 1 to ensure this does not effect the actual browsing 
exit 1 
# 

輸出,當客戶訪問網頁:

HTTP_PORT=80 
HTTP_HOST=ads.cnn.com 
SERVER[adserver]=ad3ad3:9678:1 
CLIENT[referer]=http://edition.cnn.com/ 
HTTP_PROTO=http 
CLIENT[host]=ads.cnn.com 
SERVER[vary]=Cookie 
SERVER[connection]=Keep-Alive 
CLIENTID=2 
[email protected] 
SERVER[keep-alive]=timeout=5, max=15 
SERVER[date]=Thu, 02 Feb 2012 12:09:46 GMT 
SERVER[content-type]=text/html 
CLIENT[user-agent]=Safari 
PWD=/ 
VERSION=SR.4.2.2.MR.20110523 

現在,我用os.environ for python(感謝以前的帖子之一),它的工作原理,但只能從終端,而不是代理傳遞所有請求時

#!/usr/bin/env python 

import os 
import sys 

def capture(): 

    log = os.environ 
    data = open("/tmp/capture.log", "a") 
    for key in log.keys(): 
     data.write((key)) 
     data.write(" : ") 
     for n in log[key]: 
      data.write('%s' % ((n))) 
     data.write("\n") 
    data.close() 
    sys.exit(1) 

def main(): 

    capture() 

if __name__ == "__main__": 
    main() 

我可以閱讀從標準sys.stdin.readlines(數據),但我從當代理請求重定向到腳本中的環境變量更精確的結果......

爲什麼python腳本做任何想法不顯示任何數據?

日誌在/ var/log/messages中

Feb 3 22:29:02 safesquid capture.py: abrt: detected unhandled Python exception in /opt/safesquid/safesquid/scripts/capture.py 
Feb 3 22:30:00 safesquid capture.py: abrt: detected unhandled Python exception in /opt/safesquid/safesquid/scripts/capture.py 
Feb 3 22:30:00 safesquid capture.py: abrt: detected unhandled Python exception in /opt/safesquid/safesquid/scripts/capture.py 
Feb 3 22:30:01 safesquid capture.py: abrt: detected unhandled Python exception in /opt/safesquid/safesquid/scripts/capture.py 

解決:

我移植在同一個腳本至CentOS 6.2和它的工作....看來,這對Fedora的問題。

+0

咦?它沒有顯示任何數據,因爲你從未告訴過它。你想達到什麼目的? – 2012-02-02 12:24:54

+0

我只想讀取從代理傳遞的環境變量.. – krisdigitx 2012-02-02 12:31:21

+0

您能否給我們提供一些關於您的設置和您想要實現的更多信息? – 2012-02-02 13:40:48

回答

1

如果您的輸出與unix env命令的輸出相同,您可以直接調用它嗎?

log = subprocess.check_output("env") 
+0

不,當流量從代理到腳本時,它不記錄..工作正常,如果我從命令行傳遞STDIN數據 – krisdigitx 2012-02-02 14:27:11