Python简单HTTP Server
from http.server import BaseHTTPRequestHandler, HTTPServer class MyHttpRequestHandler(BaseHTTPRequestHandler): def do_POST(self): print("path: " + self.path) print("method: " + self.command) print(self.headers) length = int(self.headers['content-length']) print("-----------------------data------------------") datas = self.rfile.read(length) print(datas) print("-----------------------data------------------\n") self.send_response(200) self.send_header("Content-type", "text/html; charset=UTF-8") self.end_headers() def do_GET(self): print("path: " + self.path) print("method: " + self.command) # 响应头 headers = """HTTP/1.1 200 OK Server: YouFather Accept-Ranges: bytes Content-Length: {data-length} Vary: Accept-Encoding Content-Type: text/html """.replace('\n', '\r\n') + '\r\n' # 响应数据 data = "<a href='http://www.baidu.com'>百度</a>".encode('gbk') # 设置一下这个Content-Length参数,告诉客户端数据的长度。 headers = headers.format_map({'data-length': len(data)}) # 写入响应头和数据 # 这里的wfile是HTTPServer的基类TCPServer为我们准备的一个写入对象。 # wfile = socket.make_file('wb') # 和open(filename,'w')是同一类型。 self.wfile.write(headers.encode()) self.wfile.write(data) ts = HTTPServer(('0.0.0.0', 8899), MyHttpRequestHandler) print("-----------------------start server------------------") ts.serve_forever()
-------------
对象存贮的WebHook
https://blog.csdn.net/u012279452/article/details/92852698
标签:HTTP,Python,self,Server,headers,length,print,data From: https://www.cnblogs.com/gispathfinder/p/17567973.html