-
Notifications
You must be signed in to change notification settings - Fork 2
/
webserver.py
executable file
·80 lines (60 loc) · 2.15 KB
/
webserver.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import rospy
import sys
import os
import roslib
import socket
DEFAULT_PORT = 8000
os.chdir(roslib.packages.get_pkg_dir('tue_mobile_ui'))
# frist try to create a twisted server
def create_twisted_server(port, index_names):
from twisted.web import static, server
from twisted.internet import reactor
print('using TwistedServer')
root = static.File('.')
root.index_names = index_names
root.contentTypes['.woff'] = 'application/font-woff'
factory = server.Site(root)
print("Serving HTTP on 0.0.0.0 port %d ...: " % port)
reactor.listenTCP(port, factory)
def shutdown():
rospy.loginfo('KeyboardInterrupt received, closing the server...')
if reactor.running:
reactor.stop()
rospy.on_shutdown(shutdown)
reactor.run()
# if that fails, create a create a SimpleHTTPRequestHandler
def create_simple_server(port):
import SimpleHTTPServer
import SocketServer
print('using SimpleHTTPServer')
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", port), Handler)
print("Serving HTTP on 0.0.0.0 port %d ...: " % port)
def shutdown():
rospy.loginfo('KeyboardInterrupt received, closing the server...')
httpd.shutdown()
rospy.on_shutdown(shutdown)
try:
httpd.serve_forever()
# except KeyboardInterrupt:
except KeyboardInterrupt:
shutdown()
if __name__ == '__main__':
rospy.init_node('webserver', anonymous=True, disable_signals=True)
try:
port = rospy.get_param('~port', DEFAULT_PORT)
index_names = rospy.get_param('~index_names', ['index.html'])
print('using port %d from parameter server' % port)
print('using index names: %s from parameter server' % index_names)
except socket.error:
print('no parameter server running, getting port from argv')
index_names = ['index.html']
if len(sys.argv) > 1 and sys.argv[1].isdigit():
port = int(sys.argv[1])
else:
port = DEFAULT_PORT
try:
create_twisted_server(port, index_names)
except ImportError:
create_simple_server(port)