Search code examples
pythonhttprequesturllib2

How to manipulate the content body of the GET request in Python


I know there are tons of ways to add headers or cookies something like this. But what I want to do is to add "\r\n" on the top of the request so as to look like the following body.

Request Body >>

\r\n <-- technically invisible.. 
GET /path/ HTTP/1.1
Host: www.website.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
Referer: https://www.google.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,ko;q=0.6
Accept-Charset: windows-949,utf-8;q=0.7,*;q=0.3

\r\n is added on the first line of the GET request as you can see.

It's like adding an empty line.

How can I do this in Python?

I've spent hours on this topic but couldn't find any useful resources.

===================== ADD ============================================

It's about hacking.

In South Korea, Government restricts some sites, but the filters preventing users from connecting to the sites can easily be evaded by just adding "\r\n" on the top of the request body.

httplib2, httplib, urllib, urllib2, etc.. etc..

Whatever library to be used, I just need to add "\r\n" to the request body.


Solution

  • You could do this by monkeypatching the httplib.HTTPConnection class; urllib, urllib2, requests etc. all use that class to handle the low-level HTTP conversation.

    The easiest is to patch the HTTPConnection._output() method to insert the extra characters before a HTTP version message:

    from httplib import HTTPConnection, _CS_REQ_STARTED
    
    orig_output = HTTPConnection._output
    
    def add_initial_newline_output(self, s):
        if (self._HTTPConnection__state == _CS_REQ_STARTED and
            s.endswith(self._http_vsn_str) and not self._buffer):
            self._buffer.append('')  # will insert extra \r\n
        orig_output(self, s)
    
    HTTPConnection._output = add_initial_newline_output
    

    This will only insert the extra starting empty line when the connection is in the correct state (request started), the line ends with the current HTTP version string, and the buffer is still empty.