File: requires_internet.py

package info (click to toggle)
python-biopython 1.78%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 65,756 kB
  • sloc: python: 221,141; xml: 178,777; ansic: 13,369; sql: 1,208; makefile: 131; sh: 70
file content (34 lines) | stat: -rw-r--r-- 1,212 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright 2002 by Jeffrey Chang.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

# This module attempts to detect whether the internet is available.
# To use it, import requires_internet into your Python code, and call
# requires_internet.check().  If the internet is available, then the
# import statement succeeds.  If it is not, then the statement will
# result in a MissingExternalDependencyError exception.

"""Common code to check if the internet is available."""

from Bio import MissingExternalDependencyError


def check():
    try:
        check.available
    except AttributeError:
        # I'm going to check for internet availability
        RELIABLE_DOMAIN = "biopython.org"
        import socket

        try:
            socket.getaddrinfo(
                RELIABLE_DOMAIN, 80, socket.AF_UNSPEC, socket.SOCK_STREAM
            )
        except socket.gaierror as x:
            check.available = False
        else:
            check.available = True
    if not check.available:
        raise MissingExternalDependencyError("internet not available")