-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
40 lines (27 loc) · 1.05 KB
/
serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
This script creates an HTTP server to expose the current working directory. It
is meant to be an easy way to expose a local swagger.json file so that
a swagger-ui service can pick it up from localhost.
Run it with Python3:
$ python3 serve.py 8000
The port number is optional, defaulting to 8000. Once the server is up and
running with a swagger.json file in the same directory, then the url (assuming
port 8000) to be used in swagger-ui would be:
http://localhost:8000/swagger.json
"""
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys
class CORSRequestHandler(SimpleHTTPRequestHandler):
"""
Allows a simple HTTP server to have CORS enabled by default
"""
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == "__main__":
if len(sys.argv) > 1:
# Allows the port to be passed in as an argument
port = sys.argv[-1]
else:
port = 8000
test(CORSRequestHandler, HTTPServer, port=port)