python-新建简单的webserver

2017-01-01 10:46
  • 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()

然后在浏览器里面输入:

Paste_Image.png

  • 看看服务器打印效果

Paste_Image.png