This program is a registered user list microservice that allows for adding, listing, retrieving, and deleting users. It uses a file-based communication.
- The microservice must be running in a separate process for this communication to work.
- Make sure the microservice (
user_management.py) is started before attempting to send commands. - The microservice operates using a file-based communication pattern where commands are written to a
commands.txtfile and responses are read from aresults.txtfile.
To request data from the microservice:
- Write your command to the
commands.txtfile - Each command should be written as a single line of text
- Wait for the response (1-2 seconds is usually sufficient)
- Read the response from the
results.txtfile
| Command | Format | Description |
|---|---|---|
| add | add <username> <password> |
Create a new user |
| list | list |
List all users |
| get | get <username> |
Get details for a specific user |
| delete | delete <username> |
Delete a specific user |
| purge | purge |
Delete all users |
def send_command(command):
"""
Send a command to the user management microservice.
Args:
command (str): The command to send (e.g., "add user1 pass123")
Returns:
bool: True if the command was sent successfully
"""
try:
# Write the command to the commands.txt file
with open("commands.txt", 'w') as f: # Use 'w' mode to ensure clean command
f.write(command + "\n")
return True
except Exception as e:
print(f"Error sending command: {e}")
return False
# Example usage:
# To add a user
send_command("add testuser password123")
# To list all users
send_command("list")
# To get a specific user
send_command("get testuser")
# To delete a user
send_command("delete testuser")After sending a command, the microservice will process it and write the result to the results.txt file. To receive the response:
- Wait a short time for processing (1-2 seconds)
- Read the contents of the
results.txtfile - Parse the relevant information from the response
Responses are timestamped entries in the following format:
[YYYY-MM-DD HH:MM:SS] Response message
For user details and lists, the format is more structured:
===== USER DETAILS =====
ID: <uuid>
Username: <username>
Password: <password>
Created At: <timestamp>
=======================
or
===== USER LIST =====
ID Username Password Created At
----------------------------------------------------------------------------------------------------------
<uuid> <username> <password> <timestamp>
<uuid> <username> <password> <timestamp>
----------------------------------------------------------------------------------------------------------
Total users: <count>
====================
import time
def receive_response():
"""
Receive and parse a response from the user management microservice.
Returns:
str: The response message
"""
time.sleep(1.5) # Adjust wait time as needed
try:
with open("results.txt", 'r') as f:
content = f.read()
# Parse the response
sections = content.split("[")
if len(sections) > 1:
# Get the latest response
latest_response = "[" + sections[-1]
# Special handling for structured data
if "USER DETAILS" in latest_response:
details_index = latest_response.find("===== USER DETAILS =====")
return latest_response[details_index:]
elif "USER LIST" in latest_response:
list_index = latest_response.find("===== USER LIST =====")
return latest_response[list_index:]
else:
return latest_response.strip()
else:
return "No response found"
except Exception as e:
return f"Error reading response: {e}"
# Example Command
send_command("get testuser")
response = receive_response()
print(response)