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)
-
dhcp
: enable DHCP for IP parameters. Default value is True. Ifdhcp
is Trueip
,mask
,gateway
,dns
arguments are ignored. Note For thedns
see also theforce_dns
argument. Whendhcp
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. Whendhcp
is True this parameter is ignored, unlessforce_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 theforce_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 thehostname
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()
configure()
function. function stop
¶
stop()
disconnect()
but performed only if the interface was previously connected. function disconnect
¶
disconnect()
function down
¶
down()
function resolve
¶
resolve(host)
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()
Bool
: DHCP enabled (True) or disabled (False)String
: IP addressString
: netmaskString
: gatewayString
: DNSString
: MAC address
function is_connected
¶
is_connected()
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)