Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a folder and handle POST requests at the same time in python http.server

I am running an https server which serves the current directory using the following code.

#!/usr/bin/env python3

from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl

httpd = HTTPServer(('hostname', 8443), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile="/Users/test/Desktop/key.pem", certfile="/Users/test/Desktop/cert.pem", server_side=True)
httpd.serve_forever()

I also wanted the server to be able to handle a POST request with some values. I want to log whatever is being sent as POST to a file. Previously I used cgi scripts for this purpose. I used to have a logger.py in ./cgi-bin and made the clients send POST request to http://hostname:8080/cgi-bin/logger.py. and invoked the server using python -m http.server --cgi 8080

However because of my new requirement to serve over https I am no longer invoking http.server from commandline and am using the above script. I have no idea POST requests can be handled without breaking the usual functionality of serving the contents inside the folder.

like image 844
hax Avatar asked May 19 '26 02:05

hax


1 Answers

With a bit more of googling, I resolved the issue with the code below.

#!/usr/bin/env python3

from http.server import HTTPServer, SimpleHTTPRequestHandler
from io import BytesIO
from sys import argv
import ssl


class requestHandler(SimpleHTTPRequestHandler):
    def do_POST(self):

        #Doing things with incoming POST data
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        body = self.rfile.read(content_length) # <--- Gets the data itself
        bodydata = body.decode('utf-8')
        logdata = "\n"+bodydata+"\n"
        logfile = open('log.txt', 'a+')
        logfile.write(logdata)
        logfile.close()

        # Crafting the response to be sent back to client

        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b'\nA POST request with some data was recieved\n')
        response.write(b'The recieved data shall be logged\n')
        self.wfile.write(response.getvalue())

portstr = argv[1]
port = int(portstr)
keyloc = "/Users/test/Desktop/key.pem"
certloc = "/Users/test/Desktop/cert.pem"

httpd = HTTPServer(('localhost', port), requestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=keyloc, certfile=certloc, server_side=True)
httpd.serve_forever()
like image 140
hax Avatar answered May 20 '26 16:05

hax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!