.. |br| raw:: html
.. _ex_eth:
Ethernet/TCP/UDP sockets
========================
Ethernet related examples.
Connect with DHCP
-----------------
You need a router to assign an IP address to the SBC.
::
import time
import network
lan = network.LAN()
lan.active(True)
while(not lan.isconnected()):
print(lan.isconnected())
time.sleep(1)
print(lan.ifconfig())
Connect with a fixed IP address
-------------------------------
The SBC IP address configuration must be compatible with the network where you want to connect. |br|
.. note:: You can check `this tutorial `_ to set a fixed IP address on your PC.
::
import network
lan = network.LAN()
lan.active(True)
(lan_ip, lan_subnet, lan_gateway, lan_dns) = ("10.0.0.1", "255.255.255.0", "10.0.0.2", "8.8.8.8")
lan.ifconfig((lan_ip, lan_subnet, lan_gateway, lan_dns))
print(lan.ifconfig())
Download a webpage
------------------
A simple urequests request is needed to download a webpage.
::
import network
import urequests as requests
lan = network.LAN()
lan.active(True)
(lan_ip, lan_subnet, lan_gateway, lan_dns) = ("10.0.0.1", "255.255.255.0", "10.0.0.2", "8.8.8.8")
lan.ifconfig((lan_ip, lan_subnet, lan_gateway, lan_dns))
req = requests.get("https://www.google.com/")
print( req.content )
req.close()
HTTP webserver
--------------
This examples shows simple HTTP server that can handle client requests.
.. note:: In order to run this test, you need to create a folder on the SBC flash memory called **web**, and place inside this file: :download:`index.html `.
::
import time
import network
import socket
lan = network.LAN()
lan.active(True)
(lan_ip, lan_subnet, lan_gateway, lan_dns) = ("10.0.0.1", "255.255.255.0", "10.0.0.2", "8.8.8.8")
lan.ifconfig((lan_ip, lan_subnet, lan_gateway, lan_dns))
index_html = open( "./web/index.html" ).read()
ip, port = "", 80
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
sock.bind( (ip, port) )
sock.listen(0)
while( True ):
conn, addr = sock.accept()
buf = conn.recv( 1024 )
if( b"GET / HTTP/1.1" in buf ):
conn.sendall( index_html )
else:
conn.sendall( "404" )
conn.close()
time.sleep(0.1)
Send information to TCP socket
------------------------------
::
import time
import network
import socket
lan = network.LAN()
lan.active( True )
while( not lan.isconnected() ):
print( lan.isconnected() )
time.sleep(1)
print( lan.ifconfig() )
ip, port = '192.168.1.102', 1234
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.send("hello world")
print( sock.recv(1024) )
sock.close()
Send information to UDP socket
------------------------------
::
import time
import network
import socket
lan = network.LAN()
lan.active( True )
while( not lan.isconnected() ):
print( lan.isconnected() )
time.sleep(1)
print( lan.ifconfig() )
ip, port = '192.168.1.102', 1234
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.send("hello world")
print( sock.recv(1024) )
sock.close()
Socket echo server
------------------
This example shows how to create an echo server
::
import time
import network
import socket
lan = network.LAN()
lan.active( True )
while( not lan.isconnected() ):
print( lan.isconnected() )
time.sleep(1)
print( lan.ifconfig() )
ip, port = "", 80
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
sock.bind( (ip, port) )
sock.listen(0)
while( True ):
conn, addr = sock.accept()
buf = conn.recv( 1024 )
conn.sendall( buf )
conn.close()
time.sleep(0.1)
Socket client
-------------
This example shows how to create simple socket client that connects to an socket server
::
import time
import network
import socket
lan = network.LAN()
lan.active( True )
while( not lan.isconnected() ):
print( lan.isconnected() )
time.sleep(1)
print( lan.ifconfig() )
ip, port = "", 80
sock = socket.socket()
sock.connect((host, port))
sock.send( b"Hello World!" )
print( sock.recv(1024) )
sock.close()