Mercurial > hg > RequestDumpster
annotate requestdumpster/dumpster.py @ 7:83c51f45b82d
optparse -> argparse and the like
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Wed, 16 Dec 2015 10:31:00 -0800 |
| parents | aa19f80caa63 |
| children | eb260393caef |
| 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 |
| 1 | 12 import wsgiref |
| 13 | |
| 5 | 14 # module globals |
| 15 __all__ = ['RequestDumpster'] | |
| 16 | |
| 1 | 17 class RequestDumpster(object): |
| 5 | 18 """WSGI interface to dump HTTP requests""" |
| 1 | 19 |
| 3 | 20 def __init__(self, directory=None): |
| 21 if directory is not None and not os.path.isdir(directory): | |
| 22 raise Exception("Not a directory") | |
| 23 self.directory = directory | |
| 24 | |
| 1 | 25 def __call__(self, environ, start_response): |
| 26 """WSGI""" | |
| 27 | |
| 5 | 28 |
| 1 | 29 def main(args=sys.argv[1:]): |
| 30 """CLI""" | |
| 31 | |
| 32 # parse command line arguments | |
|
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
33 parser = argparse.ArgumentParser(description=__doc__) |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
34 parser.add_argument('-p', '--port', dest='port', |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
35 type=int, default=9555, |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
36 help="port to serve on") |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
37 parser.add_argument('-d', '--directory', dest='directory', |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
38 help="directory to output requests to") |
| 1 | 39 options = parser.parse_args() |
| 40 | |
| 5 | 41 # instantiate WSGI app |
| 6 | 42 app = RequestDumpster(directory=options.directory) |
| 43 | |
| 1 | 44 # construct url |
| 45 url = 'http://localhost:{port}/'.format(port=options.port) | |
| 46 | |
| 6 | 47 # serve some web |
| 48 server = simple_server.make_server(host=host, port=int(port), app=app) | |
|
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
49 print url |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
50 try: |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
51 server.serve_forever() |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
52 except KeyboardInterrupt: |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
53 pass |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
54 |
| 6 | 55 |
| 1 | 56 if __name__ == '__main__': |
| 57 main() |
