Skip to content

Ethernet

Ethernet

This module implements a generic ethernet interface. To function correctly it needs a ethernet driver to be loaded, so that the module can use the driver to access the underlying hardware.

The link between the net module and the ethernet driver is established without the programmer intervetion by the driver itself.

Exceptions

exception CantRegisterInterfaceError

The ethernet interface cannot be registered.

exception ConnectionError

A connection error occurred during ethernet interface configuration.

exception ConnectionTimeoutError

The connection attempt has timed out.

exception ResolveError

Host cannot be resolved to its IP address.

exception NetworkGenericError

Generic error occurred

Functions

function configure

configure(dhcp=True, ip="", mask="", gateway="", dns="8.8.8.8", timeout=60000, hostname="", name="eth0", force_dns=False)
Configures the ethernet interface with given arguments.

  • dhcp: enable DHCP for IP parameters. Default value is True. If dhcp is True ip, mask, gateway, dns arguments are ignored. Note For the dns see also the force_dns argument. When dhcp is False, the arguments for IP parameters are:

  • ip: is the static IP address. Default value is empty string.

  • mask: the net mask expressed as A.B.C.D dotted address. Default value is empty string.
  • gateway: the gateway IP address to be used as default router. Default value is empty string.
  • dns: the Domain Name Server to be used for name resolution. When dhcp is True this parameter is ignored, unless force_dns is True. Default value is "8.8.8.8", the Google DNS.

  • force_dns: forces a custom DNS address when DHCP is on. When the force_dns is True the DNS address provided by the DHCP server is ignored. Default value is False.

  • timeout: connection timeout in milliseconds. ConnectionTimeoutError is raised if connection do not succeed during this time. Default value is 60000 ms.

  • hostname: hostname associated with the interface. When the hostname is empty string, the dcn (Device Common Name) is used as hostname. Default value is empty string.
  • name: the interface name to be used in a multi interface scenario. Default value is "eth0".

function start

start()
The function starts the interface by initiating the DHCP configuration and other IP setups (routing, DNS, etc.). The DHCP or static IP parameters are used depending upon the arguments passed to the configure() function.

function stop

stop()
The interface is stopped, all connections dropped, and all socket closed related to ethernet interface. This is like disconnect() but performed only if the interface was previously connected.

function disconnect

disconnect()
The interface is stopped, all connections dropped, and all socket closed related to ethernet interface.

function down

down()
The ethernet interface is shut down by releasing all resources and low level drivers associated with it.

function resolve

resolve(host)
Resolves the symbolic name for the given host to its IP address by using the configured DNS server and returning a string with the result.

When the host cannot be resolved, the ResolveError exception is raised.

function info

info()
Returns a tuple with the IP parameters associated to the interface. The tuple is composed by the following elements:

  1. Bool: DHCP enabled (True) or disabled (False)
  2. String: IP address
  3. String: netmask
  4. String: gateway
  5. String: DNS
  6. String: MAC address

function is_connected

is_connected()
Returns True if the interface is connected, False otherwise.

Examples

Using the ethernet module is very easy:

from bsp import board

from networking import eth

board.init()
board.summary()

try:
    # Configure ethernet to use dhcp
    eth.configure(dhcp=True)
    # Start the interface
    eth.start()
    # Print the ip, gateway, mask, dns and mac address
    print(eth.info())
    # Try resolving some hostname via dns
    ip=eth.resolve("www.zerynth.com")
    print("resolved",ip)
    # sleep a little bit
    sleep(5000)
    # disable ethernet
    eth.stop()
except ConnectionError:
    print("Ethernet Connection Exception")
except ConnectionTimeoutError:
    print("Ethernet Connection Timeout Exception")
except ResolveError:
    print("Resolv error Exception")
except NetworkGenericError:
    print("Generic Ethernet Exception")
except Exception as e:
    raise e


while True:
    sleep(1000)

More configuration is also available:

from bsp import board

from networking import eth

board.init()
board.summary()

try:
    # Configure ethernet to use static address
    eth.configure(dhcp=False, ip="192.168.1.20", gateway="192.168.1.1", mask="255.255.255.0", dns="8.8.4.4")
    # Start the interface
    eth.start()
    # Print the ip, gateway, mask, dns and mac address
    print(eth.info())
    # Try resolving some hostname via dns
    ip=eth.resolve("www.zerynth.com")
    print("resolved",ip)
    # sleep a little bit
    sleep(5000)
    # disable ethernet
    eth.stop()
except ConnectionError:
    print("Ethernet Connection Exception")
except ConnectionTimeoutError:
    print("Ethernet Connection Timeout Exception")
except ResolveError:
    print("Resolv error Exception")
except NetworkGenericError:
    print("Generic Ethernet Exception")
except Exception as e:
    raise e


while True:
    sleep(1000)