3

I am trying to build the HTTP request string in the variable "full_req". Here is how a HTTP request looks:

GET /images/logo/ HTTP/1.1
Host: www.google.com
Connection: close

I already wrote python code so if I enter a url, "http://www.google.com/images/logo/googlelogo.png", it will split the url into 3 parts. Below are the variables that I used:

  • host (contains www.google.com)
  • path (contains /images/logo/)
  • file (contains googlelogo.png)
  • port (Its showing None but I think its suppose to say 80)

How do I assign HTTP request string to full_req variable so if I print this variable, it contains all three lines?

print(full_req)

tried string concatenation but it didn't work. I used this resource: http://www.skymind.com/~ocrow/python_string/

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Damon
  • 66
  • 1
  • 6
  • 2
    what is your goal? if'youre just trying to make a request, you should better use [urllib2](http://docs.python.org/2/library/urllib2)/[urllib](http://docs.python.org/3/library/urllib.request.html) or [requests](http://docs.python-requests.org)... – mata Apr 21 '13 at 21:43
  • 1
    Did you know Python comes with a [`urlparse` module](http://docs.python.org/2/library/urlparse.html)? – Martijn Pieters Apr 21 '13 at 21:43

3 Answers3

4

You could use string formatting:

request = 'GET {path} HTTP 1.1\r\nHost: {host}\r\nConnection: Close\r\n\r\n'.format(path=path, host=host)

This uses the modern str.format() method, but you could use classic string operations as well.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

. . String format or simple concatenation should do it.

. . There are many options and it depends on versions. In Python2 it is common to use % operator syntax, but in Python3 you mainly use the .format method on string objects.

. . You can still use the simplest form of concatenation with the + operator, with something like this:

full_req = 'GET ' + path + ' HTTP/1.1\r\n' \
           'HOST: ' + host + '\r\n' + \
           'Connection: close'

. . Note that it won't request the file (as per your example you are requesting the parent folder, not the file itself) and if you plan to download the file rather than just experiment with HTTP protocol, there are built-in modules to do that (check urllib.retrieve).

. . The port number (usually 80 for HTTP) would be used when opening the connection (in a socket), but your example doesn't seem to do that part of the request as well.

. Amplexos.

diego nunes
  • 2,750
  • 1
  • 14
  • 16
2

Try:

def request(host, path, file):
    return '''GET %s%s HTTP/1.1\r
Host: %s\r
Connection: close''' % (path, file, host)

As far as I am aware, the port isn't included in the actual HTTP request. The key here is the use of the % string formatting operator.

Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
  • It is nice the clear out that your file can use the "\n"-only (linux style) line-breaks, but [RFC2616 defines the "right" line break as "\r\n"](http://stackoverflow.com/questions/5757290/http-header-line-break-style), so it's better to set the breaks manually. – diego nunes Apr 21 '13 at 21:54
  • @DiegoNunes: Thanks for the link, I actually had no idea! I'll modify the answer. – Lucas Jones Apr 21 '13 at 21:55
  • Its not storing the HTTP request in a variable tho – Damon Apr 21 '13 at 23:15
  • To store the result in a variable, use `req = request(host, path, file)` after you've defined the function. Alternatively, just write `req = '''GET %s%s HTTP/1.1\r`, etc. – Lucas Jones Apr 22 '13 at 15:25