Python - sending "incomplete" GET requests
So, I read about these partial GET requests that would cause the server to timeout the connection after a while on this request. How do I 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 should I do it?
+3
source to share
2 answers
I think you are confusing incomplete and incomplete queries:
- partial: a request for some part of the resource, i.e. a range request as shown in falsetru's answer. This will not cause a timeout, but instead a response with code 206 and the requested portion of the resource.
- incomplete: Your request is incomplete and cannot be processed by the server, so it will wait for the rest of the request and timeout after a while if it doesn't receive the request. In your question, you already have such an incomplete request because you did not complete the request correctly (it should end with
\r\n\r\n
, not one\n
). Other ways are just a TCP connection without sending any data or making a POST request with the length of the content and then not sending as much of the data specified in the request header as possible.
+1
source to share
HTTP headers end up too early. ( \n\n
must appear after headers, before content)
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 search, 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 \r\n
not be \n
like the end of the line, even if most (but not all) servers accept it \n
too.
0
source to share