A simple Python REST API server with a thread-safe in-memory user store.
This project implements a basic REST HTTP API server in Python using only the standard library. It provides a thread-safe in-memory store for user data, allowing clients to create, read, update, and delete user information via JSON over HTTP.
The server supports standard HTTP methods (GET, POST, PUT, PATCH, DELETE) to perform CRUD operations on user resources. The data is stored in memory with thread safety ensured by locking, making it safe for use with concurrent requests within a single process.
-Python 3.6+
No external packages are needed — everything uses the Python standard library.
- Clone the repository:
git clone https://github.com/daheng414514/thread-safe-user-store.gitOnce you clone the repository and activate your Python environment:
python server.pyThen you should see:
Server running on http://127.0.0.1:8000This means your REST API server is now running and ready to accept requests
You can now use tools like curl, Postman, or a browser to interact with the API.
Example:
curl http://127.0.0.1:8000/users- The server will run on localhost (127.0.0.1)
- It listens on port 8000 by default
- You can open the URL in a browser or send HTTP requests from the terminal
server.py— HTTP server and request handlersfunctions.py— Thread-safe in-memory user storetest.py— Unit tests for the store logic
The following endpoints are available in this REST API. All requests and responses use JSON.
Retrieve a list of all user names.
Request:
curl http://127.0.0.1:8000/usersRetrieve info for a specific user.
Request example:
curl "http://127.0.0.1:8000/users/<name>"Create a new user by sending JSON in the request body.
Request example:
curl -X POST "http://127.0.0.1:8000/users" \
-H "Content-Type: application/json" \
-d '{"name":"charlie","age":29,"email":"charlie@example.com"}'Replace all information for an existing user.
Request example:
curl -X PUT "http://127.0.0.1:8000/users/<name>" \
-H "Content-Type: application/json" \
-d '{"age":29,"email":"charlie@example.com"}'Update part of an existing user's information by sending JSON in the request body.
Request example:
curl -X PATCH "http://127.0.0.1:8000/users/<name>" \
-H "Content-Type: application/json" \
-d '{"age":29,"email":"charlie@example.com"}'Delete an existing user by specifying the user name in the URL.
Request example:
curl -X DELETE "http://127.0.0.1:8000/users/<name>"- The server will run on localhost (
127.0.0.1) - It listens on port
8000by default - You can interact with it using a browser,
curl, or Postman
- Data is stored in memory and will reset when the server restarts
- Designed for single-process concurrency
- No authentication or persistence layer
- Intended for learning and demonstration purposes
The API can be tested using command-line tools such as curl or API clients like Postman.
Example commands:
# Get all user in user database
curl http://127.0.0.1:8000/users
# Create a new user
curl -X POST http://127.0.0.1:8000/users \
-H "Content-Type: application/json" \
-d '{"name":"alice","age":30}'
# Update a user
curl -X PATCH http://127.0.0.1:8000/users/alice \
-H "Content-Type: application/json" \
-d '{"age":31}'
# Delete a user
curl -X DELETE http://127.0.0.1:8000/users/alice