-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpc.py
More file actions
executable file
·76 lines (67 loc) · 3.56 KB
/
httpc.py
File metadata and controls
executable file
·76 lines (67 loc) · 3.56 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
#!/usr/bin/python3
import argparse
import libhttpc
import re
def commandDescriptions():
return """
The commands are:
get executes a HTTP GET request and prints the response.
post executes a HTTP POST request and prints the response.
help print this help screen.
"""
parser = argparse.ArgumentParser(prog='httpc', usage='httpc command [arguments]', description='httpc is a curl like application but supports HTTP protocol only.', formatter_class=argparse.RawTextHelpFormatter, epilog=commandDescriptions())
#parser.add_argument('command', default='help', nargs='?', choices=['help', 'get', 'post'], help=argparse.SUPPRESS)
subparsers = parser.add_subparsers(dest='command', help=argparse.SUPPRESS)
# create the parser for the get command
parser_get = subparsers.add_parser('get', prog='httpc get', add_help=False)
parser_get.add_argument('-v','--verbose', action='store_true', help='Prints the detail of the response such as protocol, status, and headers.')
parser_get.add_argument('-p', '--port', type=int, default=80)
#append option to allow the flage multiple times
#metavar changes the description of the argument for the flag
parser_get.add_argument('-h', action='append', metavar='key:value',help='Associates headers to HTTP Request with the format \'key:value\'.')
parser_get.add_argument('URL')
# create the parser for the post command
parser_post = subparsers.add_parser('post', prog='httpc post', add_help=False)
parser_post.add_argument('-v','--verbose', action='store_true', help='Prints the detail of the response such as protocol, status, and headers.')
parser_post.add_argument('-h', action='append', metavar='key:value',help='Associates headers to HTTP Request with the format \'key:value\'.')
parser_post.add_argument('URL')
parser_post.add_argument('-p', '--port', type=int, default=80)
#Mutually exclusive group for post -d and -f options
post_group = parser_post.add_mutually_exclusive_group()
post_group.add_argument('-d',type=str, default = '', metavar='inline-data', help='Associates an inline data to the body HTTP POST request.')
post_group.add_argument('-f', metavar='file', help='Associates the content of a file to the body HTTP POST request.')
# create the parser for the help command
parser_help = subparsers.add_parser('help', help=argparse.SUPPRESS)
parser_help.add_argument('method', choices=['get','post',''], default='', nargs='?')
args = parser.parse_args()
#print (args)
if args.command == 'help':
if args.method == 'get':
parser_get.print_help()
elif args.method == 'post':
parser_post.print_help()
else:
parser.print_help()
if hasattr(args, 'URL'):
#Match http(s) 0 or 1 times then match www 0 or 1 times
#then match any char except : or / one or more times
#then match one or 0 / followed by any char 0 or more times (match path group once or 0 times at end of string)
regexp = '(?:https?://)?(?P<www>w{3}\.)?(?P<host>[^:/ ]+).?/?(?P<path>.*)?$'
host = re.search(regexp, args.URL).group('host')
path = re.search(regexp, args.URL).group('path')
if args.command == 'get':
#print (args.h[0].__class__)
libhttpc.makeRequest(host, args.port,'GET /'+path, args.verbose, args.h)
#print ("GET used")
#parser_get.print_help()
elif args.command == 'post':
try:
with open(args.f, 'r') as myfile:
data=myfile.read()
except:
data = args.d
libhttpc.makeRequest(host, args.port ,'POST /'+path, args.verbose, args.h, data)
#parser_post.print_help()
else:
#print ("Exception caught")
parser.print_help()