1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Twisted based HTTP server
19 """
20
21 __maintainer__ = "Philippe Normand <philippe@fluendo.com>"
22
23 from elisa.core import common
24 from elisa.base_components import service_provider
25 from elisa.core.bus.bus_message import HttpResource
26 from elisa.core.utils import network
27
28 from twisted.internet import reactor
29 from twisted.internet.error import CannotListenError
30 from twisted.web import resource, server
31
33 addSlash = True
34
36 return """
37 <html>
38 <head>
39 <title>Elisa HTTP Server</title>
40 </head>
41 <body>
42 <h1>Empty page</h1>
43 </body>
44 </html>
45 """
46
52
54 addSlash = True
58
60 return """
61 <html>
62 <head>
63 <title>Elisa HTTP Server</title>
64 </head>
65 <body>
66 <h1>%s Resource</h1>
67 </body>
68 </html>
69 """ % self._name
70
73
74 -class HttpServer(service_provider.ServiceProvider):
75 """
76 DOCME
77 """
78
79 default_config = {'port': '8000', 'interface': '' }
80 config_doc = {'port': 'TCP port the server should listen on',
81 'interface': 'the hostname to bind to, defaults to "" (all)'}
82
89
91 port = int(self.config.get('port'))
92 self._interface = self.config.get('interface','')
93 site = server.Site(self._root)
94
95 iface = self._interface or 'all'
96 self.info("Listening on %s:%s", iface, port)
97
98 try:
99 self._twisted_port = reactor.listenTCP(port, site,
100 interface=self._interface)
101 except CannotListenError, error:
102 self.warning(error)
103 else:
104 reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
105
107 self._twisted_port.stopListening()
108
110 if not self._ip_port:
111 thost = self._twisted_port.getHost()
112 ip = thost.host
113 port = thost.port
114
115 if ip == '0.0.0.0':
116 ip = network.get_host_address()
117 elif self._interface:
118 ip = network.get_ip_address(self._interface)
119 self._ip_port = (ip, port)
120
121 return self._ip_port
122
145