Mercurial > hg > RequestDumpster
annotate requestdumpster/dumpster.py @ 15:d2585c78e70f
make -d actually work
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 12 Aug 2016 13:44:49 -0700 |
parents | 194a66d765ae |
children | 9ef205ebb047 |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 dump HTTP requests | |
5 """ | |
6 | |
2 | 7 # imports |
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
8 import argparse |
3 | 9 import os |
2 | 10 import sys |
3 | 11 import time |
8
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
12 from wsgiref import simple_server |
10 | 13 from webob import Request, Response |
1 | 14 |
5 | 15 # module globals |
16 __all__ = ['RequestDumpster'] | |
17 | |
1 | 18 class RequestDumpster(object): |
5 | 19 """WSGI interface to dump HTTP requests""" |
1 | 20 |
3 | 21 def __init__(self, directory=None): |
22 if directory is not None and not os.path.isdir(directory): | |
23 raise Exception("Not a directory") | |
24 self.directory = directory | |
25 | |
1 | 26 def __call__(self, environ, start_response): |
27 """WSGI""" | |
28 | |
10 | 29 request = Request(environ) |
30 lines = ["{REQUEST_METHOD} {PATH_INFO} {SERVER_PROTOCOL}".format(PATH_INFO=request.path_qs, | |
31 REQUEST_METHOD=request.method, | |
32 SERVER_PROTOCOL=request.environ['SERVER_PROTOCOL'])] | |
33 lines.extend(['{0}: {1}'.format(*header) | |
34 for header in request.headers.items()]) | |
35 lines.append('') | |
36 lines.append(request.body) | |
12 | 37 body = '\r\n'.join(lines) |
8
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
38 |
15 | 39 if self.directory: |
40 filename = '{0}'.format(time.time()) | |
41 with open(os.path.join(self.directory, filename), 'w') as f: | |
42 f.write(body) | |
43 | |
10 | 44 response = Response(content_type='text/plain', |
45 body=body) | |
46 return response(environ, start_response) | |
5 | 47 |
1 | 48 def main(args=sys.argv[1:]): |
49 """CLI""" | |
50 | |
51 # parse command line arguments | |
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
52 parser = argparse.ArgumentParser(description=__doc__) |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
53 parser.add_argument('-p', '--port', dest='port', |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
54 type=int, default=9555, |
14 | 55 help="port to serve on [DEFAULT: %(default)s]") |
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
56 parser.add_argument('-d', '--directory', dest='directory', |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
57 help="directory to output requests to") |
1 | 58 options = parser.parse_args() |
59 | |
5 | 60 # instantiate WSGI app |
6 | 61 app = RequestDumpster(directory=options.directory) |
62 | |
1 | 63 # construct url |
64 url = 'http://localhost:{port}/'.format(port=options.port) | |
65 | |
6 | 66 # serve some web |
8
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
67 host = '127.0.0.1' |
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
68 server = simple_server.make_server(host=host, |
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
69 port=options.port, |
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
70 app=app) |
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
71 print url |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
72 try: |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
73 server.serve_forever() |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
74 except KeyboardInterrupt: |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
75 pass |
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
76 |
6 | 77 |
1 | 78 if __name__ == '__main__': |
79 main() |