Mercurial > hg > RequestDumpster
annotate requestdumpster/dumpster.py @ 8:eb260393caef
this is about as far as I want to get without webob
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Wed, 16 Dec 2015 10:51:32 -0800 |
| parents | 83c51f45b82d |
| children | db2ab581cedb |
| 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 |
| 1 | 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 | |
|
8
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
28 body = """{REQUEST_METHOD} {PATH_INFO} {SERVER_PROTOCOL}""".format(**environ) |
|
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
29 |
|
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
30 start_response('200 OK', [('Content-Type', 'text/plain')]) |
|
eb260393caef
this is about as far as I want to get without webob
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
31 return [body] |
| 5 | 32 |
| 1 | 33 def main(args=sys.argv[1:]): |
| 34 """CLI""" | |
| 35 | |
| 36 # parse command line arguments | |
|
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
37 parser = argparse.ArgumentParser(description=__doc__) |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
38 parser.add_argument('-p', '--port', dest='port', |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
39 type=int, default=9555, |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
40 help="port to serve on") |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
41 parser.add_argument('-d', '--directory', dest='directory', |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
42 help="directory to output requests to") |
| 1 | 43 options = parser.parse_args() |
| 44 | |
| 5 | 45 # instantiate WSGI app |
| 6 | 46 app = RequestDumpster(directory=options.directory) |
| 47 | |
| 1 | 48 # construct url |
| 49 url = 'http://localhost:{port}/'.format(port=options.port) | |
| 50 | |
| 6 | 51 # 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
|
52 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
|
53 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
|
54 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
|
55 app=app) |
|
7
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
56 print url |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
57 try: |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
58 server.serve_forever() |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
59 except KeyboardInterrupt: |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
60 pass |
|
83c51f45b82d
optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
61 |
| 6 | 62 |
| 1 | 63 if __name__ == '__main__': |
| 64 main() |
