-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhttp_test.py
executable file
·86 lines (75 loc) · 2.38 KB
/
http_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env python2
import argparse
import os
import socket
from urlparse import urlparse
def get_request(num, host):
if num == 0:
# Default request
return "GET / HTTP/1.1\r\nHost: %s\r\n\r\n" % host
elif num == 1:
# HTTP 1.0 without HOST HEADER
return "GET / HTTP/1.0\r\n\r\n"
elif num == 2:
return "GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % host
elif num == 3:
return "OPTIONS / HTTP/1.1\r\nHost: %s\r\n\r\n" % host
elif num == 4:
return "TRACE / HTTP/1.1\r\nHost: %s\r\n\r\n" % host
elif num == 5:
return "FOOBAR / HTTP/1.1\r\nHost: %s\r\n\r\n" % host
else:
return ""
def send_request(num, host, args):
req = get_request(num, host)
if req == "":
print "Bad number"
exit(1)
else:
print "========================================"
print "Request #%i to %s\n" % (num, host)
print req
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))
s.sendall(req)
#FIXME : does not get all
data = s.recv(100000)
s.close()
sep = data.find("\r\n\r\n")
headers = data[:sep]
content = data[sep+4:]
if args.content:
print data
else:
if len(content) < 700:
print data
else:
print headers
print "\nContent avoided (length %i)" % len(content)
print ""
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Make some weird raw HTTP requests and print outputs')
parser.add_argument('-a', '--all', help='Send all requests', action='store_true')
parser.add_argument('-n', '--num', help='Select only one request to send [1-2]', type=int)
parser.add_argument('-c', '--content', help='Always show the content', action="store_true")
parser.add_argument('host', metavar='HOST', help='Host targeted')
args = parser.parse_args()
# valid the host
hosturl = urlparse(args.host)
if hosturl.netloc == '':
# XXX: remove path after hostname
host = hosturl.path
else:
host = hosturl.netloc
DEFAULT = [0, 1]
ALL = [0, 1, 2]
if args.num != None:
# Only one request
send_request(args.num, host, args)
else:
if args.all:
requests = ALL
else:
requests = DEFAULT
for i in requests:
send_request(i, host, args)