Python is a very popular programming language that is used in many fields, including server programming. Writing an HTTP server via Python is very simple and can be implemented in a few lines of code. In this article, we will introduce how to write an HTTP server using Python.
First of all, we need to install Python. If you have not installed Python, you can go to the Python official website to download it. After the installation is complete, we can use Python's own module to implement the HTTP server.
In Python, we need to import the socket and http.server modules to implement the HTTP server. The socket module provides many low-level network programming interfaces, and the http.server module provides us with a ready-made HTTP server implementation.
import socket import http.server
Creating an HTTP server is very simple, we can use the http.server.HTTPServer class to create one. The HTTPServer class accepts two parameters: the server address and the handler class to handle the request. We can inherit http.server.BaseHTTPRequestHandler to implement our own request handler.
class WebRequestHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() message = "Hello, world!" self.wfile.write(bytes(message, "utf8")) httpd = http.server.HTTPServer(('localhost', 8000), WebRequestHandler)
In the above code, we created a class named WebRequestHandler
, which inherits from http.server.BaseHTTPRequestHandler
. In the do_GET
method, we send an HTTP response in response to the GET request. We then created an HTTPServer instance and set the address to localhost:8000
and the request handler to the WebRequestHandler
class.
After creating the HTTP server, we need to start it and let it start listening for requests from clients. We can use the serve_forever() method of the HTTPServer class to achieve this.
print('Starting server, use <Ctrl-C> to stop') httpd.serve_forever()
In the above code, we use the print
function to output a message to tell the user that the server has started. Then we call the serve_forever
method to keep the HTTP server running until the Ctrl-C
signal is received to stop.
Now that we have implemented a simple HTTP server, we can test this server using a browser or command line tool. Enter http://localhost:8000
in your browser's address bar. If everything goes well, you should see a web page containing the Hello, world!
message. If you don't use a browser, you can also use curl to test this server:
$ curl http://localhost:8000 Hello, world!
Conclusion
In this article, we introduced how to use Python's built-in modules to create an HTTP server. We create a WebRequestHandler
class to handle client requests, and then use the HTTPServer class to create an HTTP server and start it. Finally, we tested the server via a browser or command line tool. This is just a simple example of HTTP server implementation, you can implement more complex functions according to your own needs.
The above is the detailed content of Quick Start with Python Server Programming: Easily Implement an HTTP Server. For more information, please follow other related articles on the PHP Chinese website!