Skip to content

Commit c3e80f8

Browse files
NikolasOliveiradanvk
authored andcommitted
Add support for using a hostname
- A new config file option enables webdiff to start the diff using the client hostname. This makes sharing a webdiff around a local network much easier.
1 parent 3d83188 commit c3e80f8

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

webdiff/app.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import mimetypes
1111
import os
12+
import platform
1213
import requests
1314
import socket
1415
import sys
@@ -53,6 +54,7 @@ class Config:
5354

5455
DIFF = None
5556
PORT = None
57+
HOSTNAME = "localhost"
5658

5759
if 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

228230
def 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

237240
def usage_and_die():
@@ -267,7 +270,7 @@ def is_webdiff_from_head():
267270

268271

269272
def 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
285296
Close 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

291302
if __name__ == '__main__':

0 commit comments

Comments
 (0)