Shane
Shane

Reputation: 267

Python - Sending "Incomplete" GET requests

So I was reading about these partial GET requests that would make a server timeout the connection after a while on this request. How would send a partial GET request?..

import socket, sys
host = sys.argv[1]

request = "GET / HTTP/1.1\nHost: "+host+"\n\nUser-Agent:Mozilla 5.0\n" #How could I make this into a partial request?

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))
s.sendto(request, (host, 80))
response = s.recv(1024)

How would I do this?

Upvotes: 4

Views: 3424

Answers (2)

falsetru
falsetru

Reputation: 369454

The HTTP headers ends too early. (\n\n should come after headers, before the contents)

import socket, sys
host = sys.argv[1]

request = "GET / HTTP/1.1\nHost: "+host+"\nUser-Agent:Mozilla 5.0\n\n"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))
s.send(request)
response = s.recv(1024)

If you mean partial content retrieval, you can speicfy Range header:

"GET / HTTP/1.1\nHost: "+host+"\nUser-Agent:Mozilla 5.0\rRange: bytes=0-999\n\n"

NOTE

It should be \r\n not \n as line end, even if most (but not all) servers accept \n too.

Upvotes: 0

Steffen Ullrich
Steffen Ullrich

Reputation: 123639

I think you confuse partial and incomplete requests:

  • partial: request for some part of a resource, that is a Range request like shown in the answer of falsetru. This will not cause a timeout but instead a response with code 206 and your requested part of the resource.
  • incomplete: your request is incomplete and cannot be processed by the server, thus it will wait for the rest of the request and timeout after a while if it does not get the request. In your question you already have such an incomplete request because you did not finish you request properly (it must end with \r\n\r\n and not a single \n). Other ways are just a TCP connect without sending any data or doing a POST request with a content-length and then not sending as much data as specified in the request header.

Upvotes: 1

Related Questions