
Practice Top 30 Python Programming Questions on Networking
Q1. Write a Python program to create a TCP server that listens on port 12345 and prints any message it receives.
Input (from a client):
Hello, Server!
Expected Output (on the server):
Message from client: Hello, Server!
Q2. Write a Python program to create a TCP client that connects to a server at localhost on port 12345 and sends the message "Hello, Server!".
Expected Output (on the server):
Message from client: Hello, Server!
Q3. Write a Python program to create a TCP server that echoes back any message it receives from a client.
Input (from the client):
Echo this!
Expected Output (on the client):
Server echoed: Echo this!
Q4. Write a UDP server and client. The client sends a message, and the server responds with "Acknowledged".
Client Input:
Hello, UDP Server!
Expected Output (on the client):
Server response: Acknowledged
Q5. Write a Python program where a client sends a list of numbers to the server, and the server returns their sum.
Input (from the client):
[1, 2, 3, 4, 5]
Expected Output (on the client):
Sum from server: 15
Q6. Write a Python program where a client uploads a text file to a server. The server saves the file locally.
Client Input:
test.txt (contains Hello, this is a test.)
Expected Output (on the server):
A file named received_test.txt with the content:
Hello, this is a test.
Q7. Write a Python program for a chat application using sockets. The server and client can send messages alternately.
Client Input:
Hello!
Expected Output (on the server):
Client: Hello!
Q8. Write a TCP server that can handle multiple clients. Each client sends its name, and the server responds with "Hello, [Name]!".
Client Input:
Alice
Expected Output (on the client):
Server: Hello, Alice!
Q9. Write a program to demonstrate socket timeout. If the client doesn’t send a message within 5 seconds, the server should print "Timeout occurred".
Expected Output (on the server):
Timeout occurred
Q10. Write a program to check if a specific website (www.google.com) is reachable.
Expected Output:
Website is reachable.
Q11. Write a Python program to fetch the HTML content of http://example.com using the requests module.
Expected Output (partial):
...
Q12. Write a Python program to make a POST request to http://httpbin.org/post with data {"name": "John"}.
Expected Output:
json
{
"json": {"name": "John"}
}
Q13. Write a Python program to fetch a webpage and print "Error occurred" if the status code is not 200.
URL: http://nonexistentwebsite.com
Expected Output:
Error occurred
Q14. Write a Python program to fetch a JSON response from https://jsonplaceholder.typicode.com/posts/1.
Expected Output:
json
{
"userId": 1,
"id": 1,
"title": "...",
"body": "..."
}
Q15. Write a Python program to download an image from https://via.placeholder.com/150 and save it as image.jpg.
Expected Output:
An image named image.jpg in the current directory.
Q16. Write a Python program to make a GET request with custom headers to http://httpbin.org/headers.
Expected Output:
The response JSON includes your custom headers.
Q17. Write a Python program to measure the response time of http://example.com.
Expected Output:
Response time: 0.25 seconds
Q18. Write a Python program to fetch JSON data from https://jsonplaceholder.typicode.com/users and print the names of all users.
Expected Output:
Leanne Graham
Ervin Howell
Q19. Write a Python program to print the status code of http://example.com.
Expected Output:
Status Code: 200
Q20. Write a Python program to send a GET request with basic authentication to http://httpbin.org/basic-auth/user/passwd.
Expected Output:
Authenticated
Q21. Write a Python program to start an HTTP server on port 8080 that serves the current directory.
Expected Output:
Serving HTTP on 0.0.0.0 port 8080 ...
Q22. Write a Python program to create a simple web server that serves a custom HTML page.
Expected Output (on visiting in a browser):
Test
Welcome to My Server
Q23. Modify the web server to log all incoming requests.
Expected Output (in console):
GET /index.html HTTP/1.1
Q24. Write a web server that returns a JSON response for any GET request.
Expected Output (on visiting in a browser):
json
{
"message": "Hello, world!"
}
Q25. Write a web server that extracts and prints query parameters from the URL.
URL Input:
http://localhost:8080?name=John&age=30
Expected Output:
Name: John, Age: 30
Q26. Write a Python program to serve static files like images or CSS from a folder.
Expected Output:
Files in the folder are accessible via the browser.
Q27. Write a web server that accepts POST requests and prints the data sent by the client.
Client Input:
{"name": "Alice"}
Expected Output (on the server):
Received: {"name": "Alice"}
Q28. Write a Python program to create a web server that redirects /old to /new.
Expected Output (in the browser):
Navigating to /old redirects to /new.
Q29. Write a web server that returns a 404 Not Found for invalid paths.
Expected Output (in the browser):
404 Not Found
Q30. Write a Python program to stop the web server when the user presses a specific key (e.g., Ctrl+C).
Expected Output:
Server stopped.