-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpc.py
More file actions
81 lines (75 loc) · 2.97 KB
/
httpc.py
File metadata and controls
81 lines (75 loc) · 2.97 KB
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
import socket
import argparse
import sys
def httpc(method, URL, header, verbose, inline, file, o):
if URL.split('/')[0] == 'http:' or URL.split('/')[0] == 'https:':
host = URL.split('/')[2]
else:
host = URL.split('/')[0]
conn = socket.create_connection((host, 8080))
try:
path = ''
if len(URL.split('/')) > 1:
count = 0
for i in URL.split('/'):
if count >= len(URL.split('/')):
break
if URL.split('/')[count] == 'https:' or URL.split('/')[count] == 'http:':
count += 3
continue
if count != 0:
path += '/' + URL.split('/')[count]
count += 1
if method == 'get':
line = 'GET ' + path + ' HTTP/1.0\nHost: ' + host + '\n'
if header:
for i in header:
line += i + '\n'
line += '\n'
elif method == 'post':
line = 'POST ' + path + ' HTTP/1.0\nHost: ' + host + '\n'
if header:
for i in header:
line += i + '\n'
if inline and file:
print("Error: Can't use both -f and --d")
sys.exit(1)
if inline:
#add the content length
line+= 'Content-length:' + str(len(inline)) + '\n'
line += '\n'
line += inline + '\n'
if file:
with open(file) as f:
fileContents = f.read()
line+= 'Content-length:' + str(len(fileContents)) + '\n'
line += '\n'
line += fileContents + '\n'
line += '\n'
request = line.encode("utf-8")
conn.sendall(request)
# MSG_WAITALL waits for full request or error
response = conn.recv(1000)
if o is not None:
with open(o, "a+") as f:
if verbose:
f.write(response.decode("utf-8"))
else:
f.write(response.decode("utf-8").split("\n\n")[1])
else:
if verbose:
sys.stdout.write(response.decode("utf-8"))
else:
sys.stdout.write(response.decode("utf-8").split("\n\n")[1])
finally:
conn.close()
parser = argparse.ArgumentParser()
parser.add_argument("method", nargs="?", help="get or post argument")
parser.add_argument("-v", dest="verbose", action="store_true", help="verbose")
parser.add_argument("-u", action="append", dest="header", help="header")
parser.add_argument("--d", nargs="?", dest="inline", help="inline-datae")
parser.add_argument("-f", nargs="?", dest="file", help="verbose")
parser.add_argument("URL", help="server host")
parser.add_argument("-o", nargs="?", dest="o", help="write response in file")
args = parser.parse_args()
httpc(args.method, args.URL, args.header, args.verbose, args.inline, args.file, args.o)