So today I bought the course from udemy:

Mastering Python – Networking and Security. The first lessons is about Python 2 vs 3, standard Python,  console, running scripts, variables, loops, scoping, subroutines, system calls etc, a new refresh with code and videos.

Moving on…..

Networking section 3:
22: Network byte order:

Endian describes how the order order in a multi-byte digital integer is arranged, more specifically whether they are read from right to left or left to right. The term is usually used in the two variants Big Endian and Little Endian.

The term originated originally from Jonathan Swift’s novel Gullivers travels from 1726. The novel described the tension in the country of Lilleput, where war broke out because of the controversies about which end of the egg they were going to cook.

23: Name server lookups

https://docs.python.org/3/library/socket.html#module-socket

Two important functions from socket:

socket.gethostbyaddr(“8.8.8.8”), machines / devices uses this

socket.gethostbyname(“www.google.com”), humans can easily remember this for use.

The result of endian and the two functions:

24: Network client:

Next up is a network client that will send a byte array to local host on port 5555.

The client will fail if there is noting listening on that port, so we need to move on to the next step or use netcat (Netcat (often abbreviated to nc) is a computer networking utility for reading from and writing to network connections using TCP or UDP.)

import socket
host = “localhost”
# family = internet, type = stream means tcp
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = (host, 5555)
mysock.connect(addr)
try:
print(“Try send teleg”)
msg = b”Client data to socket\n” # byte array to send
mysock.sendall(msg)
except Exception as e:
print(“Socket error “+str(e))
finally:
mysock.close()

24: Network server

The network server will listen port 5555, for both the server and client we will use TCP and internet as layer. We will also decode the byte array sent and print it as well as write it to storage.dat

import socket
size = 512 # max size of data we will recieve
host= “”
port = 5555
# family = internet, type = stream means tcp
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# we have a socket, we need to bind to an ip21 adr and port
sock.bind((host, port))
sock.listen(5)
print(“Server started”)
# we can store information from other end
# once we accepct the connection attempt
c, addr = sock.accept()
data = c.recv(size)
if data:
teleg = data.decode(“utf-8”)
f = open(“storage.dat”, “a”)
print(“Connection from “, addr[0] +” telegram “+ teleg)
store = addr[0] + “:” + teleg
f.write(store)
f.close
sock.close()
Now lets test the server and client:
And here you can see that that the telegram that the client sent is printed at the server and also saved to file.