瀏覽代碼

Thread shutdown is working now

master
Pete Shadbolt 8 年之前
父節點
當前提交
9e597d9c13
共有 2 個檔案被更改,包括 22 行新增11 行删除
  1. +3
    -4
      server/main.js
  2. +19
    -7
      server/server.py

+ 3
- 4
server/main.js 查看文件

@@ -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 查看文件

@@ -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…
取消
儲存