Back to index

python-biopython  1.60
requires_internet.py
Go to the documentation of this file.
00001 # Copyright 2002 by Jeffrey Chang.  All rights reserved.  This code is
00002 # part of the Biopython distribution and governed by its license.
00003 # Please see the LICENSE file that should have been included as part
00004 # of this package.
00005 
00006 # This module attempts to detect whether the internet is available.
00007 # To use it, import requires_internet into your Python code, and call
00008 # requires_internet.check().  If the internet is available, then the
00009 # import statement succeeds.  If it is not, then the statement will
00010 # result in a MissingExternalDependencyError exception.
00011 
00012 from Bio import MissingExternalDependencyError 
00013 
00014 def check():
00015     try:
00016         check.available
00017     except AttributeError:
00018         # I'm going to check for internet availability 
00019         RELIABLE_DOMAIN = "biopython.org"
00020         import socket
00021         try:
00022             socket.getaddrinfo(RELIABLE_DOMAIN,
00023                                80,
00024                                socket.AF_UNSPEC,
00025                                socket.SOCK_STREAM)
00026         except socket.gaierror, x:
00027             check.available = False
00028         else:
00029             check.available = True
00030     if not check.available:
00031         raise MissingExternalDependencyError("internet not available")