Last active
July 23, 2024 17:12
-
-
Save scimad/ae0196afc0bade2ae39d604225084507 to your computer and use it in GitHub Desktop.
Basic python HTTP server and manipulation of POST data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Not eligible</title> | |
</head> | |
<body> | |
<h1>Oops, you are too young to have your own conscience. Try again next year.</h1> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Eligible</title> | |
</head> | |
<body> | |
<h1>Good news, You are eligible to vote.</h1> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Using Python's SimpleHTTPServer Module</title> | |
</head> | |
<body> | |
<form method = 'POST' action="verify"> | |
<label for="fname">First name:</label><br> | |
<input type="text" id="fname" name="fname" placeholder="Enter your name"><br> | |
<label for="age">Last name:</label><br> | |
<input type="text" id="age" name="age" placeholder="Enter your age here"><br><br> | |
<input type="submit" value="Submit"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.server | |
import socketserver | |
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
print ("MY SERVER: I got a GET request.") | |
if self.path == '/': | |
print ("MY SERVER: The GET request is for the root URL.") | |
self.path = 'my_webpage.html' | |
return http.server.SimpleHTTPRequestHandler.do_GET(self) | |
def do_POST(self): | |
print ("MY SERVER: I got a POST request.") | |
if self.path == '/verify': | |
print ("MY SERVER: The POST request is for the /verify URL.") | |
content_length = int(self.headers['Content-Length']) | |
post_data_bytes = self.rfile.read(content_length) | |
print ("MY SERVER: The post data I received from the request has following data:\n", post_data_bytes) | |
post_data_str = post_data_bytes.decode("UTF-8") | |
list_of_post_data = post_data_str.split('&') | |
post_data_dict = {} | |
for item in list_of_post_data: | |
variable, value = item.split('=') | |
post_data_dict[variable] = value | |
print ("MY SERVER: I have changed the post data to a dict and here it is:\n", post_data_dict) | |
age = int(post_data_dict['age']) | |
if age > 18: | |
self.path = 'age_okay.html' | |
else: | |
self.path = 'age_not_okay.html' | |
return http.server.SimpleHTTPRequestHandler.do_GET(self) | |
# Create an object of the above class | |
handler_object = MyHttpRequestHandler | |
PORT = 8000 | |
my_server = socketserver.TCPServer(("", PORT), handler_object) | |
# Start the server | |
my_server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example. Here some suggestions: