Skip to content

Commit 76dc920

Browse files
committed
Announce selected port better.
- Use actually used port in the printed message. - Flush the printed message to make sure it ends to the sys.stdout. - Support writing port number to a given `port_file`. Implements code changes related to issue robotframework#3. Documentation missing.
1 parent 70e90e4 commit 76dc920

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/robotremoteserver.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ class RobotRemoteServer(SimpleXMLRPCServer):
3939
_generic_exceptions = (AssertionError, RuntimeError, Exception)
4040
_fatal_exceptions = (SystemExit, KeyboardInterrupt)
4141

42-
def __init__(self, library, host='127.0.0.1', port=8270, allow_stop=True):
42+
def __init__(self, library, host='127.0.0.1', port=8270, port_file=None,
43+
allow_stop=True):
4344
SimpleXMLRPCServer.__init__(self, (host, int(port)), logRequests=False)
4445
self._library = library
4546
self._allow_stop = allow_stop
4647
self._register_functions()
4748
self._register_signal_handlers()
48-
self._log('Robot Framework remote server starting at %s:%s'
49-
% (host, port))
49+
self._announce_start(port_file)
5050
self.serve_forever()
5151

5252
def _register_functions(self):
@@ -65,6 +65,17 @@ def stop_with_signal(signum, frame):
6565
if hasattr(signal, 'SIGINT'):
6666
signal.signal(signal.SIGINT, stop_with_signal)
6767

68+
def _announce_start(self, port_file=None):
69+
host, port = self.server_address
70+
self._log('Robot Framework remote server starting at %s:%s.'
71+
% (host, port))
72+
if port_file:
73+
pf = open(port_file, 'w')
74+
try:
75+
pf.write(str(port))
76+
finally:
77+
pf.close()
78+
6879
def serve_forever(self):
6980
self._shutdown = False
7081
while not self._shutdown:
@@ -216,4 +227,5 @@ def _restore_stdout(self):
216227
def _log(self, msg, level=None):
217228
if level:
218229
msg = '*%s* %s' % (level.upper(), msg)
219-
print msg
230+
sys.stdout.write(msg + '\n')
231+
sys.stdout.flush()

0 commit comments

Comments
 (0)