
Friday, September 6, 2019
The present world is entirely taken off by the Internet, we so call the driving force of the modern era. Networking has emerged right from wired connections to wireless transmission using protocols. Sockets brought communication in between connections. All the network protocols that are present use the oldest BSD sockets a low-level networking interface.
The socket module in Python provides an interface in accessing this low-level interface by sending and receiving the data over common networking protocols. Socket automatically selects address formats when its object is created. Here are some of the address formats :
- AF_UNIX - Unix Socket (Here a string or byte-like objects can be used as parameters )
- AF_INET - IPV4 (host, ports as parameters)
- AF_INET6 - IPV6 (host, port, flowinfo, scopeid as parameters)
- AF_BLUETOOTH - Bluetooth protocol
- AF_PACKET - Playing with raw packets in Linux
A raw packet is different from the other packets such as stream sockets and datagram sockets (These recieve data from the transport layer), and these do not require port and address.
Let's understand the methods that sockets use to communicate.
socket.accept()
⇰Accepts a connection
socket.bind()
⇰Binds to an address
socket.listen()
⇰Enable a server to accept connections
socket
.
connect()
⇰connects to an address
socket.close()
⇰closes a connection
socket.timeout()
⇰returns timeout in float
These are base system calls that are supported by most operating systems like Unix.
Socketserver:
Sockets provide an interface to BSD sockets, But you should have to code from the base. If you are too lazy like me then you can use socketserver. socketserver is a consistent API that provides interface directly to these system calls using python classes.
Now let's be dirty with some code. Fire up your python environments now!
🛡These codes may ask for admin privileges to run as it is interacting with base API.
This is a simple implementation of sockets
Here we initiated the IPV4 protocol (AF_INET) binding to localhost with port 5000 by listening to a maximum of 5 connections and also receiving a buffer size of 68.
Let's see on client-side to
TCP & UDP Protocols:
TCP (Transmission Control Protocol) :
Most of the services like word wide web, email and remote administration uses this TCP protocol. It provides a host to host connection at the transport layer of the internet protocol. The above program is also an example of a TCP transmission protocol.
Now let's write a Network sniffer which sniffs raw network packets
gethostbyname() - Translate a hostname to IPV4 address format
setsockopt() - Include IP headers
ioctl() - RCVALL_OFF used to disable promiscuous mode.
setsockopt() - Include IP headers
ioctl() - RCVALL_OFF used to disable promiscuous mode.
UDP (User Datagram Protocol) :
Generally, UDP is a connectionless protocol. This means that peers sending messages doesn't need a connection to be established.
As always I look for easy things, this time we go with socketserver library
Then its time to receive the time that we sent from the UDP server.
from socket import socket, AF_INET, SOCK_DGRAM
sock = socket(AF_INET, SOCK_DGRAM)
sock.recvfrom(8192)
(b'Fri Sep 6 21:37:32 2019', ('127.0.0.1', 5000))
In my next post, We shall see advanced concepts like encrypting networks, SSH using paramiko, and a lot more, subscribe to receive notifications into your inbox.
Also read :
📢Serving files using Http server in Python
Also read :
📢Serving files using Http server in Python
Friday, September 6, 2019
Computer
operating systems
python
great
ReplyDelete