change to tornado
http.server seems to be slow to handle massive requests.
This commit is contained in:
parent
5e08d676f6
commit
4b10bc9b98
@ -1,7 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from http.server import SimpleHTTPRequestHandler, HTTPServer
|
import tornado.ioloop
|
||||||
from urllib.parse import parse_qs
|
import tornado.web
|
||||||
|
from tornado.web import RequestHandler, StaticFileHandler
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
@ -13,32 +14,31 @@ PORT = 5678
|
|||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
|
|
||||||
class MapHandler(SimpleHTTPRequestHandler):
|
class IndexHandler(RequestHandler):
|
||||||
def __init__(self, *args, **kwargs):
|
def get(self):
|
||||||
directory = os.path.dirname(__file__)
|
self.redirect('/static/baidumap.html')
|
||||||
super().__init__(*args, directory=directory, **kwargs)
|
|
||||||
|
|
||||||
def do_GET(self):
|
|
||||||
if self.path == '/':
|
|
||||||
self.send_response(301)
|
|
||||||
self.send_header('Location', '/static/baidumap.html')
|
|
||||||
self.end_headers()
|
|
||||||
else:
|
|
||||||
super(MapHandler, self).do_GET()
|
|
||||||
|
|
||||||
def do_POST(self):
|
class PostHandler(RequestHandler):
|
||||||
if self.path == '/post':
|
def post(self):
|
||||||
urlencoded = self.rfile.read(int(self.headers['Content-Length']))
|
self.set_header('Content-Type', 'text/plain')
|
||||||
parsed = parse_qs(urlencoded)
|
pos = [
|
||||||
pos = [float(parsed.get(k)[0]) for k in [b'lon', b'lat', b'hgt']]
|
float(self.get_body_argument(k)) for k in ['lon', 'lat', 'hgt']
|
||||||
data = struct.pack('ddd', *pos)
|
]
|
||||||
sock.sendto(data, (HOST, PORT))
|
data = struct.pack('ddd', *pos)
|
||||||
print(*pos)
|
sock.sendto(data, (HOST, PORT))
|
||||||
self.send_response(200)
|
print(*pos)
|
||||||
self.send_header('Content-Type', 'text/plain')
|
self.write('OK')
|
||||||
else:
|
|
||||||
self.send_response(404)
|
|
||||||
self.end_headers()
|
def make_app():
|
||||||
|
return tornado.web.Application([
|
||||||
|
(r'/', IndexHandler),
|
||||||
|
(r'/post', PostHandler),
|
||||||
|
(r'/static/(.*)', StaticFileHandler, {
|
||||||
|
'path': os.path.dirname(__file__)
|
||||||
|
})
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -56,6 +56,7 @@ if __name__ == "__main__":
|
|||||||
help='specify host map server to bind [default: 0.0.0.0]')
|
help='specify host map server to bind [default: 0.0.0.0]')
|
||||||
args = argp.parse_args()
|
args = argp.parse_args()
|
||||||
|
|
||||||
httpd = HTTPServer((args.host, args.port), MapHandler)
|
app = make_app()
|
||||||
print('Serving at http://{}:{}'.format(args.host, args.port))
|
print('Serving at http://{}:{}'.format(args.host, args.port))
|
||||||
httpd.serve_forever()
|
app.listen(args.port, args.host)
|
||||||
|
tornado.ioloop.IOLoop.current().start()
|
||||||
|
Loading…
Reference in New Issue
Block a user