Sending TCP Data to a Python App Hosted on Azure VM: A Complete Tutorial
Muhammad Zubair
AI Engineer | AI Agent Developer | Transforming Ideas into AI SaaS | LLMs | Fine-Tuning | RAG | AI Mind Mapping | Cloud-Based Services | Generative AI | Prompt Engineering | AI Ethics | MLOps | Multi AI Agents
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
- Go to the Azure Portal and sign in with your credentials.
Create a New VM
- Navigate to the "Virtual Machines" section.
- Click on "Add" to create a new VM.
- Follow the prompts to configure your VM settings, including the operating system (OS), size, region, and other options.
Review and Create
- After configuring the settings, review your choices.
- 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
- Once your VM is deployed, go to the "Virtual Machines" section.
- Click on the name of your VM to open its overview page.
- Here you can see the status, public IP address, and other details of your VM.
Networking Configuration
Step 3: Configure Network Settings
Network Settings
- On the left side of the VM overview page, click on "Networking" under the "Settings" section.
- This section allows you to manage inbound and outbound rules for your VM's network security group (NSG).
Add an Inbound Port Rule
- Click on "Add inbound port rule" to create a new rule for allowing traffic to your VM.
- 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
- 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
- Open your terminal (on Windows, Mac, or Linux).
- 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
- Use nano tcplistener.py to create a new Python file.
- 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
- Add your message in ASCII format in Packet Sender.
- Enter your VM's public IP address (find it in the VM overview on Azure Portal).
- Enter "5544" as the port.
- Select "TCP" as the protocol.
- 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
Content Creator | Video Editor | Social Media Manger | Cinematographer
8 个月Good to know!