Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
Archivo: misc.py Proyecto: bollu/sagenb
def find_next_available_port(interface, start, max_tries=100, verbose=False):
    """
    Find the next available port at a given interface, that is, a port for
    which a current connection attempt returns a 'Connection refused'
    error message.  If no port is found, raise a RuntimeError exception.

    INPUT:

    - ``interface`` - address to check

    - ``start`` - an int; the starting port number for the scan

    - ``max_tries`` - an int (default: 100); how many ports to scan

    - ``verbose`` - a bool (default: True); whether to print information
      about the scan

    OUTPUT:

    - an int - the port number

    EXAMPLES::

        sage: from sagenb.misc.misc import find_next_available_port
        sage: find_next_available_port('127.0.0.1', 9000, verbose=False)   # random output -- depends on network
        9002
    """
    alarm_count = 0
    for port in range(start, start + max_tries + 1):
        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
Ejemplo n.º 4
0
def find_next_available_port(interface, start, max_tries=100, verbose=False):
    """
    Find the next available port at a given interface, that is, a port for
    which a current connection attempt returns a 'Connection refused'
    error message.  If no port is found, raise a RuntimeError exception.

    INPUT:

    - ``interface`` - address to check

    - ``start`` - an int; the starting port number for the scan

    - ``max_tries`` - an int (default: 100); how many ports to scan

    - ``verbose`` - a bool (default: True); whether to print information
      about the scan

    OUTPUT:

    - an int - the port number

    EXAMPLES::

        sage: from sagenb.misc.misc import find_next_available_port
        sage: find_next_available_port('127.0.0.1', 9000, verbose=False)   # random output -- depends on network
        9002
    """
    alarm_count = 0  
    for port in range(start, start+max_tries+1):
        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 
Ejemplo n.º 5
0
 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()