I would like to implement a web page in Python using ONLY the std library.
I have done it in PHP but I want to do it in Python and it is very difficult to me to understand the documentation.
This is what I done in PHP:
Access http://localhost:8888/project/src/car.php.
The user has to complete a form where it specifies the brand of the car.
Then the site returns the JSON list of cars in http://localhost:8888/project/src/car.php?brand=toyota
It works very well.
It was very easy in PHP, but I could not find a way to do that in Python using only standard library.
Do you have any suggestion ?
This is what I have so far:
server.py:
import http.server
import socketserver
if __name__ == '__main__':
PORT = 8888
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
index.html:
<!-- used to make a form for the user -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Autocompletion</title>
</head>
<body>
<h2>Manufacturer finder</h2>
<p>
Please enter your research
</p>
<form action="car.py" method="get" class="form-example">
<div class="form-example">
<label for="brand">Enter your query: </label>
<input type="text" name="brand" id="brand" required>
</div>
<div class="form-example">
<input type="submit" value="Submit">
</div>
</form>
</body>
</html>
The user land on index.html that has a form to be completed. When a user complete the form, this open the file : http://localhost:8888/car.py?brand=toyota
The script works in http://localhost:8888/ and not http://localhost:8888/car.py (as does my PHP script) but it is not a problem.
After the form is filled, I don't know how to retrieve the variable manufacturer and display the list of cars from the brand. Inside car.py I have a list of cars for each brand I just need so I just need to fetch the correct cars from the brand and print it as a JSON only without additional HTML.
The problem I have: Completing the form just print the entire car.py file.
It is not a problem to have a form not secure (where you can have the results by appending ?brand=toyota to the url).
This is how I start the server:
python server.py
Do you have any suggestions ?
Thank you for you attention