-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_server.py
65 lines (55 loc) · 2.07 KB
/
simple_server.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Simple socket server using threads
'''
from http.server import BaseHTTPRequestHandler
from threading import Thread
from http.server import HTTPServer
PORT_NUMBER = 8888
class HttpServer():
http_server = True
def __init__(self):
super(HttpServer, self).__init__()
if not self.http_server:
return
self.simple_server = HTTPServer(('', 8888), myHandler)
self.simple_server.message = 'Let\'s go'
self.server_thread = Thread(target = self.simple_server.serve_forever)
self.server_thread.start()
def stop(self):
print('stopping server')
self.simple_server.shutdown()
self.simple_server.socket.close()
print('server stopped')
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header(b'Content-type','text/html')
self.end_headers()
# Send the html message
if (self.path != '/'):
self.wfile.write(self.server.message.encode())
else:
self.wfile.write(b"""<html>
<body>
Inside the mind of the LingLover
<div id="a"></div>
<script>
function a() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("a").innerHTML = "<pre>" + this.responseText + "</pre>";
setTimeout(a, 100);
}
};
xhttp.open("GET", "http://localhost:8888/something", true);
xhttp.send();
}
a();
</script>
</body>
</html>""")
return