在本機搭建Web服務器其實也有更簡單的方法,可以利用iis功能。可以自行搜索本機iis搭建Web服務器。不用寫代碼,Windows自帶的web服務器功能。
Python2提供了BaseHTTPServer模塊,不過在Py3把它合并到了http.server中。
老教材用BaseHTTPServer你可以直接用http.server代替即可。
這里利用http.server搭建最簡單的web服務器:
from http.server import HTTPServer,BaseHTTPRequestHandlerclass Request(BaseHTTPRequestHandler): def do_GET(self): print(self.path) self.send_response(200) # 標識傳遞數據類型 self.send_header('Content-type','text/html') self.end_headers() self.wfile.write('這里用來傳數據') # 下面的形式可以用來傳html文件 # with open('D://Python網絡編程基礎//Python代碼//http.html','rb') as t: # print('輸出了') # self.wfile.write(t.read()) def run(): host='localhost' port=80 server=HTTPServer((host,port),Request) server.serve_forever()if __name__=='__main__': # print(Request.path) run()
然后可以用瀏覽器,訪問localhost,默認的是80端口。
一般80是http,443是https,這里你也可以用別的端口。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答