python 快速启动http监听服务

python3

[root@vm10-20-9-45 ~]# python3 -m http.server 2378
Serving HTTP on 0.0.0.0 port 2378 (http://0.0.0.0:2378/) ...
100.81.171.16 - - [03/Jan/2021 19:24:57] code 501, message Unsupported method ('POST')
100.81.171.16 - - [03/Jan/2021 19:24:57] "POST /index HTTP/1.1" 501 -

python2

 [root@vm10-20-9-45 ~]# python2 -m SimpleHTTPServer 2378
Serving HTTP on 0.0.0.0 port 2378 ...
100.81.82.14 - - [03/Jan/2021 19:26:24] code 501, message Unsupported method ('POST')
100.81.82.14 - - [03/Jan/2021 19:26:24] "POST /index HTTP/1.1" 501 -
100.81.82.12 - - [03/Jan/2021 19:26:34] code 404, message File not found
100.81.82.12 - - [03/Jan/2021 19:26:34] "GET /index HTTP/1.1" 404 -

 python3 启动http 打印header

cat http-server-py3.py
#!/usr/bin/python3
from http.server import HTTPServer, BaseHTTPRequestHandler

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/plain')
        self.end_headers()
        message = 'Request Headers: \n' + str(self.headers)
        print(message) # 调试打印
        self.wfile.write(message.encode()) # 返回客户端

    def do_HEAD(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/plain')
        self.end_headers()
        message = 'Request Headers: \n' + str(self.headers)
        print(message) # 调试打印
        self.wfile.write(message.encode()) # 返回客户端


if __name__ == '__main__':
    httpd = HTTPServer(('0.0.0.0', 80), SimpleHTTPRequestHandler)
    print("Serving at http://0.0.0.0:80")
    httpd.serve_forever()

 

posted on 2021-01-03 19:29  zhangmingda  阅读(2023)  评论(0编辑  收藏  举报

导航