def server(self, server_id): ''' get server associated with an id, create one if none exists ''' try: if server_id in self.server_dict: server_info = self.server_dict[server_id] return server_info['proxy'] else: url_fmt = "tcp://127.0.0.1:%i" rep_url = url_fmt % get_unused_ip_port() pub_url = url_fmt % get_unused_ip_port() out_url = url_fmt % get_unused_ip_port() DEBUG("%s \n\t RPC on %s \n\t pub on %s \n\t out on %s" % (server_id, rep_url, pub_url, out_url)) server = ZMQServer.spawn_server(self.classpath, rep_url, pub_url, out_url) proxy = ZMQ_RPC(rep_url) self.server_dict[server_id] = { 'server': server, 'proxy': proxy, 'rep_url': rep_url, 'pub_url': pub_url, 'out_url': out_url } return proxy except Exception as err: print 'Error getting server', server_id print str(err.__class__.__name__), ":", err exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) traceback.print_tb(exc_traceback, limit=30) return None
def server(self, server_id): ''' get server associated with an id, create one if none exists ''' try: if server_id in self.server_dict: server_info = self.server_dict[server_id] return server_info['proxy'] else: url_fmt = "tcp://127.0.0.1:%i" rep_url = url_fmt % get_unused_ip_port() pub_url = url_fmt % get_unused_ip_port() out_url = url_fmt % get_unused_ip_port() DEBUG("%s \n\t RPC on %s \n\t pub on %s \n\t out on %s" % (server_id, rep_url, pub_url, out_url)) server = ZMQServer.spawn_server(self.classpath, rep_url, pub_url, out_url) proxy = ZMQ_RPC(rep_url) self.server_dict[server_id] = { 'server': server, 'proxy': proxy, 'rep_url': rep_url, 'pub_url': pub_url, 'out_url': out_url } return proxy except Exception as err: print 'Error getting server', server_id print str(err.__class__.__name__), ":", err exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) traceback.print_tb(exc_traceback, limit=30) return None
def __init__(self, options): self.options = options user_dir = get_user_dir() # initialize some settings database = os.path.join(user_dir, 'projects.db') if options.reset or not os.path.exists(database): print "Resetting project database..." if os.path.exists(database): print "Deleting existing project database..." os.remove(database) pdb = Projects(database) pdb.create() options.orig_port = options.port if (options.port < 1): options.port = get_unused_ip_port() # save secret between restarts secret_file = os.path.join(user_dir, 'secret') if os.path.exists(secret_file): secret = open(secret_file, 'rb').read() else: secret = os.urandom(1024) open(secret_file, 'wb').write(secret) self.app = App(secret, options.external)
def get_websocket_url(self, server_id, stream_name, target_url): ''' Get the url of the websocket for the specified stream of the specified server; if websocket server does not exist, start it. server_id: The id of the server. stream_name: The name of the stream ('out' or 'pub'). target_url: The relative url at which to serve the websocket. ''' if stream_name not in ['out', 'pub']: print >>sys.stderr, \ "ZMQServerManager - Invalid stream requested:", stream_name return None server_info = self.server_dict[server_id] ws_url_key = stream_name + '_ws_url' if ws_url_key in server_info: return server_info[ws_url_key] else: ws_port = get_unused_ip_port() ws_url = 'ws://%s:%d%s' % (self.address, ws_port, target_url) server_info[stream_name + '_ws_server'] = \ ZMQStreamServer.spawn_process(server_info[stream_name+'_url'], ws_port, target_url, self.external) server_info[ws_url_key] = ws_url return ws_url
def __init__(self, options): self.options = options user_dir = get_user_dir() # initialize some settings database = os.path.join(user_dir, 'projects.db') if options.reset or not os.path.exists(database): print "Resetting project database..." if os.path.exists(database): print "Deleting existing project database..." os.remove(database) pdb = Projects(database) pdb.create() options.orig_port = options.port if (options.port < 1): options.port = get_unused_ip_port() # save secret between restarts secret_file = os.path.join(user_dir, 'secret') if os.path.exists(secret_file): secret = open(secret_file, 'rb').read() else: secret = os.urandom(1024) open(secret_file, 'wb').write(secret) self.app = App(secret, options.external)
def get_out_server_url(self, server_id, ws_url): ''' get the output socket web server for the server associated with an id, create one if none exists ''' server_info = self.server_dict[server_id] if 'out_server' in server_info: return server_info['out_server_url'] else: ws_port = get_unused_ip_port() ws_addr = 'ws://localhost:%d%s' % (ws_port, ws_url) server_info['out_server'] = ZMQStreamServer.spawn_process(server_info['out_url'], ws_port, ws_url) server_info['out_server_url'] = ws_addr return ws_addr
def setUp(self): self.startdir = os.getcwd() gui_path = os.path.dirname(inspect.getfile(run_server)) os.chdir(gui_path) # so server can find it's static files self.port = get_unused_ip_port() self.server = Process(target=run_server,args=(self.port,)) self.server.start() self.url = "http://localhost:"+str(self.port) self.verificationErrors = [] # the selenium standalone server should be running on port 4444 self.selenium = selenium("localhost", 4444, "*chrome", self.url) self.selenium.start()
def get_out_server_url(self, server_id, ws_url): ''' get the output socket web server for the server associated with an id, create one if none exists ''' server_info = self.server_dict[server_id] if 'out_server' in server_info: return server_info['out_server_url'] else: ws_port = get_unused_ip_port() ws_addr = 'ws://localhost:%d%s' % (ws_port, ws_url) server_info['out_server'] = ZMQStreamServer.spawn_process( server_info['out_url'], ws_port, ws_url) server_info['out_server_url'] = ws_addr return ws_addr
def get_pub_server_url(self, server_id, ws_url): ''' get the publisher socket web server for the server associated with an id, create one if none exists ''' server_info = self.server_dict[server_id] if 'pub_server' in server_info: return server_info['pub_server_url'] else: ws_port = get_unused_ip_port() ws_addr = 'ws://%s:%d%s' % (self.address, ws_port, ws_url) server_info['pub_server'] = \ ZMQStreamServer.spawn_process(server_info['pub_url'], ws_port, ws_url, self.external) server_info['pub_server_url'] = ws_addr return ws_addr
def get_pub_server_url(self, server_id, ws_url): ''' get the publisher socket web server for the server associated with an id, create one if none exists ''' server_info = self.server_dict[server_id] if 'pub_server' in server_info: return server_info['pub_server_url'] else: ws_port = get_unused_ip_port() ws_addr = 'ws://%s:%d%s' % (self.address, ws_port, ws_url) server_info['pub_server'] = \ ZMQStreamServer.spawn_process(server_info['pub_url'], ws_port, ws_url, self.external) server_info['pub_server_url'] = ws_addr return ws_addr
def setup_server(virtual_display=True): """ Start server on ``localhost`` using an unused port. """ global _display, _display_set # Check if already setup (or skipping setup). if _display_set: return # Start server. port = get_unused_ip_port() TEST_CONFIG['port'] = port server_dir = 'gui-server' if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) TEST_CONFIG['server_dir'] = server_dir orig = os.getcwd() os.chdir(server_dir) stdout = open('stdout', 'w') TEST_CONFIG['stdout'] = stdout try: server = subprocess.Popen(('python', '-m', 'openmdao.gui.omg', '--server', '--port', str(port)), stdout=stdout, stderr=subprocess.STDOUT) finally: os.chdir(orig) TEST_CONFIG['server'] = server # Wait for server port to open. for i in range(200): # ~20 sec. time.sleep(.1) try: sock = socket.create_connection(('localhost', port)) except socket.error as exc: if 'Connection refused' not in str(exc): raise RuntimeError('connect failed: %r' % exc) else: sock.close() break else: raise RuntimeError('Timeout trying to connect to localhost:%d' % port) # If running headless, setup the virtual display. if virtual_display: _display = Display() _display.start() _display_set = True
def setup_server(virtual_display=True): """ Start server on ``localhost`` using an unused port. """ global _display, _display_set # Check if already setup (or skipping setup). if _display_set: return # Start server. port = get_unused_ip_port() TEST_CONFIG['port'] = port server_dir = 'gui-server' if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) TEST_CONFIG['server_dir'] = server_dir orig = os.getcwd() os.chdir(server_dir) stdout = open('stdout', 'w') TEST_CONFIG['stdout'] = stdout try: server = subprocess.Popen(('python', '-m', 'openmdao.gui.omg', '--server', '--port', str(port)), stdout=stdout, stderr=subprocess.STDOUT) finally: os.chdir(orig) TEST_CONFIG['server'] = server # Wait for server port to open. for i in range(200): # ~20 sec. time.sleep(.1) try: sock = socket.create_connection(('localhost', port)) except socket.error as exc: if 'refused' not in str(exc): raise RuntimeError('connect failed: %r' % exc) else: sock.close() break else: raise RuntimeError('Timeout trying to connect to localhost:%d' % port) # If running headless, setup the virtual display. if virtual_display: _display = Display(size=(1280, 1024)) _display.start() _display_set = True
def serve(self): ''' Start server listening on port, launch browser if requested, and start the ioloop. ''' self.http_server = httpserver.HTTPServer(self.app) for retry in range(3): try: if self.options.external: self.http_server.listen(self.options.port) else: self.http_server.listen(self.options.port, address='localhost') except socket.error: # Possibly 'Address already in use', try finding another port. if self.options.orig_port < 1 and retry < 2: self.options.port = get_unused_ip_port() else: raise else: break if not self.options.serveronly: launch_browser(self.options.port, self.options.browser) if self.options.external: print '***********************************************************' print '** WARNING: You have exposed the server to the external **' print '** network. THIS IS NOT SAFE!! Clients will **' print '** have access to a command prompt on the host **' print '** computer with the identity and privileges of **' print '** the userid under which the server was run. **' print '** **' print '** This is very dangerous and you should NOT do it. **' print '** You exercise this option at your own risk!!! **' print '** (Ctrl-C to terminate server) **' print '***********************************************************' DEBUG('Serving on port %d' % self.options.port) try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: DEBUG('interrupt received, shutting down.')
def serve(self): ''' Start server listening on port, launch browser if requested, and start the ioloop. ''' self.http_server = httpserver.HTTPServer(self.app) for retry in range(3): try: if self.options.external: self.http_server.listen(self.options.port) else: self.http_server.listen(self.options.port, address='localhost') except socket.error: # Possibly 'Address already in use', try finding another port. if self.options.orig_port < 1 and retry < 2: self.options.port = get_unused_ip_port() else: raise else: break if not self.options.serveronly: launch_browser(self.options.port, self.options.browser) if self.options.external: print '***********************************************************' print '** WARNING: You have exposed the server to the external **' print '** network. THIS IS NOT SAFE!! Clients will **' print '** have access to a command prompt on the host **' print '** computer with the identity and privileges of **' print '** the userid under which the server was run. **' print '** **' print '** This is very dangerous and you should NOT do it. **' print '** You exercise this option at your own risk!!! **' print '** (Ctrl-C to terminate server) **' print '***********************************************************' DEBUG('Serving on port %d' % self.options.port) try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: DEBUG('interrupt received, shutting down.')
def test_get_unused_ip_port(self): ip_port = get_unused_ip_port() self.assertTrue(isinstance(ip_port, int)) # actually, in practice the range does not go that low but it varies from system to system self.assertTrue(ip_port >= 1024) self.assertTrue(ip_port <= 65535)
help="preferred browser") parser.add_option("-s", "--server", action="store_true", dest="serveronly", help="don't launch browser, just run server") parser.add_option("-i", "--init", action="store_true", dest="initialize", help="(re)initialize settings") parser.add_option("-r", "--reset", action="store_true", dest="reset", help="reset project database (valid only with -i and without -d)") (options, args) = parser.parse_args() if options.initialize or not os.path.exists('settings.py'): if options.reset and not options.devserver: init(reset=True) else: init(reset=False) if (options.port < 1): options.port = get_unused_ip_port() if not options.serveronly: # NOTE: with dev server, you are going to get two browsers # this is due to forking I suppose # not sure what can be done about it launch_browser(options.port, options.browser) if (options.devserver): dev(options.port) else: pro(options.port)
load: - %(gd)s/static/js/require-jquery* - %(gd)s/static/js/ba-debug.min.js - %(gd)s/static/js/openmdao/Util.js - %(gd)s/static/js/openmdao/Model.js - %(gd)s/static/js/sinon-1.2.0.js - %(gd)s/test/js_unit_tests/test_model.js ''' % { 'port_num': str( port_num ), 'gd': gui_directory } config_file_temp.close() return config_file_temp port_number = get_unused_ip_port() config_file = create_jstestdriver_config_file( port_number ) jstestdriver_path = os.path.join( gui_directory, "test/js_unit_tests", "JsTestDriver-1.3.3c.jar" ) class BrowserJsUnitTestCase(JsTestDriverTestCase): """base class for testing GUI JavaScript""" config_filename = config_file.name def get_browser_info( self ): '''returns browser name and exes''' pass def setUp(self):
def test_get_unused_ip_port(self): ip_port = get_unused_ip_port() self.assertTrue( isinstance( ip_port, int ) ) # actually, in practice the range does not go that low but it varies from system to system self.assertTrue( ip_port >= 1024 ) self.assertTrue( ip_port <= 65535 )
- %(gd)s/static/js/openmdao/ValueEditor.js - %(gd)s/static/js/sinon-1.2.0.js - %(gd)s/test/js_unit_tests/src/*.js test: - %(gd)s/test/js_unit_tests/test/*.js ''' % { 'port_num': str(port_num), 'gd': gui_directory } config_file_temp.close() return config_file_temp port_number = get_unused_ip_port() config_file = create_jstestdriver_config_file(port_number) jstestdriver_path = os.path.join(gui_directory, "test/js_unit_tests", "JsTestDriver-1.3.3c.jar") class BrowserJsUnitTestCase(JsTestDriverTestCase): """base class for testing GUI JavaScript""" config_filename = config_file.name def get_browser_info(self): '''returns browser name and exes''' pass def setUp(self): super(BrowserJsUnitTestCase, self).setUp()
def setup_server(virtual_display=True): """ Start server on ``localhost`` using an unused port. """ global _display, _display_set # Check if already setup (or skipping setup). if _display_set: return # Start server. server_dir = 'gui-server' # Try to clean up old server dir. If this fails (looking at you Windows), # then use another directory. if os.path.exists(server_dir): try: shutil.rmtree(server_dir, onerror=onerror) except WindowsError as exc: print >> sys.stderr, 'Could not delete %s: %s' % (server_dir, exc) nxt = 1 while os.path.exists(server_dir): nxt += 1 server_dir = 'gui-server-%s' % nxt os.mkdir(server_dir) TEST_CONFIG['server_dir'] = server_dir orig = os.getcwd() os.chdir(server_dir) try: stdout = open('stdout', 'w') TEST_CONFIG['stdout'] = stdout sigfile = os.path.join(os.getcwd(), 'SIGTERM.txt') if os.path.exists(sigfile): os.remove(sigfile) TEST_CONFIG['sigfile'] = sigfile port = get_unused_ip_port() TEST_CONFIG['port'] = port server = subprocess.Popen(('python', '-m', 'openmdao.gui.omg', '--server', '--port', str(port)), stdout=stdout, stderr=subprocess.STDOUT) finally: os.chdir(orig) TEST_CONFIG['server'] = server TEST_CONFIG['failed'] = [] # Wait for server port to open. for i in range(200): # ~20 sec. time.sleep(.1) try: sock = socket.create_connection(('localhost', port)) except socket.error as exc: if 'refused' not in str(exc): raise RuntimeError('connect failed: %r' % exc) else: sock.close() break else: raise RuntimeError('Timeout trying to connect to localhost:%d' % port) # If running headless, setup the virtual display. if sys.platform != 'win32' and virtual_display: _display = Display(size=(1280, 1024)) _display.start() _display_set = True
def setup_server(virtual_display=True): """ Start server on ``localhost`` using an unused port. """ global _display, _display_set # Check if already setup (or skipping setup). if _display_set: return # Start server. port = get_unused_ip_port() TEST_CONFIG['port'] = port server_dir = 'gui-server' # Try to clean up old server dir. If this fails (looking at you Windows), # then use another directory. if os.path.exists(server_dir): try: shutil.rmtree(server_dir, onerror=onerror) except WindowsError as exc: print >> sys.stderr, 'Could not delete %s: %s' % (server_dir, exc) nxt = 1 while os.path.exists(server_dir): nxt += 1 server_dir = 'gui-server-%s' % nxt os.mkdir(server_dir) TEST_CONFIG['server_dir'] = server_dir orig = os.getcwd() os.chdir(server_dir) try: stdout = open('stdout', 'w') TEST_CONFIG['stdout'] = stdout sigfile = os.path.join(os.getcwd(), 'SIGTERM.txt') if os.path.exists(sigfile): os.remove(sigfile) TEST_CONFIG['sigfile'] = sigfile server = subprocess.Popen(('python', '-m', 'openmdao.gui.omg', '--server', '--port', str(port)), stdout=stdout, stderr=subprocess.STDOUT) finally: os.chdir(orig) TEST_CONFIG['server'] = server TEST_CONFIG['failed'] = [] # Wait for server port to open. for i in range(200): # ~20 sec. time.sleep(.1) try: sock = socket.create_connection(('localhost', port)) except socket.error as exc: if 'refused' not in str(exc): raise RuntimeError('connect failed: %r' % exc) else: sock.close() break else: raise RuntimeError('Timeout trying to connect to localhost:%d' % port) # If running headless, setup the virtual display. if sys.platform != 'win32' and virtual_display: _display = Display(size=(1280, 1024)) _display.start() _display_set = True