]> git.proxmox.com Git - mirror_novnc.git/blame - utils/web.py
Merge pull request #242 from Medical-Insight/use-wss-for-binary-detect
[mirror_novnc.git] / utils / web.py
CommitLineData
e9155818 1#!/usr/bin/env python
5aca52e1
JM
2'''
3A super simple HTTP/HTTPS webserver for python. Automatically detect
f9d45665 4
5aca52e1
JM
5You can make a cert/key with openssl using:
6openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
7as taken from http://docs.python.org/dev/library/ssl.html#certificates
8
9'''
10
11import traceback, sys
12import socket
13import ssl
14#import http.server as server # python 3.X
15import SimpleHTTPServer as server # python 2.X
16
17def do_request(connstream, from_addr):
18 x = object()
19 server.SimpleHTTPRequestHandler(connstream, from_addr, x)
e66f3f89 20 connstream.close()
5aca52e1
JM
21
22def 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
49try:
5aca52e1 50 PORT = int(sys.argv[1])
f6515e3e
JM
51except:
52 print "%s port" % sys.argv[0]
53 sys.exit(2)
54
5aca52e1 55serve()