2012-02-01 64 views
2

我將代理服務器重定向到腳本,該腳本檢查URL,User-Agents等,並根據檢查結果將用戶返回給指定的URL。python sys.stdout.write()redirect

現在,如果我發送GET請求回到客戶端它的工作,但它只顯示文本版本的網站沒有CSS /圖像,這是代碼的一部分發送請求回客戶端..

if l.startswith('User-Agent:') and ('Safari' in l): 

    new = "http://www.yahoo.com" 
    new_get = "GET" + " " + str(new) + " " + "HTTP/1.1" + "Status: 302" + "\r\n\r\n" 
    sys.stdout.write(new_get) 
    sys.stdout.flush() 

我試圖設置

new = "302:http://www.yahoo.com" 

但也不管用...

任何想法?

更新:

if l.startswith('User-Agent:') and ('Safari' in l): 

    new = "http://www.yahoo.com" 
    new_get = "GET" + " " + str(new) + " " + "HTTP/1.1" + "Status: 302" + "\r\n\r\n" 
    uopen = urllib2.urlopen("http://www.yahoo.com") 
    sys.stdout.write(uopen.read()) 
    sys.stdout.flush() 

試圖用的urllib2及其需要大量的時間用於瀏覽器渲染page..but相同results..no圖像/ CSS ..

回答

-1

爲什麼你是用手做這一切的嗎? Python標準庫包含模塊,如urllib2httplib,甚至SimpleHTTPServer可幫助您執行類似任務。

+1

但這項工作時發送的標準輸出回代理爲客戶... – krisdigitx 2012-02-01 17:29:43

+0

@kris:爲什麼不呢? – cha0site 2012-02-01 17:32:35

+0

嘗試它不工作..我已經粘貼上面的代碼.. – krisdigitx 2012-02-01 17:45:00

0

如果你想重定向客戶端:

new_url = 'http://www.yahoo.com/' 

print 'Status:', '302 Found' 
print 'Location:', new_url 
print 

相反,如果你想直接服務的東西給客戶端:

from shutil import copyfileobj 
from sys import stdout 
from urllib2 import urlopen 

http_message = urlopen("http://www.yahoo.com") 
copyfileobj(http_message, stdout)