October 31, 2019 Python Networking: Sockets

“We live in a world that has walls and those walls need to be guarded by men with guns.”
– Aaron Sorkin

This article will introduce the basics of Python networking programming. The reader will learn how to implement advanced Python networking related scenarios and use cases. 

Cybersecurity plays an important role in organizations when a large amount of data is stored after collection and/or processed during transactions or analytics computation. The information processed from the data can be very sensitive and related to intellectual property, financial assets, personal preferences, historical behavior and performance of resources. The sensitive data can be communicated across networks and to other business organizations. Cybersecurity helps to protect the sensitive data communication and secure the data.

Cybersecurity consists of different elements such as network, application, endpoint, data, identity, database, infrastructure, cloud, mobile security, disaster recovery, business continuity planning and end-user security. Network infrastructure security is a subset of cyber security. Network security is related to network protection, data transport devices such as switches and routers, and data protection between computing nodes.

Network protocol is related to the group of rules associated with communication between servers in a network. This includes connection mechanisms and data packaging formats to send and receive messages. The networking protocols which are used are TCP/IP or others associated at a higher or lower level. The other protocols are SSL, TLS, IPSec, S-HTTP, PGP, S/MIME, DNDSEC and SSH.

The code samples below show advanced Python networking implementation. In this implementation, we look at the basic samples where Python is used for security.

Prerequisites:

  1. You need to set up Python3.5 to run the code samples below. You can download from this link.

Problem

socket module has the features to use related to socket creation and binding sockets to an address. The features related to server implementation and client connection are shown in the example below. 

The code snippet below shows the socket server and client implementation:

import socket              
 
sock = socket.socket()        
host = socket.gethostname()
print("host name",host)
 
port = 32567                
sock.bind(('', port))        
 
sock.listen(5)                 
while True:
   conn, address = sock.accept()     
   print('connected to', address)
   str = 'Thank you for connecting'
   byts = str.encode()
   conn.send(byts)
   conn.close()

Socket module has socket function to create a socket. Socket created has methods bind, listen and accept.  Server socket binds to the port 32567 when the bind method is invoked. When the socket receives the message, the response is converted from string to bytes. Connection is created by invoking the accept method of socket. The method send is used for sending bytes to the client socket. The connection is closed by invoking close method.

import socket               
 
sock = socket.socket()        
host = socket.gethostname() 
print(“host”,host)
port = 32567               
 
sock.connect(('127.0.0.1', port))
print(sock.recv(1024))
sock.close()

Client socket is created by invoking the method socket method on socket. The connect method on the socket is used to connect to the server running on the localhost (ip address: 127.0.0.1). The response coming from the server is printed and socket is closed by invoking close method.

Instructions for Running the Code

#running the socket server and client python code
python3 socket_server.py
 
python3 socket_client.py

Output


bhagvanarch

Guest Blogger


categories & Tags


UNLEASH THE GIG ECONOMY. START A PROJECT OR TALK TO SALES
Close

Sign up for the Topcoder Monthly Customer Newsletter

Thank you

Your information has been successfully received

You will be redirected in 10 seconds