2015-05-31 112 views
-2

我已經寫在python的web服務器獲取網址,客戶端使用網絡瀏覽器發送的請求是這樣的:如何從Python客戶端請求

http://localhost:13555/ChessBoard_x16_y16.bmp 

在服務器端,當我在打印客戶端 - 請求是這樣的:

GET /ChessBoard_x16_y16.bmp HTTP/1.1 
Host: localhost:13555 
Connection: keep-alive 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0 
.8 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/43.0.2357.81 Safari/537.36 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 


GET /favicon.ico HTTP/1.1 
Host: localhost:13555 
Connection: keep-alive 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/43.0.2357.81 Safari/537.36 
Accept: */* 
Referer: http://localhost:13555/ChessBoard_x16_y16.bmp 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 

但我只想要得到&打印實際的URL,如:

Referer: http://localhost:13555/ChessBoard_x16_y16.bmp 

請告訴我該怎麼做?

+0

的URL應該從得到的**' GET'行**,而不是favicon上的Referer頭。 –

+0

爲什麼這個問題幾乎完全重複[this one](http://stackoverflow.com/questions/30552358/how-to-get-integer-values-from-a-url-request)? – MattDMo

回答

0

不要在結果的定期REG-EX搜索蟒蛇內設置:

import re 
for line in <your results set>: 
    line = line.rstrip() 
    if re.search('^Referer:', line) : 
     print line 

如果你沒有REG-EX:

for line in <your results set>: 
    line = line.rstrip() 
    if '^Referer:' in line) : 
     print line 
+0

我正在使用Windows –

+0

@SajalAli:...和Windows上的Python沒有正則表達式支持?新聞給我! –