annotate requestdumpster/dumpster.py @ 16:9ef205ebb047 default tip

CORS
author Jeff Hammel <k0scist@gmail.com>
date Mon, 12 Dec 2016 15:26:40 -0800
parents d2585c78e70f
children
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
7
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
8 import argparse
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
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
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
13 from webob import Request, Response
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
15 # module globals
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
16 __all__ = ['RequestDumpster']
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
17
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 class RequestDumpster(object):
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
19 """WSGI interface to dump HTTP requests"""
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
3
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
21 def __init__(self, directory=None):
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
22 if directory is not None and not os.path.isdir(directory):
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
23 raise Exception("Not a directory")
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
24 self.directory = directory
f2f983b76090 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
25
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def __call__(self, environ, start_response):
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """WSGI"""
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
10
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
29 request = Request(environ)
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
30 lines = ["{REQUEST_METHOD} {PATH_INFO} {SERVER_PROTOCOL}".format(PATH_INFO=request.path_qs,
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
31 REQUEST_METHOD=request.method,
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
32 SERVER_PROTOCOL=request.environ['SERVER_PROTOCOL'])]
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
33 lines.extend(['{0}: {1}'.format(*header)
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
34 for header in request.headers.items()])
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
35 lines.append('')
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
36 lines.append(request.body)
12
d329dfdf6099 dont think i need this
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
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
d2585c78e70f make -d actually work
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
39 if self.directory:
d2585c78e70f make -d actually work
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
40 filename = '{0}'.format(time.time())
d2585c78e70f make -d actually work
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
41 with open(os.path.join(self.directory, filename), 'w') as f:
d2585c78e70f make -d actually work
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
42 f.write(body)
d2585c78e70f make -d actually work
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
43
16
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
44 # CORS
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
45 headers = [('Access-Control-Allow-Origin', '*')]
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
46 headers.append(('Access-Control-Allow-Headers',
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
47 ', '.join(request.headers.keys())))
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
48 headers.append(('Access-Control-Allow-Methods',
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
49 'GET, POST, DELETE, PUT, PATCH, OPTIONS'))
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
50
10
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
51 response = Response(content_type='text/plain',
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
52 body=body)
16
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
53 for key, value in headers:
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
54 response.headers.add(key, value)
10
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
55 return response(environ, start_response)
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
56
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 def main(args=sys.argv[1:]):
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 """CLI"""
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 # parse command line arguments
7
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
61 parser = argparse.ArgumentParser(description=__doc__)
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
62 parser.add_argument('-p', '--port', dest='port',
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
63 type=int, default=9555,
14
194a66d765ae print default port
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
64 help="port to serve on [DEFAULT: %(default)s]")
7
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
65 parser.add_argument('-d', '--directory', dest='directory',
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
66 help="directory to output requests to")
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 options = parser.parse_args()
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68
5
afa7a874d28e dumpster;
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
69 # instantiate WSGI app
6
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
70 app = RequestDumpster(directory=options.directory)
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
71
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 # construct url
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 url = 'http://localhost:{port}/'.format(port=options.port)
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74
6
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
75 # 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
76 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
77 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
78 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
79 app=app)
7
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
80 print url
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
81 try:
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
82 server.serve_forever()
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
83 except KeyboardInterrupt:
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
84 pass
83c51f45b82d optparse -> argparse and the like
Jeff Hammel <k0scist@gmail.com>
parents: 6
diff changeset
85
6
aa19f80caa63 more stubbing
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
86
1
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 if __name__ == '__main__':
4ab07b01ee63 begin to stub dumpster
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 main()