PP - Python Networking1 - English
PP - Python Networking1 - English
(https://www.varonis.com/blog/cybersecurity-statistics/)
Why learn both?
“If you know the enemy and know yourself, you need not fear the result of a
hundred battles. If you know yourself but not the enemy, for every victory gained
you will also suffer a defeat. If you know neither the enemy nor yourself, you will
succumb in every battle.”
― Sun Tzu, The Art of Warr
Client Server
The device that requests The device that accepts Client
data from Server. requests and sends data back.
5503
import socket
SERVER.py
Sockets -Creating a Socket
Next, we create a socket object and name it as ‘sock’
sock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
If you don’t know those terms, no worries. They are not important now. SERVER.py
Try it yourself!
Declare variables IP as a string "127.0.0.1" and PORT
number as an integer 5005.
SERVER.py
Answer
Declare IP Address as a string "127.0.0.1" and
PORT number as an integer 5005.
IP = "127.0.0.1"
PORT = 5005
SERVER.py
Bind the sockets
with IP and PORT
Sockets - Binding
Next, we need to bind both IP Address and Port Number
to a socket.
sock.bind((IP, PORT))
Now, the courier knows which door to send to! SERVER.py
Receiving Data
Sockets - Receiving
We are nearly there! Now you just have to stand at the
door and get ready to receive data.
while True:
data, addr = sock.recvfrom(1024)
print ("received message: ", data)
1) Create a socket
2) Declare IP Address and Port Numbers (should they be the same as before?)
3) Bind the socket
Answer
import socket
IP = "127.0.0.1"
PORT = 5005
MESSAGE = "Hello, World!"
sock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
CLIENT.py
Answer
import socket The reason why this is the same is
IP = "127.0.0.1" because you are in the same house
(computer)!
PORT = 5005
MESSAGE = "Hello, World!"
sock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
CLIENT.py
Sending Data
Like finally!
Sockets - Sending
Last but not least, the client has to send the actual message
CLIENT.py
Results
You should see something like this appear in the Server window
import socket
sock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
IP = "127.0.0.1"
PORT = 5005
MESSAGE = "Hello, World!"
sock.sendto(MESSAGE.encode(), (IP, PORT))
CLIENT.py
So what just happened...
We managed to send a message as packets from one ‘device’
to another! This is how messages are sent in your messaging
apps like Whatsapp and Telegram.
Lets recap!
1) What are IP Addresses, Port Numbers and Sockets?
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = "127.0.0.1"
PORT = 5005
BUFFER_SIZE = 20
sock.bind((IP, PORT))
sock.listen(1)
conn, addr = sock.accept()
while True:
data = conn.recv(BUFFER_SIZE)
if not data: break
conn.send(data)
print ("received data:", data)
conn.close()
TSERVER.py
Making the Socket
again
Sockets - Back to Server
Let’s make a new script called TServer.py
IP = "127.0.0.1"
PORT = 5005
BUFFER_SIZE = 20
sock.bind((IP, PORT))
TSERVER.py
Listening and Accepting
For TCP
TCP SERVER
4 3 2 1
Sockets - Listening and Accepting
For this stream to be created in the first place, the Server
has to listen, aka wait at the door, for someone to request
for a connection.
s.listen(1)
TSERVER.py
Sockets - Listening and Accepting
Once someone comes, we accept him and send back a
confirmation. A connection is thus established.
conn, addr = s.accept()
data = conn.recv(BUFFER_SIZE)
TSERVER.py
TCP
We eventually will stop receiving data so to stop our loop,
we add this.
if not data: break
TSERVER.py
TCP
Did you know that the stream is a two-way connection?
conn.send(data)
TSERVER.py
R1 R2 R3 R4
TCP
Let’s print the message so that we know we have received it.
print ("received data:", data)
Last but not least, we close the ‘door’ outside the loop, to
prevent any errors from occuring.
conn.close()
TSERVER.py
Result- What you should have
while True: This causes the following code to loop continuously
TSERVER.py
Mini-activity - Try it yourself for the Client!
The TCP version of Client is largely similar to its UDP
counterpart, but….
TCLIENT.py
Results
You should see this appear on both the Client and Server side
TCP UDP
Connection-oriented Connection-less
Reliable (Can replace lost packets) Unreliable (No replacement for lost packets)
Requires more setup and more overhead Requires less setup and less overhead
Chapter 3
Chatroom
Chapter Overview
In this chapter, we will skip a few steps and provide you with a working
code of a 2-person chatroom.
IP Address
Let’s Hack #2
Enable port forwarding on your computer.
Port forwarding allows you to pass on packets you
receive to another destination.
sudo sysctl -w net.inet.ip.forwarding=1
1) Application Layer
How your apps use the internet
2) Transport Layer
How your apps communicate with other app
3) Internet Layer
How your data travels across the web
4) Network Access Layer
How your physical devices (WiFi) facilitate that travel
Additional Notes - Network layers
The network can be said to be represented by various models. What this
tutorial used as reference was the TCP/IP model.
IP = "127.0.0.1"
print(IP)
Python Cheat Sheet - Variables
● Variables have many types
The double quotes (“”) means
IP = "127.0.0.1" that this is a String (think of it
as a series of characters)
sock = socket.socket(...)
socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
while True:
data, addr = sock.recvfrom(1024)
print ("received message: ", data)