Ethernet

Ethernet related examples.

Get network information

To get network information, is activate the interface and then wait until is connected

import time
import network
lan = network.LAN()
lan.active( True )
while( not lan.isconnected() ):
    print( lan.isconnected() )
    time.sleep(1)
print( lan.ifconfig() )

Download a webpage

A simple uresuqests request is needed for download a webpage

import time
import network
import urequests as requests
lan = network.LAN()
lan.active( True )
while( not lan.isconnected() ):
    print( lan.isconnected() )
    time.sleep(1)
print( lan.ifconfig() )
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

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() )
index_html = open( "./web/index.html" ).read()
style_css = open( "./web/css/style.css" ).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 )
    elif( b"GET /css/style.css HTTP/1.1" in buf ):
        conn.sendall( style_css )
    else:
        conn.sendall( "404" )
    conn.close()
    time.sleep(0.1)

Make NTRIP connection

Short description

# TODO

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()