Python Script Continually Listen on Tcp Port

We already know about two main transport layer protocols like TCP and UDP. For more information about TCP and UDP you can check reference section.  In this article we will learn how to send and receive UDP packets using python program.

Expectations:

Here are some key points can be learned from this article

  1. Sending some text using python program which uses UDP protocol.
  2. Receiving some text using python program which uses UDP protocol.
  3. Check UDP packet in Wireshark.
  4. Learn about python code to send and receive UDP packets.

General Set Up Diagram:

System A and B should able to ping each other.

Assumptions or Limitations:

  1. Both systems should be having Linux with Ubuntu. The code may or may not work on other operating system like Windows10, MAC etc.
  2. Both systems should have python version 3. This code may or may not work on python 2.7 version.

Note: You can check reference for trying out Send and Receive UDP packets via Linux CLI before going for python files to do the same task.

Python files:

There are two python files server.py and client.py. server file and client file should be present in Server system and Client system respectively.

Server.py

import socket
import sys

if len ( sys.argv ) == 3:
# Get "IP address of Server" and also the "port number" from
argument 1 and argument 2
ip = sys.argv [ 1 ]
port = int ( sys.argv [ 2 ] )
else:
print ( "Run like : python3 server.py <arg1:server ip:this system IP 192.168.1.6> <arg2:server port:4444 >" )
exit( 1 )

# Create a UDP socket
s = socket.socket ( socket.AF_INET , socket.SOCK_DGRAM )
# Bind the socket to the port
server_address = (ip, port)
s.bind (server_address)
print ( "Do Ctrl+c to exit the program !!" )

while True:
print ( "####### Server is listening #######" )
data, address = s.recvfrom ( 4096 )
print ( "\n \n 2. Server received: " , data.decode ( 'utf-8' ) , "\n \n" )
send_data = input ( "Type some text to send => " )
s.sendto (send_data.encode ( 'utf-8' ) , address)
print ( "\n \n 1. Server sent : " , send_data, "\n \n" )

Client.py

import socket
import sys

if len ( sys.argv ) == 3:
# Get "IP address of Server" and also the "port number" from argument 1 and argument 2
ip = sys.argv [ 1 ]
port = int ( sys.argv [ 2 ] )
else:
print ( "Run like : python3 client.py <arg1 server ip 192.168.1.102> <arg2 server port 4444 >" )
exit( 1 )

# Create socket for server
s = socket.socket ( socket.AF_INET , socket.SOCK_DGRAM , 0 )
print ( "Do Ctrl+c to exit the program !!" )

# Let's send data through UDP protocol
while True:
send_data = input ( "Type some text to send =>" ) ;
s.sendto (send_data.encode ( 'utf-8' ) , (ip, port) )
print ( "\n \n 1. Client Sent : " , send_data, "\n \n" )
data, address = s.recvfrom ( 4096 )
print ( "\n \n 2. Client received : " , data.decode ( 'utf-8' ) , "\n \n" )
# close the socket
s.close ( )

Send/Receive UDP packet:

Let's take an example like we will send UDP packet from System A to System B. So, in server-client concept, we have to run server at System B side and client at System A side.

Also we have valid IP addresses.

System A IP: 192.168.1.6
System B IP: 192.168.1.102

Now unlike Send and Receive UDP packets via Linux CLI we will run server.py in System, B [192.168.1.102] and then we will run client.py in System A [192.168.1.6].

How to run server.py in 192.168.1.102?

Here is the command to run server.py

$python3 server.py 192.168.1.102 4444

Here is the screenshot

Here there are two arguments for the python program. 1st argument is IP address of server itself , here its 192.168.1.102 and 2nd argument is port which server will be listening, here we have chosen 4444.

How to run client.py in 192.168.1.6?

Here is the command to run client.py

$python3 client.py 192.168.1.102 4444

Here is the screenshot

Here there are two arguments for the python program. 1st argument is IP address of server , here its 192.168.1.102 and 2nd argument is port where server is running. For our example it's 4444.

Send or receive some text:

Now as you can see we are ready to communicate between two systems. But we need to start from client first. Let's type something in client and see if it reaches to server or not.

Send Data from client: "I am from Client"

Screenshot form client:

Now this client message should come to server. Here is the server screenshot.

Now we can see in server side also we have option to send something to client. Let's try that.

Send Data from client: "I am from Server"

Server screenshot:

And here is the screenshot on client side.

Now this will go on infinite times until we stop the python program using Ctrl+c.

Check UDP packet in Wireshark:

Now we have done some communication but how do we come to know that UDP was used to send or receive those packets. So, we can check capture in Wireshark.

Let's see the packet when client [192.168.1.6] sent data ["I am from Client"] to server [192.168.1.6].

Code explanation:

For basic python code explanation refer "Python Socket File Transfer Send" in reference section.

We will only explain important lines codes for Client and Server python file. There are useful comments inside the client and server code.

Client code explanation:

The above line is required to check whether user has passed required mandatory arguments. Or else program will exit. Same check is there in server program.

s = socket.socket ( socket.AF_INET , socket.SOCK_DGRAM , 0 )

The above line is to create socket server with UDP [SOCK_DGRAM] protocol. Same code is there in server.py.

To run program in infinite loop until user does Ctrl+c. Same code is there in server.py.

s.sendto (send_data.encode ( 'utf-8' ) , (ip, port) )

To send data for mentioned ip and port number.

data, address = s.recvfrom ( 4096 )

To receive any data coming from server. Same code is there in server.py.

Server code explanation:

s.sendto (send_data.encode ( 'utf-8' ) , address)

Send data to client address.

Conclusion:

We can send or receive UDP data using python program. Internally it uses server client mechanism.

References:

To understand TCP: https://linuxhint.com/tcp_packet_capture_analysis/
To understand UDP: https://linuxhint.com/udp_wireshark_analysis/
Send and Receive UDP packets via Linux CLI:
https://linuxhint.com/send_receive_udp_packets_linux_cli/
Python Socket File Transfer Send :
https://linuxhint.com/python_socket_file_transfer_send/

About the author

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com

florescoulin.blogspot.com

Source: https://linuxhint.com/send_receive_udp_python/

0 Response to "Python Script Continually Listen on Tcp Port"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel