Browse Source

Thread shutdown is working now

master
Pete Shadbolt 8 years ago
parent
commit
9e597d9c13
2 changed files with 22 additions and 11 deletions
  1. +3
    -4
      server/main.js
  2. +19
    -7
      server/server.py

+ 3
- 4
server/main.js View File

@@ -1,12 +1,11 @@
function poll() {
console.log("polling");
var xhr = new XMLHttpRequest();
xhr.load=function() {
console.log(JSON.parse(xhr.responseText));

xhr.onload=function() {
console.log(xhr.responseText);
};

xhr.onerror = function(e){
console.log(e);
};

xhr.open("GET", "/state", true);


+ 19
- 7
server/server.py View File

@@ -2,6 +2,10 @@ import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler
from SimpleHTTPServer import SimpleHTTPRequestHandler
import SocketServer
import sys
import json
import threading
import time

class MyHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
@@ -9,19 +13,27 @@ class MyHandler(SimpleHTTPRequestHandler):

def get_state(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write("here is the state")
self.wfile.write(json.dumps({"state":"here is the state"}))
def do_GET(self, *args, **kwargs):
parsed_path = urlparse.urlparse(self.path)
if parsed_path == "/state":
print parsed_path.path
if parsed_path.path == "/state":
return self.get_state()
else:
return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs)

if __name__ == '__main__':
port = int(sys.argv[1]) if len(sys.argv)==2 else 8000
server = SocketServer.TCPServer(("127.0.0.1", port), MyHandler)


httpd = SocketServer.TCPServer(("", 8000), MyHandler)

print "Go to 127.0.0.0:8000"
httpd.serve_forever()
print "Go to 127.0.0.0:{}".format(port)
thread = threading.Thread(None, server.serve_forever)
thread.daemon = True
thread.start()
time.sleep(5)
print "Shutting down ... "
server.shutdown()
#thread.join()

Loading…
Cancel
Save