Skip to content
Menu
e-lo [IT Engineer life]
  • Home
    • Note
  • Database
    • T-SQL
    • SQL Server quick
    • SQL server docs
    • MySql quick sheet
    • Postgre
    • InfluxDB
  • Programming
    • MS Azure Powershell
    • MS Azure Command-Line Interface (CLI) doc
    • Python Docs
    • Python Logging
    • Python-cheat-sheet
    • Git-guide
  • Azure
    • MS Windows virtual machines in Azure
    • MS ARM Docs
    • MS ARM Template Docs
    • MS ARM Functions
    • MS Bicep+ARM
    • MS ARM Tutorial
    • MS Deployment scripts (intern/extern)
    • MS Virtual Network
  • Az-nutshell
    • ms-technology-choices-compute-decision-tree
    • ms-data-store-decision-tree
    • ms-data-explorer
    • ms-storage-explorer
    • ms-azure-sql
    • ms-common-data-services
    • ms-azure-mysql-daas
    • ms-sla
    • az paas
    • az glossary-quicksheet
    • az-test-vm-script-quickguide
  • Linux
    • Top CMD’s
    • Useful CMD Linux
    • ss64 Linux
    • Ubuntu
    • 30 things Ubuntu 18.04
    • Bootable Ubuntu USB
    • LinuxFilesystemTreeOverview
  • Sys Admin
    • System Administrator
    • Sys News
  • Zen
    • Not thinking about anything is Zen
e-lo [IT Engineer life]

Python network client and server using socket

Posted on November 14, 2018November 14, 2018 by espenk

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.

 

RSS Azure

  • Scale your cloud-native apps and accelerate app modernization with Azure, the best cloud for your apps May 24, 2022

RSS RabbitMQ

  • RabbitMQ 3.8.15 release

RSS Python

  • PEP 691: JSON-based Simple API for Python Package Indexes May 4, 2022

Tags

5 min (26) Ansible (1) ARM (10) azure (40) cmd (3) Django (4) Docker (1) e-lo (2) Flask (2) Github (9) Grafana (2) Information (7) Information Retrieval (13) JAVA (1) kivy (2) Kotlin (6) linux (15) mobile (2) Natural Language Prossesing (NLP) (2) Net.Core (1) Networking and Security (6) OPC (2) PEP8 (1) Philosophy (3) Python (47) Python Networking and Security (5) Reason (2) RMQ (3) Solr (11) Sql (10) Uncategorized (2) VSC (1) Warframe (2) WMVARE (4) Zabbix (7)

Recent Posts

  • 5 min Logic App Storage Table
  • 5 min Logic App PSQL
  • 5 min Logic App
  • 5 MIN Azure Data Explorer
  • TODO Build a Hash Table in Python With TDD Real Python

Archives

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Photo by Markus Spiske from Pexels "Matrix"

©2022 e-lo [IT Engineer life] | Powered by WordPress & Superb Themes