131 | 131 |
|
132 | 132 |
if status_code != 304:
|
133 | 133 |
if "content-type" not in header_set:
|
134 | |
headers.append(("Content-Type", "application/octet-stream; charset=UTF-8"))
|
|
134 |
headers.append((
|
|
135 |
"Content-Type",
|
|
136 |
"application/octet-stream; charset=UTF-8"
|
|
137 |
))
|
135 | 138 |
if "content-length" not in header_set:
|
136 | 139 |
headers.append(("Content-Length", str(len(body))))
|
137 | 140 |
|
|
206 | 209 |
|
207 | 210 |
self.watcher.watch(filepath, func, delay, ignore=ignore)
|
208 | 211 |
|
209 | |
def application(self, port, host, liveport=None, debug=None, live_css=True):
|
|
212 |
def application(self, port, host, liveport=None, debug=None,
|
|
213 |
live_css=True):
|
210 | 214 |
LiveReloadHandler.watcher = self.watcher
|
211 | 215 |
LiveReloadHandler.live_css = live_css
|
212 | |
if liveport is None:
|
213 | |
liveport = port
|
214 | 216 |
if debug is None and self.app:
|
215 | 217 |
debug = True
|
216 | 218 |
|
|
223 | 225 |
# The livereload.js snippet.
|
224 | 226 |
# Uses JavaScript to dynamically inject the client's hostname.
|
225 | 227 |
# This allows for serving on 0.0.0.0.
|
226 | |
live_reload_path = ":{port}/livereload.js?port={port}".format(port=liveport)
|
227 | |
if liveport == 80 or liveport == 443:
|
228 | |
live_reload_path = "/livereload.js?port={port}".format(port=liveport)
|
229 | |
|
230 | |
live_script = escape.utf8((
|
231 | |
'<script type="text/javascript">'
|
232 | |
'document.write("<script src=''//"'
|
233 | |
' + window.location.hostname + "{path}''>'
|
234 | |
' </"+"script>");'
|
235 | |
'</script>'
|
236 | |
).format(path=live_reload_path))
|
|
228 |
live_script = (
|
|
229 |
'<script type="text/javascript">(function(){'
|
|
230 |
'var s=document.createElement("script");'
|
|
231 |
'var port=%s;'
|
|
232 |
's.src="//"+window.location.hostname+":"+port'
|
|
233 |
'+ "/livereload.js?port=" + port;'
|
|
234 |
'document.head.appendChild(s);'
|
|
235 |
'})();</script>'
|
|
236 |
)
|
|
237 |
if liveport:
|
|
238 |
live_script = escape.utf8(live_script % liveport)
|
|
239 |
else:
|
|
240 |
live_script = escape.utf8(live_script % 'window.location.port')
|
237 | 241 |
|
238 | 242 |
web_handlers = self.get_web_handlers(live_script)
|
239 | 243 |
|
240 | 244 |
class ConfiguredTransform(LiveScriptInjector):
|
241 | 245 |
script = live_script
|
242 | 246 |
|
243 | |
if liveport == port:
|
|
247 |
if not liveport:
|
244 | 248 |
handlers = live_handlers + web_handlers
|
245 | 249 |
app = web.Application(
|
246 | 250 |
handlers=handlers,
|
|
270 | 274 |
]
|
271 | 275 |
|
272 | 276 |
def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
|
273 | |
open_url=False, restart_delay=2, open_url_delay=None, live_css=True):
|
|
277 |
open_url=False, restart_delay=2, open_url_delay=None,
|
|
278 |
live_css=True):
|
274 | 279 |
"""Start serve the server with the given port.
|
275 | 280 |
|
276 | 281 |
:param port: serve on this port, default is 5500
|
|
281 | 286 |
via Tornado (and causes polling). Defaults to True when
|
282 | 287 |
``self.app`` is set, otherwise False.
|
283 | 288 |
:param open_url_delay: open webbrowser after the delay seconds
|
284 | |
:param live_css: whether to use live css or force reload on css. Defaults to True
|
|
289 |
:param live_css: whether to use live css or force reload on css.
|
|
290 |
Defaults to True
|
285 | 291 |
"""
|
286 | 292 |
host = host or '127.0.0.1'
|
287 | 293 |
if root is not None:
|
|
290 | 296 |
self._setup_logging()
|
291 | 297 |
logger.info('Serving on http://%s:%s' % (host, port))
|
292 | 298 |
|
293 | |
self.application(port, host, liveport=liveport, debug=debug, live_css=live_css)
|
|
299 |
self.application(
|
|
300 |
port, host, liveport=liveport, debug=debug, live_css=live_css)
|
294 | 301 |
|
295 | 302 |
# Async open web browser after 5 sec timeout
|
296 | 303 |
if open_url or open_url_delay:
|