99import logging
1010import mimetypes
1111import os
12+ import platform
1213import requests
1314import socket
1415import sys
@@ -53,6 +54,7 @@ class Config:
5354
5455DIFF = None
5556PORT = None
57+ HOSTNAME = "localhost"
5658
5759if app .config ['TESTING' ] or app .config ['DEBUG' ]:
5860 handler = logging .StreamHandler ()
@@ -216,7 +218,7 @@ def kill():
216218 last_ms = LAST_REQUEST_MS
217219 def shutdown ():
218220 if LAST_REQUEST_MS <= last_ms : # subsequent requests abort shutdown
219- requests .post ('http://localhost :%d/seriouslykill' % PORT )
221+ requests .post ('http://%s :%d/seriouslykill' % ( HOSTNAME , PORT ) )
220222 else :
221223 pass
222224
@@ -227,11 +229,12 @@ def shutdown():
227229
228230def open_browser ():
229231 global PORT
232+ global HOSTNAME
230233 if not 'NO_OPEN_BROWSER' in app .config :
231234 if is_hot_reload ():
232235 log .debug ('Skipping browser open on reload' )
233236 else :
234- webbrowser .open_new_tab ('http://localhost :%s' % PORT )
237+ webbrowser .open_new_tab ('http://%s :%s' % ( HOSTNAME , PORT ) )
235238
236239
237240def usage_and_die ():
@@ -267,7 +270,7 @@ def is_webdiff_from_head():
267270
268271
269272def run ():
270- global DIFF , PORT
273+ global DIFF , PORT , HOSTNAME
271274 try :
272275 parsed_args = argparser .parse (sys .argv [1 :], VERSION )
273276 except argparser .UsageError as e :
@@ -281,11 +284,19 @@ def run():
281284
282285 PORT = pick_a_port (parsed_args )
283286
284- sys .stderr .write ('''Serving diffs on http://localhost:%s
287+ if app .config .get ('USE_HOSTNAME' ):
288+ _hostname = platform .node ()
289+ # platform.node will return empty string if it can't find the hostname
290+ if not _hostname :
291+ sys .stderr .write ('Warning: hostname could not be determined' )
292+ else :
293+ HOSTNAME = _hostname
294+
295+ sys .stderr .write ('''Serving diffs on http://%s:%s
285296Close the browser tab or hit Ctrl-C when you're done.
286- ''' % PORT )
297+ ''' % ( HOSTNAME , PORT ) )
287298 Timer (0.1 , open_browser ).start ()
288- app .run (host = '0.0.0.0' , port = PORT )
299+ app .run (host = HOSTNAME , port = PORT )
289300
290301
291302if __name__ == '__main__' :
0 commit comments