Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a raw, unparsed HTTP response

Are there any straightforward ways to make a HTTP request and get at the raw, unparsed response (specifically the headers)?

like image 435
Acorn Avatar asked Jan 18 '12 22:01

Acorn


People also ask

What is raw HTTP response?

The Raw HTTP action sends a HTTP request to a web server. How the response is treated depends on the method, but in general the status code and the response headers are returned in variables defined as part of the page load options.

How do I get the HTTP response header?

In order to extract a header from a response, you will have to use Http. request along with the expectStringResponse function, which includes the full response including headers. Here is an example on ellie-app.com which returns the value of content-type as an example.

What is raw HTTP header?

Raw means that the header is not URL-encoded, whereas if the word "raw" is omitted, the header is encoded. For example: $header = 'http://www.mywebsite.com?


1 Answers

Using the socket module directly:

import socket

CRLF = "\r\n"

request = [
    "GET / HTTP/1.1",
    "Host: www.example.com",
    "Connection: Close",
    "",
    "",
]

# Connect to the server
s = socket.socket()
s.connect(('www.example.com', 80))

# Send an HTTP request
s.send(CRLF.join(request))

# Get the response (in several parts, if necessary)
response = ''
buffer = s.recv(4096)
while buffer:
    response += buffer
    buffer = s.recv(4096)

# HTTP headers will be separated from the body by an empty line
header_data, _, body = response.partition(CRLF + CRLF)

print header_data
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: Keep-Alive
Content-Length: 0
like image 112
Ian Clelland Avatar answered Sep 21 '22 12:09

Ian Clelland