def _geturl(self, url, use_alarm=True): """ Download the given url. If use_alarm is True (the default) timeout and return the TIMEOUT object if the default download timeout is exceeded. """ if not use_alarm: return urllib2.urlopen(url).read() try: alarm(self._url_timeout) return urllib2.urlopen(url).read() except KeyboardInterrupt: return TIMEOUT finally: cancel_alarm()
def _timeit(self, code): """ Time evaluation of the given code, timing out after self._url_timeout seconds, and using the default number and repeat values as options to timeit. """ try: alarm(self._url_timeout) T = sage_timeit(code, globals(), number=self._timeit_number, repeat=self._timeit_repeat) except KeyboardInterrupt: return TIMEOUT finally: cancel_alarm() return T
def test_allpub(self): """ View every single one of the published worksheets on the Sage notebook server. """ if self._verbose: print "testing download of all published worksheets..." tm = walltime() pub = self.get_urls_of_published_worksheets() try: alarm(self._url_timeout) for i, X in enumerate(pub): t0 = walltime() self._geturl(X, use_alarm=False) if self._verbose: print "Got %s [%s/%s] %.2f seconds"%(X,i+1,len(pub), walltime(t0)) return walltime(tm) except KeyboardInterrupt: return TIMEOUT finally: cancel_alarm()
try: alarm(5) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((interface, port)) except socket.error, msg: if msg[1] == 'Connection refused': if verbose: print "Using port = %s" % port return port except KeyboardInterrupt: if verbose: print "alarm" alarm_count += 1 if alarm_count >= 10: break pass finally: cancel_alarm() if verbose: print "Port %s is already in use." % port print "Trying next port..." raise RuntimeError, "no available port." def open_page(address, port, secure, path=""): if secure: rsrc = 'https' else: rsrc = 'http' os.system('%s %s://%s:%s%s 1>&2 > /dev/null &' % (browser(), rsrc, address, port, path))
try: alarm(5) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((interface, port)) except socket.error, msg: if msg[1] == 'Connection refused': if verbose: print "Using port = %s"%port return port except KeyboardInterrupt: if verbose: print "alarm" alarm_count += 1 if alarm_count >= 10: break pass finally: cancel_alarm() if verbose: print "Port %s is already in use."%port print "Trying next port..." raise RuntimeError, "no available port." def open_page(address, port, secure, path=""): if secure: rsrc = 'https' else: rsrc = 'http' os.system('%s %s://%s:%s%s 1>&2 > /dev/null &'%(browser(), rsrc, address, port, path)) def pad_zeros(s, size=3):