]>
Commit | Line | Data |
---|---|---|
e9155818 | 1 | #!/usr/bin/env python |
5aca52e1 JM |
2 | ''' |
3 | A super simple HTTP/HTTPS webserver for python. Automatically detect | |
f9d45665 | 4 | |
5aca52e1 JM |
5 | You can make a cert/key with openssl using: |
6 | openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem | |
7 | as taken from http://docs.python.org/dev/library/ssl.html#certificates | |
8 | ||
9 | ''' | |
10 | ||
11 | import traceback, sys | |
12 | import socket | |
13 | import ssl | |
14 | #import http.server as server # python 3.X | |
15 | import SimpleHTTPServer as server # python 2.X | |
16 | ||
17 | def do_request(connstream, from_addr): | |
18 | x = object() | |
19 | server.SimpleHTTPRequestHandler(connstream, from_addr, x) | |
e66f3f89 | 20 | connstream.close() |
5aca52e1 JM |
21 | |
22 | def serve(): | |
23 | bindsocket = socket.socket() | |
a22a3cc0 | 24 | bindsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
5aca52e1 JM |
25 | #bindsocket.bind(('localhost', PORT)) |
26 | bindsocket.bind(('', PORT)) | |
27 | bindsocket.listen(5) | |
28 | ||
29 | print("serving on port", PORT) | |
30 | ||
31 | while True: | |
32 | try: | |
33 | newsocket, from_addr = bindsocket.accept() | |
34 | peek = newsocket.recv(1024, socket.MSG_PEEK) | |
35 | if peek.startswith("\x16"): | |
36 | connstream = ssl.wrap_socket( | |
37 | newsocket, | |
38 | server_side=True, | |
39 | certfile='self.pem', | |
40 | ssl_version=ssl.PROTOCOL_TLSv1) | |
41 | else: | |
42 | connstream = newsocket | |
43 | ||
44 | do_request(connstream, from_addr) | |
45 | ||
46 | except Exception: | |
47 | traceback.print_exc() | |
f6515e3e JM |
48 | |
49 | try: | |
5aca52e1 | 50 | PORT = int(sys.argv[1]) |
f6515e3e JM |
51 | except: |
52 | print "%s port" % sys.argv[0] | |
53 | sys.exit(2) | |
54 | ||
5aca52e1 | 55 | serve() |