Sunday, April 20, 2025

Sockets with Python - 1 - Creating Simple Server

import socket 

msg = "Meeting at 14h, Office 279"

#Local IP Data

host = "192.168.0.103"
port = 54321
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("Preparing for Connection")
s.bind((host, port))

print("Server Listening")
s.listen()

conn, addr = s.accept()
print("Connection From: ", addr)
print("Sending Message: ")
conn.sendall(msg.encode())

This script sets up a TCP server that listens on a specific IP address and port for incoming connections. Once a client establishes a connection, it sends a message to the client.

Here is a step-by-step explanation of the code:

  1. import socket: This line imports the socket module which provides a way to communicate over the internet using TCP/IP.

  2. msg = "Meeting at 14h, Office 279": This line sets the message that will be sent to the client when the connection is established.

  3. host = "192.168.0.103": This line sets the IP address of the server.

  4. port = 54321: This line sets the port number that the server will listen on.

  5. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM): This line creates a new socket object using the AF_INET address family (IPv4) and the SOCK_STREAM socket type (TCP).

  6. print("Preparing for Connection"): This line prints a message to the console to indicate that the server is preparing to start.

  7. s.bind((host, port)): This line binds the socket to the specified IP address and port number.

  8. print("Server Listening"): This line prints a message to the console to indicate that the server is now listening for incoming connections.

  9. s.listen(): This line sets the server to listen for incoming connections. The listen() method takes one argument which specifies the maximum number of queued connections (backlog) that the server will allow.

  10. conn, addr = s.accept(): This line waits for a client to connect to the server and accepts the connection. The accept() method returns a tuple containing a new socket object (conn) which can be used to send and receive data on the connection, and the address of the client (addr) which consists of the client's IP address and port number.

  11. print("Connection From: ", addr): This line prints the address of the client that has connected to the server.

  12. print("Sending Message: "): This line prints a message to the console to indicate that the server is about to send the message to the client.

  13. conn.sendall(msg.encode()): This line sends the message to the client using the sendall() method of the socket object (conn). The encode() method is used to convert the message string into bytes, which is required when sending data over the network.

Overall, this script sets up a simple TCP server that listens for incoming connections and sends a message to the client once the connection is established.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...