2016-03-03 3 views
0

Типичный HTTP 1.0 заголовок выглядит следующим образом:Зачистка заголовки ответа - Python

Server: nginx/1.6.2 (Ubuntu) 
Date: Thu, 03 Mar 2016 07:00:00 GMT 
Content-Type: text/html 
Content-Length: 13471 
Last-Modified: Sat, 19 Dec 2015 02:42:32 GMT 
Connection: close 
ETag: "5674c418-349f" 
Cache-Control: no-store 
Accept-Ranges: bytes 

<!doctype html> // or <!DOCTYPE html> 
# remaining of the page content here. 

Что это самый простой способ для меня, чтобы отделить начало страницы (отмеченный <!doctype html> или <!DOCTYPE html> из заголовка HTTP запроса? Например

response = get_response() # get response is a string containing the page. 
tokens = response.split("<!doctype html>") # won't work well. 
return ''.join(tokens) 

не будет хорошо работать. Я искал в способ разделить между первой половиной (ответ заголовка) и второй половиной (тело)

ответ

2

Вы могли бы просто использовать find() со строчной версии ответа следующим образом:

response = """ 
Server: nginx/1.6.2 (Ubuntu) 
Date: Thu, 03 Mar 2016 07:00:00 GMT 
Content-Type: text/html 
Content-Length: 13471 
Last-Modified: Sat, 19 Dec 2015 02:42:32 GMT 
Connection: close 
ETag: "5674c418-349f" 
Cache-Control: no-store 
Accept-Ranges: bytes 

<!doctype html> // or <!DOCTYPE html> 
# remaining of the page content here. 
""" 

print response[response.lower().find('<!doctype html>'):] 

Это будет печатать:

<!doctype html> // or <!DOCTYPE html> 
# remaining of the page content here. 

или, возможно, просто искать <!doctype

+0

Это круто! спасибо – cybertextron

Смежные вопросы