Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / public / vendor / noVNC / utils / web.py @ 5fcb9de8

History | View | Annotate | Download (1.46 KB)

1
#!/usr/bin/env python
2
'''
3
A super simple HTTP/HTTPS webserver for python. Automatically detect
4

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)
20
    connstream.close()
21

    
22
def serve():
23
    bindsocket = socket.socket()
24
    bindsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
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()
48

    
49
try:
50
    PORT = int(sys.argv[1])
51
except:
52
    print "%s port" % sys.argv[0]
53
    sys.exit(2)
54

    
55
serve()