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)
dhcp
is True (the default) other arguments are ignored. When dhcp
is False, the other arguments are: ip
: is the IP address.mask
: the net mask expressed as A.B.C.D dotted address.gateway
: the gateway to be used as default router.dns
: the Domain Name Server to be used for name resolution. Default is "8.8.8.8", the Google DNS.timeout
: Connection timeout in milliseconds.ConnectionTimeoutError
is raised if connection do not succeed during this time. Default value 60000 ms.
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)