http.server - 不支持的方法('POST')

So I have created a website, when the user tries to fill out the login form (username and password), the following error message is displayed:

Error response
Error code: 501

Message: Unsupported method ('POST').

Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.

For the server im using Python 3 http.server. To start the server i have a batch file containing the following code:

python -m http.server 80

I'm looking for a way to receive the login credentials the user submitted, and store them in a text file (.txt).

After doing some research I got the impression I need to create a PHP script, i have no idea how to do this. here is my attempt at it:

<?php
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);

echo $username, ' ', $password;
?>

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501

It seems like you don't have POST support on your webserver. Try using GET instead.

<?php
$username = htmlspecialchars($_GET['username']);
$password = htmlspecialchars($_GET['password']);

echo $username, ' ', $password;
?>

Although the code you wrote will simply print the user details, not store them. I would however recommend enabling POST and using it instead.

Edit: As pointed out by another user GET is not secure and should not be used for passwords. This method will work but I would recommended not using it and instead looking for a way to implement POST into your webserver.

I would also not recommend storing the passwords in a text file. PHP comes with built in methods for encrypting passwords. I would recommend reading about how to implement hashing such as Argon2:

https://wiki.php.net/rfc/argon2_password_hash

Thank you @MonkeyZeus, I managed to solve the problem by making a few changes to your code.

import socketserver
import http.server
import logging
import cgi

PORT = 80

class ServerHandler(http.server.SimpleHTTPRequestHandler):

    def do_GET(self):
        logging.error(self.headers)
        http.server.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        logging.error(self.headers)
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE':self.headers['Content-Type'],
                     })
        for item in form.list:
            logging.error(item)
        http.server.SimpleHTTPRequestHandler.do_GET(self)

        with open("data.txt", "w") as file:
            for key in form.keys(): 
                file.write(str(form.getvalue(str(key))) + ",")

Handler = ServerHandler

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

When the post request is received, a text file called "data" is created. A for loop is then used to iterate through the keys (source) and write there values to the file.