Skip to content

Commit

Permalink
Server port is now variable
Browse files Browse the repository at this point in the history
  • Loading branch information
andfoy committed Apr 11, 2017
1 parent 3d80f7c commit 212e0aa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
18 changes: 14 additions & 4 deletions spyder_terminal/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
import os
import routes
import logging
import argparse
import coloredlogs
import tornado.web
import tornado.ioloop
from logic import term_manager


parser = argparse.ArgumentParser(
description='Websocket-based bash terminal server')

parser.add_argument('--port',
default=8070,
help="TCP port to be listened on")

LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
'-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)
Expand All @@ -21,7 +29,7 @@
clr = 'cls'


def main():
def main(port):
"""Create and setup a new tornado server."""
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
settings = {"static_path": os.path.join(
Expand All @@ -30,11 +38,11 @@ def main():
debug=True,
serve_traceback=True,
autoreload=True, **settings)
print("Server is now at: 127.0.0.1:8070")
print("Server is now at: 127.0.0.1:{}".format(port))
application.term_manager = term_manager.TermManager()
application.logger = LOGGER
ioloop = tornado.ioloop.IOLoop.instance()
application.listen(8070, address='127.0.0.1')
application.listen(port, address='127.0.0.1')
try:
ioloop.start()
except KeyboardInterrupt:
Expand All @@ -46,4 +54,6 @@ def main():

if __name__ == '__main__':
os.system(clr)
main()
args = parser.parse_args()
port = int(args.port)
main(port)
6 changes: 4 additions & 2 deletions spyder_terminal/terminalplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def __init__(self, parent):
SpyderPluginWidget.__init__(self, parent)
self.tab_widget = None
self.menu_actions = None
self.port = select_port(default_port=8070)
self.server = subprocess.Popen(
['python', osp.join(LOCATION, 'server', 'main.py')],
['python', osp.join(LOCATION, 'server', 'main.py'),
'--port', str(self.port)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
time.sleep(0.5)
Expand Down Expand Up @@ -183,7 +185,7 @@ def get_current_term(self):
def create_new_term(self, name=None, give_focus=True):
"""Add a new terminal tab."""
font = self.get_plugin_font()
term = TerminalWidget(self, font=font.family())
term = TerminalWidget(self, self.port, font=font.family())
self.add_tab(term)

def close_term(self, index=None, term=None):
Expand Down
6 changes: 4 additions & 2 deletions spyder_terminal/tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def test_terminal_font(qtbot):
terminal = setup_terminal(qtbot)
term = terminal.get_current_term()
qtbot.wait(TERM_UP)
status_code = requests.get('http://127.0.0.1:8070').status_code
port = terminal.port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200
term.set_font('Ubuntu Mono')
fonts = term.get_fonts()
Expand All @@ -80,7 +81,8 @@ def test_new_terminal(qtbot):
qtbot.wait(TERM_UP)

# Test if server is running
status_code = requests.get('http://127.0.0.1:8070').status_code
port = terminal.port
status_code = requests.get('http://127.0.0.1:{}'.format(port)).status_code
assert status_code == 200

# Move to LOCATION
Expand Down
5 changes: 3 additions & 2 deletions spyder_terminal/widgets/terminalgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
class TerminalWidget(QFrame):
"""Terminal widget."""

def __init__(self, parent, font=None):
def __init__(self, parent, port, font=None):
"""Frame main constructor."""
QWidget.__init__(self, parent)
self.view = TermView(self)
url = 'http://127.0.0.1:{0}'.format(port)
self.view = TermView(self, term_url=url)
self.font = font

layout = QVBoxLayout()
Expand Down

0 comments on commit 212e0aa

Please sign in to comment.