annotate requestdumpster/dumpster.py @ 6:aa19f80caa63

more stubbing
author Jeff Hammel <k0scist@gmail.com>
date Wed, 01 Jul 2015 08:42:12 -0700
parents afa7a874d28e
children 83c51f45b82d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 dump HTTP requests
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
2
8ddcab56f340 forgot an import
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
7 # imports
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import optparse
3
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
9 import os
2
8ddcab56f340 forgot an import
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
10 import sys
3
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
11 import time
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import wsgiref
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
14 # module globals
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
15 __all__ = ['RequestDumpster']
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
16
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 class RequestDumpster(object):
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
18 """WSGI interface to dump HTTP requests"""
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
3
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
20 def __init__(self, directory=None):
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
21 if directory is not None and not os.path.isdir(directory):
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
22 raise Exception("Not a directory")
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
23 self.directory = directory
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
24
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 def __call__(self, environ, start_response):
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 """WSGI"""
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
28
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 def main(args=sys.argv[1:]):
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 """CLI"""
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 # parse command line arguments
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 parser = optparse.OptionParser(description=__doc__)
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 parser.add_option('-p', '--port', dest='port',
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 type='int', default=9555,
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 help="port to serve on")
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
37 parser.add_option('-d', '--directory', dest='directory',
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
38 help="directory to output requests to")
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 options = parser.parse_args()
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
41 # instantiate WSGI app
6
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
42 app = RequestDumpster(directory=options.directory)
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
43
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 # construct url
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 url = 'http://localhost:{port}/'.format(port=options.port)
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46
6
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
47 # serve some web
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
48 server = simple_server.make_server(host=host, port=int(port), app=app)
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
49 server.serve_forever()
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
50
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 if __name__ == '__main__':
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 main()