0% found this document useful (0 votes)
9 views

Experiment 2 Networking HTTP API (2)

Uploaded by

skandapmwork2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Experiment 2 Networking HTTP API (2)

Uploaded by

skandapmwork2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Week 2 Experiment HTTP Server Implementation Experiment

Objective

To understand and compare two different HTTP server implementations in Python:


1. Basic HTTP server using Python's built-in libraries
2. Flask application server for REST API endpoints

Prerequisites
- Python 3.x installed
- Flask framework (`pip install flask`)
- Web browser or cURL for testing

Experiment 1: Basic HTTP Server

Setup and Implementation


1. Create a new directory for the experiment:

mkdir http_server_experiment
cd http_server_experiment

2. Save the basic HTTP server code as `http1.py`:

from http.server import SimpleHTTPRequestHandler, HTTPServer

host = 'localhost'
port = 8000

server = HTTPServer((host, port), SimpleHTTPRequestHandler)


print(f"Starting server on {host}:{port}")
server.serve_forever()

3. Run the server:

python http1.py

4. Test the server:


- Open a web browser and navigate to `http://localhost:8000`
- Or use curl: `curl http://localhost:8000`

Expected Output for Experiment 1


Your screenshot should show:
1. Terminal window showing the server running with the message:

Starting server on localhost:8000

2. Browser window or curl output showing:


- Directory listing of your current folder (if empty, it will show "Directory listing for /")
- HTTP response status code 200 OK in browser developer tools or curl response

Experiment 2: Flask REST API Server

Setup and Implementation


1. Install Flask:

pip install flask

2. Save the Flask application code as `app.py`:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/get')
def get_method():
return jsonify(message="GET Request Success"), 200

@app.route('/post', methods=['POST'])
def post_method():
return jsonify(message="POST Request Success"), 201

@app.route('/notfound')
def not_found():
return jsonify(error="Resource Not Found"), 404

if __name__ == '__main__':
app.run(debug=True)

3. Run the Flask server:

python app.py
4. Test the Flask endpoints using these exact commands:

Test GET endpoint


curl http://localhost:5000/get

Test POST endpoint


curl -X POST http://localhost:5000/post

Test 404 endpoint


curl http://localhost:5000/notfound

Expected Output for Experiment 2


Your screenshot should show:
1. Terminal window showing Flask server running with output similar to:

* Serving Flask app 'app'


* Debug mode: on
* Running on http://127.0.0.1:5000

2. Terminal window showing curl command outputs:

GET endpoint response


{"message":"GET Request Success"}

POST endpoint response


{"message":"POST Request Success"}

404 endpoint response


{"error":"Resource Not Found"}

Experiment Submission Instructions

Required Screenshots
1. For Basic HTTP Server (Experiment 1):
- Screenshot 1: Terminal showing server running
- Screenshot 2: Browser or curl output showing directory listing
- Name these files as: `exp1_server.png` and `exp1_output.png`

2. For Flask REST API (Experiment 2):


- Screenshot 1: Terminal showing Flask server running
- Screenshot 2: Terminal showing all three curl command outputs (GET, POST, and notfound
endpoints)
- Name these files as: `exp2_server.png` and `exp2_output.png`

Screenshot Requirements
1. Each screenshot must:
- Show the full terminal window or browser window
- Include timestamp (system clock) visible in the screenshot
- Be clearly readable with good resolution
- Show the command being executed and its output

2. For terminal screenshots:


- Ensure the command prompt is visible
- Show the complete output without truncation
- Use dark theme terminal for better readability

Submission Format
1. Create a ZIP file named `HTTP_Server_Experiment_<YourName>` containing:
- All four screenshots named as specified above
- Source code files (`http1.py` and `app.py`)
- A brief text file named `readme.txt` containing:
- Your name and date of experiment
- Python version used
- Flask version used
- Any issues encountered and their resolution

Validation Checklist
Before submitting, verify that:
- Both servers run without errors
- All endpoints return expected responses
- Screenshots show complete output
- Source code files are included
- readme.txt is complete
- All files are named correctly

Note: Partial or incorrectly formatted submissions will not be accepted. Ensure all outputs match
exactly with the expected outputs shown above.

Common Issues and Solutions


1. Port already in use:
- Error: "Address already in use"
- Solution: Kill the existing process or use a different port

2. Flask not installed:


- Error: "ModuleNotFoundError: No module named 'flask'"
- Solution: Run `pip install flask`

3. Connection refused:
- Error: "Connection refused"
- Solution: Verify the server is running and port number is correct

Remember: If you encounter any issues not listed here, document them and their solutions in
your readme.txt file.

You might also like