<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zephaniah Joshua</title>
    <description>The latest articles on DEV Community by Zephaniah Joshua (@black_strok3).</description>
    <link>https://dev.to/black_strok3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F551771%2F1a62279c-d8a4-4319-8519-c78dbdaaedda.jpg</url>
      <title>DEV Community: Zephaniah Joshua</title>
      <link>https://dev.to/black_strok3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/black_strok3"/>
    <language>en</language>
    <item>
      <title>Interaction Between FTP Server and client</title>
      <dc:creator>Zephaniah Joshua</dc:creator>
      <pubDate>Sat, 09 Jan 2021 18:07:07 +0000</pubDate>
      <link>https://dev.to/black_strok3/interaction-between-ftp-server-and-client-3f9c</link>
      <guid>https://dev.to/black_strok3/interaction-between-ftp-server-and-client-3f9c</guid>
      <description>&lt;h6&gt;
  
  
  Photo by panumas nikhomkhai from Pexels
&lt;/h6&gt;

&lt;p&gt;The client sends a Passive[PASV] commands to the server which invokes passive modes&lt;br&gt;
The server responds with a 227 code with IP address and port of data socket&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4wRA5Jqh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ojj7nqfnfa9hg5032a53.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4wRA5Jqh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ojj7nqfnfa9hg5032a53.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The client sends a retrieve[RETR] command plus, the name of the file to be retrieved&lt;br&gt;
If the client is authorised to  access the file the server sends a 150 code[request is successful and the file is about to be transferred]&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nCbf9eLz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z5jsfg7n44xzmv26eoo5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nCbf9eLz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z5jsfg7n44xzmv26eoo5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to establish the connection to be used connection the client sends an SYC[synchronise] command&lt;br&gt;
The server replies with an acknowledgement and so does the client&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n_u9BaGA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ectosxvf3qdek5xnednu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n_u9BaGA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ectosxvf3qdek5xnednu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The server sends part of the file and the client acknowledges it and this continues until the whole file is received.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cuAiop3G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/grtz5mw7k7xgalimarrr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cuAiop3G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/grtz5mw7k7xgalimarrr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading please leave comments of suggestions or contact me &lt;a href="https://twitter.com/black_strok3"&gt;@black_strok3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>network</category>
    </item>
    <item>
      <title>Detecting missing pixels in an Image</title>
      <dc:creator>Zephaniah Joshua</dc:creator>
      <pubDate>Fri, 08 Jan 2021 14:03:21 +0000</pubDate>
      <link>https://dev.to/black_strok3/detecting-missing-pixels-in-an-image-30jc</link>
      <guid>https://dev.to/black_strok3/detecting-missing-pixels-in-an-image-30jc</guid>
      <description>&lt;h3&gt;
  
  
  Today we will be talking about finding missing data from the image we sent over the network. From our previous example of sending an image over a network, we are going to check if any pixels were lost during the transfer
&lt;/h3&gt;

&lt;h4&gt;
  
  
  First off we will try to map out an algorithm for detecting an error in the image file.
&lt;/h4&gt;

&lt;p&gt;Receive a pixel&lt;br&gt;
Store position of the pixel&lt;br&gt;
Receive the next &lt;br&gt;
Check if the pixels are in the correct order i.e pixel1, pixel2&lt;br&gt;
If they are not then there is a missing pixel&lt;br&gt;
Get the position of the missing pixel&lt;br&gt;
Continue this 6 steps repeatedly until the image is sent&lt;br&gt;
Send a request for missing pixels&lt;br&gt;
Open image&lt;br&gt;
And insert missing pixels into their position&lt;br&gt;
&lt;strong&gt;Because we are transferring images on the same machine I induced an error by skipping any pixel with an odd Y value.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  This is the code that sends the image
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from socket import *
from pickle import *
from PIL import Image
import time

#create a socket

sockfd = socket(AF_INET, SOCK_DGRAM)

#open image
image = Image.open("100kb.png")

#get image size
width, height = image.size
print(image.size)

for x in range(width):
    for y in range(height):

        #get the position in the two dimensional array
        try:
            pos = (x, y)
            rgba = image.getpixel(pos)

            message = (pos, rgba)
            if y % 2 == 0:
                msg = dumps(message)
                sockfd.sendto(msg, ("127.0.0.1", 9916))
            #time.sleep(0.001)
        except:
            print(1)
message = "done"
msg = dumps(message)
sockfd.sendto(msg, ("127.0.0.1", 9916))

def send_missing_pixel():
    while True:
        msg, cli_addr = sockfd.recvfrom(1024)
        pos = loads(msg)
        if pos == "done":
            break
        rgba = image.getpixel(pos)
        data = (pos, rgba)
        data = dumps(data)
        sockfd.sendto(data, cli_addr)
        print("eggs")


send_missing_pixel()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The first image sent&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3LgWQLDm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g7hvtd1h5s1fp64y91k4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3LgWQLDm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g7hvtd1h5s1fp64y91k4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Code to receive the image and ask for missing pixels
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from socket import *
from pickle import *
from PIL import Image 
import time



#viewer = ImageViewer()
sockfd = socket(AF_INET, SOCK_DGRAM)
sockfd.bind(("127.0.0.1", 9916))

height = 170
width = 272
isize = (height, width)
#image = open("image2.png", "x")
image4 = Image.new("RGBA", size=(width, height))
pixels = image4.load()
#the following variables for storing positions of missing pixels
error_pos = []
pos1 = None
x = []
y = []

def get_pixel_data():
    j = 0
    x1 = []
    e_count = 0
    #erro counter
    error_count = 0
    while True:
        j = j + 1
        msg, cli_addr = sockfd.recvfrom(1024)
        #decoding 
        message = loads(msg)
        if message == "done":
            image4.save("image3.png")
            break
        pos = message[0]
        rgba = message[1]
        #print(pos)
        #setting pixels into image file
        pixels[pos[0], pos[1]] = rgba
        #stroing postitions to check if there is a missing pixel
        xy = (pos[0], pos[1]) 
        #print(xy)
        x1.append(xy)
        try:
            s = j - 1
            #print(x1[_s])
            if len(x1) &amp;gt; 1:
                #checking difference betwwen previous coordinates and present coordinates 
                xnums = x1[s][0] - x1[s-1][0]
                ynums = x1[s][1] - x1[s-1][1]
                #this will check if the difference Y returns a negative
                #that means a new X value and recalculate the Y value
                if ynums &amp;lt; 0:
                    ynums = height - x1[s-1][1] + x1[s][1]
                #iterate through the difference and create a list of missing pixel positions
                if ynums &amp;gt; 1:
                    for i in range(1, ynums):
                        if xnums &amp;gt; 1:

                            un = x1[s][0] + i
                            x.append(un)                        
                        if xnums &amp;lt;= 1:
                            e = x1[s][0]
                            x.append(e)
                        un = x1[s][1] + i
                        y.append(un)
                        _c = (x[e_count], y[e_count])
                        error_pos.append(_c)
                        e_count = e_count + 1
        except:
            pass

    q = len(error_pos)
    j1 = width * height
    #print(q , e_count)
    if e_count == q:
        #print(123)
        get_missing_data(cli_addr)


def get_missing_data(cli_addr):
    _x = len(error_pos)
image5 = Image.open("image3.png")
    for i in range(_x):
        e_pos = error_pos[i]
        msg = dumps(e_pos)
        sockfd.sendto(msg, cli_addr)
        #time.sleep(0.001)
        msg, cli_addr = sockfd.recvfrom(1024)
        #decoding 
        message = loads(msg)
        pos = message[0]
        rgba = message[1]
        pixels = image5.load()
        pixels[pos[0], pos[1]] = rgba
        print(pixels[pos[0], pos[1]])
    image5.save("image3.png")
    x = "done"
    msg = dumps(x)
    sockfd.sendto(msg, cli_addr)

get_pixel_data()



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Halfway into correcting image&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QRIU9w4O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lnh6038xyik4e8dtjhz6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QRIU9w4O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lnh6038xyik4e8dtjhz6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;complete Image&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zDDGNanK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/092vj7nd2fp6ptbmdrw2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zDDGNanK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/092vj7nd2fp6ptbmdrw2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Photo by Umberto on Unsplash
&lt;/h5&gt;

&lt;p&gt;leave a comment, suggestion or contact me &lt;a href=""&gt;@black_strok3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>network</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Transfering Bitmap Images over a network</title>
      <dc:creator>Zephaniah Joshua</dc:creator>
      <pubDate>Tue, 05 Jan 2021 12:46:38 +0000</pubDate>
      <link>https://dev.to/black_strok3/transfering-bitmap-images-over-a-network-5f4e</link>
      <guid>https://dev.to/black_strok3/transfering-bitmap-images-over-a-network-5f4e</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LhI5qKj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p8fds3zul61lt0ha6cr5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LhI5qKj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p8fds3zul61lt0ha6cr5.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Photo by Sora Shimazaki from Pexels
&lt;/h6&gt;

&lt;h3&gt;
  
  
  An image is made up of pixels(tiny dots of colours). Images can be stored digitally as bitmaps.
&lt;/h3&gt;

&lt;p&gt;A bitmap (Bit-Map) is a mapping of a domain to bits. In our case, the domain is an array of pixels, here we map a single bit to a pixel.&lt;br&gt;
Pix-map is a map of pixels which maps multiple bits to a pixel.&lt;/p&gt;

&lt;p&gt;Bitmaps use the 24bit or 32bit colour depth and use the RGB(255, 255, 255) colour system each of the value in the RGB colour system is stored as a byte(8 bits) an extra 8bits might be used to show transparency of the image&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R =8bits
G=8bits
B=8bits #24bit
A=8bits #32bit #extra transparency bit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Storing images in bitmap format will make them very large so images are mostly stored in compressed file format&lt;br&gt;
.PNG:: the lossless compression. It compresses data by using pattern tin pixel colour to reduce the amount of data stored. &lt;a href="https://en.wikipedia.org/wiki/Portable_Network_Graphics#Compression"&gt;readmore&lt;/a&gt;.&lt;br&gt;
.jpg:: the lossy compression. It compresses data by getting rid o colours that the human eyes will not pick. &lt;a href="https://en.wikipedia.org/wiki/JPEG#JPEG_compression"&gt;read more&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now we are going to write a program to send an image over a network. So we are going to send an image over a network using &lt;strong&gt;UDP&lt;/strong&gt; and we will also use &lt;strong&gt;pickle&lt;/strong&gt; for serialisation&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fist of we create a socket &lt;code&gt;sockfd = socket(AF_INET, SOCK_DGRAM)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open the image to be sent &lt;code&gt;image = Image.open("image.png")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Get size of the image i.e width and height&lt;code&gt;width, height = image.size&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Iterate through the whole image (the reason why we have two loops because the image is in a two-dimensional array form)&lt;/li&gt;
&lt;li&gt;Get RGB value of every pixel in the image&lt;code&gt;rgba = image.getpixel(pos)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Change it to a transferable form using “dump” from the pixel module &lt;code&gt;msg = dumps(message)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sending it &lt;code&gt;sockfd.sendto(msg, ("127.0.0.1", 9999))&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PcJ1oojd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cuo99rkfm44tbiltyic5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PcJ1oojd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cuo99rkfm44tbiltyic5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  image we are sending
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from socket import *
from pickle import *
from PIL import Image
import time

#create a socket

sockfd = socket(AF_INET, SOCK_DGRAM)

#open image
image = Image.open("100kb.png")

#get image size
width, height = image.size
print(image.size)

for x in range(width):
    for y in range(height):

        #get the position in the two dimensional array
        try:
            pos = (x, y)
            rgba = image.getpixel(pos)

            message = (pos, rgba)
            msg = dumps(message)
            sockfd.sendto(msg, ("127.0.0.1", 9999))
            time.sleep(0.001)
        except:
            print(1)#just a check



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now to receive the image…&lt;/li&gt;
&lt;li&gt;Create a socket &lt;/li&gt;
&lt;li&gt;Bind socket&lt;/li&gt;
&lt;li&gt;Create an image file to save data sent &lt;code&gt;image4 = Image.new("RGBA", size=(width, height))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a function to receive data and write it into the image file&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Here I already knew the width and height but we can also send that over the network before we start sending the pixel data&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from socket import *
from pickle import *
from PIL import Image 



#viewer = ImageViewer()
sockfd = socket(AF_INET, SOCK_DGRAM)
sockfd.bind(("127.0.0.1", 9999))

height = 170
width = 272
isize = (height, width)
#image = open("image2.png", "x")
image4 = Image.new("RGBA", size=(width, height))
pixels = image4.load()

def get_pixel_data():
    while True:
        msg, cli_addr = sockfd.recvfrom(1024)
        #decoding 
        message = loads(msg)
        pos = message[0]
        rgba = message[1]
        print(pos)
        pixels[pos[0], pos[1]] = rgba
        #print(pixels[pos[0], pos[1]])
        if pos[0] + 1 == width and pos[1] + 1 == height:    
            image4.save("image4.png")
            break
#calling my function
get_pixel_data()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  image we received
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_7gGi08i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d93ngu0mobqhcoq64whq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_7gGi08i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d93ngu0mobqhcoq64whq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was fun to do hope you learnt from my write up, please leave a comment or contact me &lt;a href="https://twitter.com/black_strok3"&gt;@black_stroke&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Serialisation</title>
      <dc:creator>Zephaniah Joshua</dc:creator>
      <pubDate>Mon, 04 Jan 2021 08:58:59 +0000</pubDate>
      <link>https://dev.to/black_strok3/serialisation-1bo2</link>
      <guid>https://dev.to/black_strok3/serialisation-1bo2</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---OdqSSou--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ckve7tcybz5vq3mic7mr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---OdqSSou--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ckve7tcybz5vq3mic7mr.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Previously we talked about  &lt;strong&gt;parsing&lt;/strong&gt; data so it could be sent over a network and we used an example code in python. &lt;strong&gt;parsing&lt;/strong&gt; is a simple form of serialisation.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Serialisation&lt;/strong&gt; is converting data into a stream of bytes so that it can be stored or transmitted.&lt;/p&gt;

&lt;h4&gt;
  
  
  Remember our example information that was to be sent over the internet that has two data types an integer and a string
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data1 = “hello” #string
data2 = 123  #interger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use &lt;strong&gt;pickle&lt;/strong&gt; a serialisation module in python(FYI: marshal, JSON are other python serialisation modules ) to convert this data to bytes and send them over the network.&lt;br&gt;
NOTE: any data type can be serialised with &lt;strong&gt;pickle&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pickle  

data1 = “hello” #string
data2 = 123  #interger

#creating our message as a tuple
message = (data1, data2)

#converting data to a stream of bytes with pickle
msg = pickle.dumps(message)
print(msg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this message is received it needs to be  deserialised&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pickle 

#deserialising
message = pickle.loads(msg)


data1 = message[0]
data2 = message[1]

print(data1)
print(data2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Warning as stated in the &lt;a href="https://docs.python.org/3/library/pickle.html"&gt;pickle&lt;/a&gt; documentation deserialisation(unpickling) can be exploited by crafting data to cause arbitrary code execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hope this gives you a basic understanding on &lt;strong&gt;serialisation&lt;/strong&gt; and how to use the &lt;strong&gt;pickle&lt;/strong&gt; python module to do it. Please do leave suggestions or questions in the comment box or contact me &lt;a href="https://twitter.com/black_strok3"&gt;@black_strok3&lt;/a&gt;
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Photo by Neo from Pexels
&lt;/h6&gt;

</description>
    </item>
    <item>
      <title>Parsing Data</title>
      <dc:creator>Zephaniah Joshua</dc:creator>
      <pubDate>Sun, 03 Jan 2021 13:48:45 +0000</pubDate>
      <link>https://dev.to/black_strok3/parsing-data-1l80</link>
      <guid>https://dev.to/black_strok3/parsing-data-1l80</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JoDBsI8b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u7h174tkygwpwp4f71uo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JoDBsI8b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u7h174tkygwpwp4f71uo.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Yesterday I wrote about the difference between UDP and TCP. Today we will be talking about sending information that contains different data types over a network by parsing.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  A string of data can easily be sent over a network because it can easily be encoded as bytes but the same cannot be said for a large amount of data that contains different data types e.g strings, integers, float, tuples, dictionaries etc.
&lt;/h4&gt;

&lt;p&gt;For example, if we have data that contains a string, date, a tuple as information we need to send over a network we might need to encompass all this data into a single message convert to bytes and send over the network. On receiving the data the server will separate the data back into its individual components and decode it.&lt;br&gt;
The process of separating data back to its components is called &lt;strong&gt;parsing&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First step is converting other data types to string e.g 
Converting an integer to a string &lt;code&gt;str(123)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Encompassing converted data into a single variable with a separator like “,” to separate the different data.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data1 = “hello”
data2 = 123

#convert the integer to string
data2 = str(data2)
#encompassing data into single variable
message = data1 + “,” data2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Encode the message &lt;code&gt;message.encode()&lt;/code&gt; and send &lt;code&gt;socket.send(msg)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The server has received the message and will now parse it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#recieve and decode message
message = socket.recv(1024)
msg = message.decode()

#split the message using the split function in python
#remember that we used “, ” to separate data
parts = msg.split(“,”)
#data1 is a string
data1 = str(parts[0])
#data2 is an int
data2 = int(parts[1])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  So basically &lt;strong&gt;parsing&lt;/strong&gt; is changing all data to be sent into a uniform data type(in our case a string) sending it as a single message and on reception separating it back to its components and returning them to there original data types.
&lt;/h4&gt;

&lt;p&gt;Hopes this gives you a basic understanding of parsing. Feel free to ask questions or leave suggestions in the comment section or contact me &lt;a href="https://twitter.com/black_strok3"&gt;@black_strok3&lt;/a&gt;.&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;a href="https://www.pexels.com/@jorge-jesus-137537?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels"&gt;image&lt;/a&gt;
&lt;/h6&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>networking</category>
    </item>
    <item>
      <title>Difference between UDP and TCP (example code)</title>
      <dc:creator>Zephaniah Joshua</dc:creator>
      <pubDate>Sat, 02 Jan 2021 16:50:30 +0000</pubDate>
      <link>https://dev.to/black_strok3/difference-between-udp-and-tcp-example-code-1pg1</link>
      <guid>https://dev.to/black_strok3/difference-between-udp-and-tcp-example-code-1pg1</guid>
      <description>&lt;h4&gt;
  
  
  Hi everybody this is my first post. I started the 100DaysofCodechallenge so I decided to write about what I learn each day.
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Today I learnt about the difference between UDP and TCP and used it in the client/server model python code.
&lt;/h4&gt;

&lt;h2&gt;
  
  
  UDP(User datagram protocol):
&lt;/h2&gt;

&lt;h4&gt;
  
  
  In simple terms &lt;strong&gt;UDP&lt;/strong&gt; is a very fast and not-so-reliable communications protocol and does not need to make connections before sending data.
&lt;/h4&gt;

&lt;h2&gt;
  
  
  TCP(Transfer control protocol):
&lt;/h2&gt;

&lt;h4&gt;
  
  
  On the other hand &lt;strong&gt;TCP&lt;/strong&gt; makes connections before data is sent, is a reliable transmissions protocol, not as fast &lt;strong&gt;UDP&lt;/strong&gt;.
&lt;/h4&gt;

&lt;h3&gt;
  
  
  I will use a simple python server code to show the differences
&lt;/h3&gt;

&lt;h3&gt;
  
  
  -A simple server using &lt;strong&gt;TCP&lt;/strong&gt; connection
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from socket import * 

#create socket
sockfd = socket(AF_INET, SOCK_STREAM)
#bind socket
sockfd.bind(("127.0.0.1", 9999))
#listen for connections
sockfd.listen()

#accept connection
newsockfd, address = sockfd.accept() 

#recieve message
response = newsockfd.recv(1024)

#send message
message="this message is for you"
newsockfd.send(msg)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  -A simple server using &lt;strong&gt;UDP&lt;/strong&gt; connection
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#create socket
sockfd = socket(AF_INET, SOCK_DGRAM)

#bind socket
sockfd.bind(("127.0.0.1", 9999))

#recv message
data, client_addr = sockfd.recvfrom(1024)

#send message
message = "yo, yo"
msg = message.encode()
sockfd.sendto(msg, client_addr)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  We can clearly see that the &lt;strong&gt;UDP&lt;/strong&gt; code is shorter than the &lt;strong&gt;TCP&lt;/strong&gt;.This is because:
&lt;/h4&gt;

&lt;p&gt;1.TCP connection listen &lt;code&gt;sockfd.listen()&lt;/code&gt;for connections on it socket while UDP just doesn't do that(if data is sent and server is not up it is simply lost)&lt;/p&gt;

&lt;p&gt;2.TCP accepts connection&lt;code&gt;newsockfd, address = sockfd.accept()&lt;/code&gt;from the client before exchanging information while UDP just accepts information and stores &lt;code&gt;data, client_addr = sockfd.recvfrom(1024)&lt;/code&gt;the IP address from which it received information&lt;/p&gt;

&lt;p&gt;3.The receiving function. TCP uses &lt;code&gt;recv(1024)&lt;/code&gt; to recv data "1024" specify the amount of data&lt;br&gt;
while UDP uses &lt;code&gt;recvfrom(1024)&lt;/code&gt;basically because the address from which the data was received also needs to be store&lt;/p&gt;

&lt;p&gt;4.The send function. TCP uses the &lt;code&gt;send()&lt;/code&gt; because it does not need to specify the address to which the data needs to be sent&lt;br&gt;
because a connection has already been established between client and server&lt;br&gt;
while the UDP uses &lt;code&gt;sendto()&lt;/code&gt;which needs to sppecify the address to which data will be sent.&lt;/p&gt;

&lt;p&gt;FTP, SSH, email use TCP.&lt;br&gt;
Streaming media, online gaming use UDP.&lt;/p&gt;

&lt;p&gt;Hope this little information gives you a basic understanding of &lt;strong&gt;TCP&lt;/strong&gt; and &lt;strong&gt;UDP&lt;/strong&gt; feel free to live your thoughts in the comments or contact me at &lt;a href="https://twitter.com/black_strok3"&gt;black_strok3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>networking</category>
    </item>
  </channel>
</rss>
