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/

2
  • 2
    what is your goal? if'youre just trying to make a request, you should better use urllib2/urllib or requests... Commented Apr 21, 2013 at 21:43
  • 1
    Did you know Python comes with a urlparse module? Commented Apr 21, 2013 at 21:43

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Note that string concatenation is more cumbersome than string formatting. Otherwise, good points.
I am actually trying to display an image on screen. Why do I need save a file?
You don't have to save the file to disk; the named libraries can still help you achieve what you want.
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.

4 Comments

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", so it's better to set the breaks manually.
@DiegoNunes: Thanks for the link, I actually had no idea! I'll modify the answer.
Its not storing the HTTP request in a variable tho
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.