2015-05-31 114 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没有正则表达式支持?新闻给我! –