-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHttpServer.py
35 lines (30 loc) · 1.5 KB
/
HttpServer.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
"""
Simple HTTP server for Python3
- SSL/TLS option
- multithreaded requests and server
"""
#python version check (run by interpreter)
from sys import version_info
if version_info[0] < 3: raise Exception("Python3 required. Module http.server is needed.")
ssl = False #generate certificate:
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
#server.pem then also contains the private key, which should be protected
import http.server #could be run standalone 'python3 -m http.server 8000'
import socketserver
import os
os.chdir(os.getcwd()+'/src')
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): #handles requests in a separate thread. Solves deadlock if a script tries to import other scripts in parallel and waits for them.
pass
httpServer = ThreadedTCPServer(("",8000),http.server.SimpleHTTPRequestHandler)
if ssl:
import ssl
httpServer.socket = ssl.wrap_socket (httpServer.socket, certfile='../server.pem', server_side=True)
#httpServer.serve_forever() #blocks execution
# Start a thread with the server (that starts another thread for each request)
import threading
server_thread = threading.Thread(target=httpServer.serve_forever,daemon=True) #daemon=True #daemon threads get terminated when we( non-deamon caller) terminate. No (cleaner) httpServer.shutdown() needed.
server_thread.start()
#block program, and provide shutdown command
cmd = input("Press 'Enter' to quit.\n")
httpServer.shutdown();
print("exiting...")