Python http.server模块学习1_code

本文介绍了Python3的http.server模块,包括HTTPServer和BaseHTTPRequestHandler的使用。讲解了如何创建一个简单的HTTP服务器,处理GET请求,以及如何通过类属性和方法定制服务器行为。示例代码展示了服务端返回'Hello, World!'的实现。最后提到了更简便的服务器创建方式。" 110180711,10326040,Python matplotlib绘制多个子图详解,"['Python绘图', 'matplotlib库', '数据可视化']
摘要由CSDN通过智能技术生成

http.server

注:代码为python3代码

One class, HTTPServer, is a socketserver.TCPServer subclass. It creates and listens at the HTTP socket, dispatching the requests to a handler.

HTTPServer,父类是socketserver.TCPServer,用于创建HTTP Socket服务器端,并监听HTTP socket连接,调度请求给handler处理。

引用一段来自父类socketserver的说明,这能为我们如何使用http.server或者使用其他派生的模块,如SimpleXMLRPCServer提供指导:
Creating a server requires several steps. First, you must create a request handler class by subclassing the BaseRequestHandler class and overriding its handle() method; this method will process incoming requests. Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class. It is recommended to use the server in a with statement. Then call the handle_request() or serve_forever() method of the server object to process one or many requests. Finally, call server_close() to close the socket (unless you used a with statement).
译文:创建一个服务端需要如下几个步骤:
1.创建一个继承自BaseRequestHandle的request handler类,并重写它的handler方法,这个方法将处理收到的请求
2.实例化一个server类,类的初始化参数为server’s address and the request handler class
3.推荐使用with上下文的方式
4.调用handle_request()方法或serve_forever()方法运行服务端程序
5.调用server_close()函数结束,如果是with语句这不需要

class http.server.HTTPServer(server_address, RequestHandlerClass)

类对象初始化参数 server_address = (server_ip,server_port)

class http.server.BaseHTTPRequestHandler(request, client_address, server)

This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP requests; it must be subclassed to handle each request method (e.g. GET or POST). BaseHTTPRequestHandler provides a number of class and instance variables, and methods for use by subclasses.

它不能回复任何HTTP请求,必须通过子类实现对请求的响应

The handler will parse the request and the headers, then call a method specific to the request type. The method name is constructed from the request. For example, for the request method SPAM, the do_SPAM() method will be called with no arguments. All of the relevant information is stored in instance variables of the handler. Subclasses should not need to override or extend the init() method.

handler会根据请求的类型而调用相应的方法,比如对于get请求,它会调用do_GET方法,我们要做的不是重写它的handler方法而是重写它的do_*方法。子类不需要重写或扩展父类的__init__()方法

下面是基于文档的简单实现example_A

	from http.server import HTTPServer,BaseHTTPRequestHandler
	server_address = ('127.0.0.1',1234)
	httpd = HTTPServer(server_address,BaseHTTPRequestHandler)
	httpd.serve_forever()
	httpd.server_close()

example_A_result:

127.0.0.1 - - [14/Jan/2019 10:30:46] "GET /favicon.ico HTTP/1.1" 501 -

注解:HTTP 501错误:未实现(Not implemented)是指Web服务器不理解或不支持发送给它的HTTP数据流中找到的HTTP方法。
可知这是由于没有实现BaseHTTPRequestHandler子类,并重写父类的do_*方法导致的。我们需要完善代码,那就需要补充一些关于这个类的了解。

class BaseHTTPRequestHandler的一些属性与方法

  • wfile属性,wfile是一个io.BufferedIOBase对象,也就是具备.write方法,可以用于返回数据时使用,具体参看文档。
  • 具备各种等待我们重写的do_*方法
  • .path属性获取用户输入的链接的请求路径
  • .send_error()方法返回错误提示

send_response(code, message=None)

Adds a response header to the headers buffer and logs the accepted request. The HTTP response line is written to the internal buffer, followed by Server and Date headers. The values for these two headers are picked up from the version_string() and date_time_string() methods, respectively. If the server does not intend to send any other headers using the send_header() method, then send_response() should be followed by an end_headers() call

如果server不打算通过调用send_header()方法发送其他响应头参数,那么send_response方法需要以end_headers()结束

send_header(keyword, value)
用于构造HTTP头,调用后必须调用end_headers方法结束.HTTP头中的keywoad和value可以参考下图。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值