tags:

views:

47

answers:

2

I am studying Python language. I want to know about splitting HTTP request

GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1

Host: www.explainth.at
User-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11
Accept: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.explainth.at/en/misc/httpreq.shtml

I want to combine the part after GET and Host ( in bold letters) ..

GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1

Host: www.explainth.at

How it can be done?

A: 

You must split HTTP request by \r\n bytes. (newline marker on windows)

Svisstack
+1  A: 

It's not clear why you want to do this, what the context or goal is, or how this data is arriving in your program. However, Python supports a number of useful string operations on its string type. So if you have a string containing all of this text, then you may find the splitlines method useful, along with some list slicing:

s = """\ ... GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1 ... Host: www.explainth.at ... User-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11 ... Accept: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5 ... """ s.splitlines()[:2] ['GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1', 'Host: www.explainth.at']

Of course, if you're writing any kind of real HTTP server software, this isn't likely to be the right approach (there's almost zero reason to operate at such a low level, and if you need to you almost certainly want to write or re-use a real HTTP parser instead). So you may want to ask a more precise question.

Jean-Paul Calderone