python 新建简单的webserver

  • server的信息
from http.server import BaseHTTPRequestHandler,HTTPServer
PORT_NUMBER = 8088
class myHandler(BaseHTTPRequestHandler):
    # Handler for the GET requests
    def do_GET(self):
        print('Get request received')
        req = self.raw_requestline.decode().split()
        find_str = "msg"
        for i in req:
            if i.find(find_str) > 0:  #得到客户端的请求,处理参数
                print(i[len(find_str)+3:])
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write(b"Hello World !") #发送信息给客户端
try:
    server = HTTPServer(("192.168.1.38", PORT_NUMBER), myHandler)
    print ('Started httpserver on port ' , PORT_NUMBER)
    server.serve_forever()
except KeyboardInterrupt:
    print ('^C received, shutting down the web server')
    server.socket.close()

然后在浏览器里面输入:

python 新建简单的webserver_第1张图片
Paste_Image.png
  • 看看服务器打印效果
python 新建简单的webserver_第2张图片
Paste_Image.png

你可能感兴趣的:(python 新建简单的webserver)