Sending TCP Data to a Python App Hosted on Azure VM: A Complete Tutorial

Sending TCP Data to a Python App Hosted on Azure VM: A Complete Tutorial

Creating and managing a virtual machine (VM) on the Microsoft Azure Portal is a fundamental skill for cloud professionals. In this guide, we will walk you through the process of creating a VM, configuring network settings, setting up inbound rules, running a simple TCP server in Python, and sending TCP messages or packets to the server.

VM Setup

Step 1: Create a VM on Azure Portal

Sign in to Azure Portal

Create a New VM

  1. Navigate to the "Virtual Machines" section.
  2. Click on "Add" to create a new VM.
  3. Follow the prompts to configure your VM settings, including the operating system (OS), size, region, and other options.

Review and Create

  1. After configuring the settings, review your choices.
  2. Click "Create" to deploy the VM. This process might take a few minutes as Azure sets up your VM.

Step 2: Access the VM

Open the VM

  1. Once your VM is deployed, go to the "Virtual Machines" section.
  2. Click on the name of your VM to open its overview page.
  3. Here you can see the status, public IP address, and other details of your VM.

Networking Configuration

Step 3: Configure Network Settings

Network Settings

  1. On the left side of the VM overview page, click on "Networking" under the "Settings" section.
  2. This section allows you to manage inbound and outbound rules for your VM's network security group (NSG).



Add an Inbound Port Rule

  1. Click on "Add inbound port rule" to create a new rule for allowing traffic to your VM.
  2. This is essential for enabling communication with your VM over the network.

Step-by-Step Configuration of Inbound Rule

Step 4: Source

  • Set to "Any" to allow traffic from any source.

Step 5: Source Port Ranges

  • Set to "*" to accept traffic from any port on the source machine.

Step 6: Destination

  • Set to "Any" to allow traffic to any destination within the network.

Step 7: Service

  • Set to "Custom" to specify custom port ranges and protocols.

Step 8: Destination Port Ranges

  • Enter "5544" to allow traffic to port 5544.

Step 9: Protocol

  • Select "TCP" to apply the rule to TCP traffic.

Step 10: Action

  • Choose "Allow" to permit the traffic.

Step 11: Priority

  • Set to "350". Lower numbers have higher priority, so this rule will be considered before higher-numbered rules.

Step 12: Name

  • Enter a name for the rule, such as "TCP-Inbound".

Step 13: Add the Rule

  • Click the "Add" button to apply the new rule to the network security group (NSG). This will allow traffic on port 5544 to reach your VM

  • .


Connecting to the VM

Step 14: Connect to the VM

Connect to VM

  1. On the left side, click "Connect" and select the method you prefer (e.g., SSH).

Step 15: Select Native SSH

  • Choose "Native SSH" for connecting to your VM using a terminal.

Step 16: Execute SSH Command

  • Copy the SSH command provided by Azure.


Step 17: Paste in Terminal

  1. Open your terminal (on Windows, Mac, or Linux).
  2. Paste the SSH command and press Enter.

Step 18: Enter Password

  • Enter the password for your VM when prompted. This authenticates your connection to the VM.


Setting Up the Python TCP Server

Step 19: Create a Python Application to Listen on TCP

Check Python and Nano Installation

  • Ensure Python and Nano are installed on your VM. Use python --version and nano --version to check their availability.

Create Python File

  1. Use nano tcplistener.py to create a new Python file.
  2. Paste the following code into the file:

import socket

def start_tcp_server(host='0.0.0.0', port=5544):
    
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        # Bind the socket to the specified host and port
        s.bind((host, port))
        # Listen for incoming connections
        s.listen()
        print(f"Listening on {host}:{port}")

        while True:
            # Accept a new connection
            conn, addr = s.accept()
            with conn:
                # Print the client's address
                print(f"Connected by {addr}")
                # Receive data from the client
                data = conn.recv(1024)
                # If data is received, print it
                if data:
                    print(f"Received data: {data.decode('utf-8')}")

if __name__ == "__main__":
    start_tcp_server()        

Step 20: Run the Python File

Run the File

  • Execute the Python file using python3 tcplistener.py or python tcplistener.py.

Step 21: Confirm Listening

  • Verify that the script is listening on the specified port (5544). You should see a message indicating the server is listening.


Testing with Packet Sender

Step 22: Test Using Packet Sender

Install Packet Sender

  • Download and install Packet Sender from Packet Sender's website.

Configure Packet Sender

  1. Add your message in ASCII format in Packet Sender.
  2. Enter your VM's public IP address (find it in the VM overview on Azure Portal).
  3. Enter "5544" as the port.
  4. Select "TCP" as the protocol.
  5. Click on "Send" to send the packet or message to your VM.

Verify Packet Reception

Step 23: Enter Port as Your App Running in VM

  • Ensure the port is set to "5544" in Packet Sender.

Step 24: Select Protocol as TCP

  • Confirm that the protocol selected is TCP.

Step 25: Click on Send to Send Packet or Message

  • Click the "Send" button to transmit your packet or message.

Step 26: Look at Packet Sender Logs for Errors

  • Check the logs in Packet Sender to ensure there are no errors.

Step 27: Verify Packet Reception on VM Terminal

  • Look at the terminal on your VM. You should see the packets or messages being displayed, confirming successful communication.


Best Practices

Secure Your VM

  • After testing, ensure you secure your VM by restricting inbound traffic to trusted IPs and using strong authentication methods.

Monitor Network Traffic

  • Use Azure Network Watcher to monitor and analyze network traffic to and from your VM for better security and performance.

Keep Software Updated

  • Regularly update your VM's operating system and installed software to protect against vulnerabilities.

By following these steps and best practices, you will be able to create and manage a virtual machine on Microsoft Azure , configure network settings, set up a TCP server using Python, and test communication using Packet Sender

Umair Majeed

Content Creator | Video Editor | Social Media Manger | Cinematographer

8 个月

Good to know!

赞
回复

要查看或添加评论,请登录

Muhammad Zubair的更多文章

  • Intoduction to Azure AI Foundry

    Intoduction to Azure AI Foundry

    Microsoft Azure AI Foundry is a unified platform that simplifies the creation, customization, and management of AI…

  • The Impact of Meta's LCMs on Natural Language Processing

    The Impact of Meta's LCMs on Natural Language Processing

    Meta's Large Concept Models (LCMs) represent a significant advancement in artificial intelligence (AI), particularly in…

  • How Memory Works for Multi-User, Multi-Request Applications.

    How Memory Works for Multi-User, Multi-Request Applications.

    When multiple users interact with an application at the same time, each of their requests is handled by different…

  • Foundation Models in Artificial Intelligence

    Foundation Models in Artificial Intelligence

    Artificial Intelligence (AI) is growing very fast, thanks to strong foundation models. These models are trained on huge…

  • How Large Language Models (LLMs) Work?

    How Large Language Models (LLMs) Work?

    When I first started exploring artificial intelligence four years ago, I found it hard to understand how Large Language…

    2 条评论
  • RAG vs. Long-Context Language Models (LCLMs)

    RAG vs. Long-Context Language Models (LCLMs)

    RAG vs. Long-Context Language Models (LCLMs) As we explore new possibilities with Large Language Models (LLMs), the…

  • What is Prompt Engineering?

    What is Prompt Engineering?

    Introduction to Prompt Engineering Prompt engineering is a way to communicate with AI models, like ChatGPT, by asking…

    6 条评论
  • Why Use Containers for Azure AI Services?

    Why Use Containers for Azure AI Services?

    Containers provide an efficient, flexible way to bring Microsoft Azure AI services closer to where our data resides…

    1 条评论
  • Common Challenges in Azure Functions: Solutions for CORS, Temporary Storage, and Local Development Issues.

    Common Challenges in Azure Functions: Solutions for CORS, Temporary Storage, and Local Development Issues.

    Azure Functions offer a flexible and scalable way for us to run event-driven code in the cloud. However, there are…

  • Azure AI Services: Build Smarter Apps and Services

    Azure AI Services: Build Smarter Apps and Services

    Microsoft Azure AI Services offers a list of tools that enable developers to incorporate artificial intelligence into…

社区洞察

其他会员也浏览了