Skip to content

Latest commit

 

History

History
122 lines (82 loc) · 3 KB

File metadata and controls

122 lines (82 loc) · 3 KB

Certainly! Below is a README write-up for your repository based on the provided information:


Python Socket Communication Example

This repository contains a simple example of client-server communication using Python sockets. The communication is established over a local network using the TCP protocol.

Files

client.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1234))

full_msg = ''
while True:
    msg = s.recv(8)
    if len(msg) <= 0:
        break
    full_msg += msg.decode("utf-8")

print(full_msg)

The client.py file represents the client-side of the communication. It connects to the server and receives a message.

server.py

import socket

def sayHey():
    hey_msg = 'Bingo!!!'
    return hey_msg

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))
s.listen(5)

client_msg = sayHey()

while True:
    clientsocket, address = s.accept()
    print(f"Connection from {address} has been established.")
    clientsocket.send(bytes(client_msg, "utf-8"))
    clientsocket.close()

The server.py file represents the server-side of the communication. It binds to a port, listens for incoming connections, and sends a predefined message to connected clients.

VSC Devcontainer Deployment

The repository includes a configuration for Visual Studio Code (VSC) Devcontainer.

.devcontainer/devcontainer.json

{
  "name": "Red Hat Linux with Python 3",
  "dockerFile": "Dockerfile",
  "extensions": [
    "ms-python.python"
  ],
  "settings": {
    "python.pythonPath": "/usr/bin/python3"
  }
}

This configuration specifies a development container using Red Hat Universal Base Image (UBI) 8 with Python 3. It includes the Python extension for Visual Studio Code.

Dockerfile

# Use the official Red Hat UBI as the base image
FROM registry.access.redhat.com/ubi8/ubi

# Set metadata
LABEL maintainer="Your Name <your.email@example.com>"

# Install necessary packages
RUN yum install -y python3 python3-pip

# Set Python 3 as the default python interpreter
RUN alternatives --set python /usr/bin/python3

# Upgrade pip and install required Python packages
RUN pip3 install --upgrade pip
RUN pip3 install \
    python-socketio

# Set the default working directory
WORKDIR /workspace

# Specify entry point
CMD ["bash"]

The Dockerfile sets up the development environment by installing Python 3 and required packages.

Usage

  1. Clone the repository to your local machine:

    git clone https://github.com/your-username/python-socket-example.git
  2. Open the repository in Visual Studio Code.

  3. Visual Studio Code will detect the Devcontainer configuration and prompt you to reopen the project in the Dev Container.

  4. Build and run the server using the provided Python scripts.

  5. Run the client script to establish a connection and receive the predefined message.

Feel free to modify and extend this example for your specific use case!