Skip to content

Instantly share code, notes, and snippets.

@scimad
Last active June 24, 2024 14:59
Show Gist options
  • Save scimad/ae0196afc0bade2ae39d604225084507 to your computer and use it in GitHub Desktop.
Save scimad/ae0196afc0bade2ae39d604225084507 to your computer and use it in GitHub Desktop.
Basic python HTTP server and manipulation of POST data
<!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>
<!DOCTYPE html>
<html>
<head>
<title>Eligible</title>
</head>
<body>
<h1>Good news, You are eligible to vote.</h1>
</body>
</html>
<!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>
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()
@isidroas
Copy link

Thanks for the example. Here some suggestions:

  • fix(html): field name
diff --git a/my_webpage.html b/my_webpage.html
index 09b2869..c32ca94 100644
--- a/my_webpage.html
+++ b/my_webpage.html
@@ -7,9 +7,9 @@
 	<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>
+		<label for="age">Age:</label><br>
 		<input type="text" id="age" name="age" placeholder="Enter your age here"><br><br>
 		<input type="submit" value="Submit">
 	</form>
 </body>
-</html>
\ No newline at end of file
+</html>
  • use urllib.parse.parse_qs
diff --git a/server.py b/server.py
index 1909b9e..c26e69c 100644
--- a/server.py
+++ b/server.py
@@ -1,6 +1,8 @@
 import http.server
 import socketserver
 
+from urllib.parse import parse_qs
+
 class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
     def do_GET(self):
         print ("MY SERVER: I got a GET request.")
@@ -19,16 +21,11 @@ class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
             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 =parse_qs(post_data_str)
             
-            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'])
+
+            age = int(post_data_dict['age'][0])
             if age > 18:
                 self.path = 'age_okay.html'
             else:
  • context mananger like official documentation
diff --git a/server.py b/server.py
index c26e69c..772323c 100644
--- a/server.py
+++ b/server.py
@@ -38,7 +38,5 @@ class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
 handler_object = MyHttpRequestHandler
 
 PORT = 8000
-my_server = socketserver.TCPServer(("", PORT), handler_object)
-
-# Start the server
-my_server.serve_forever()
+with socketserver.TCPServer(("", PORT), handler_object) as httpd:
+    httpd.serve_forever()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment