As recommended in the http.server (http.server — HTTP servers — Python 3.9.5 documentation) module documentation, it informs that it is not advisable to use it in the production environment. What would be the ideal python module to work with the protocol and http requests? Or would you have to write one from scratch ?
You could take a look at the recommendations in this article: Exploring HTTPS With Python – Real Python
In my case I’m developing a server for python, as well as apache is for php
Thank you for the recommended article because it clarified how the communication procedures between the client and server are. And it made me feel safe by encouraging me to continue my research on how to structure a web server for python. In the project I’m dedicating myself I’ve managed to implement a socket using the module (socketserver) which has the role of establishing the connection (client x server), so that I can handle application layer protocols (http, https and others). Now my doubt would really be related to the http protocol that the server will receive, how can I access this protocol, how to get access to the protocol variables (http) to be able to manipulate ?
In the project repository, where I exemplified, I demonstrate the response of the http protocol, but using the module ( http.server ), not recommended.
I’m not sure if I understand you correctly, but as far as I can see, your questions will be answered by reading the whole RealPython article I linked to. Note that they refer to several other useful articles regarding HTTP and socket programming in the introduction. I think a lot of your questions will be answered by following those links and reading them carefully.
Good luck, and happy hacking
Thank you for the RealPython suggestion, and now I need to go deeper and this requires studies. But the goal is to be able to manipulate the http protocol as long as there is already a connection established between the client and server and this I do using the python socketserver module as shown in the example below, also see the result of the processing that requires an http response:
# Module Name : socketserver -> https://docs.python.org/3/library/socketserver.html#module-socketserver from socketserver import BaseRequestHandler, ThreadingTCPServer # Transport Layer Protocol : TCP # This is the superclass of all request handler objects. It defines the interface, given below. class HTTPTCPIPv4(BaseRequestHandler): def handle(self): # self.request is the TCP socket connected to the client self.data = self.request.recv(1024).decode() print(self.data) if __name__ == "__main__": HOST, PORT = "", 8080 # Create the asynchronous server, binding to localhost on port 8080 with ThreadingTCPServer((HOST, PORT), HTTPTCPIPv4) as server: print("Server : Action v0.0.1, running address http://127.0.0.1:8080,") print("cancel program with Ctrl-C") server.serve_forever()
GET / HTTP/1.1 Host: 127.0.0.1:8080 Connection: keep-alive sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91" sec-ch-ua-mobile: ?0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 Purpose: prefetch Sec-Fetch-Site: none Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Accept-Encoding: gzip, deflate, br Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
What I need is to access the request method, the path, and host as below to be able to manipulate.
GET / HTTP/1.1 Host: 127.0.0.1:8080
Just change your
handle method to do that instead of
I could exemplify, as I’ve made several changes to handle and I was unsuccessful.
The problem is that the data received is practically all unorganized and due to the fact it is complicated to capture certain data. For that it was necessary to use a native python function that organizes the data in an array ( split() ), so that later it can have access to the http headers of the request.