<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 網絡服務器 > 原文: [https://pythonbasics.org/webserver/](https://pythonbasics.org/webserver/) Python 中的網絡服務器可以通過兩種方式設置。 Python 開箱即用地支持 Web 服務器。 您可以使用一個襯墊啟動 Web 服務器。 但是,您也可以創建具有獨特功能的自定義 Web 服務器。 在本文中,您將學習如何做到這一點。 此示例中的 Web 服務器只能在本地網絡上訪問。 它可以是`localhost`或其他網絡主機。 您可以使用 VPN 跨位置投放。 ## 示例 ### 內置網絡服務器 要啟動網絡服務器,請運行以下命令: ```py python3 -m http.server ``` 這將在端口 8080 上打開 Web 服務器。然后,您可以在 [http://127.0.0.1:8080/](http://127.0.0.1:8080/) 中打開瀏覽器。 也可以使用您的`192.168.*.*`地址通過網絡訪問 Web 服務器。 這是默認服務器,可用于從計算機下載文件。 ### Web 服務器 運行以下代碼以啟動自定義 Web 服務器。 要創建自定義 Web 服務器,我們需要使用 HTTP 協議。 通過設計,http 協議有一個“獲取”請求,該請求返回服務器上的文件。 如果找到該文件,它將返回 200。 服務器將從端口 8080 開始,并接受默認的 Web 瀏覽器請求。 ```py # Python 3 server example from http.server import BaseHTTPRequestHandler, HTTPServer import time hostName = "localhost" serverPort = 8080 class MyServer(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8")) self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8")) self.wfile.write(bytes("<body>", "utf-8")) self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8")) self.wfile.write(bytes("</body></html>", "utf-8")) if __name__ == "__main__": webServer = HTTPServer((hostName, serverPort), MyServer) print("Server started http://%s:%s" % (hostName, serverPort)) try: webServer.serve_forever() except KeyboardInterrupt: pass webServer.server_close() print("Server stopped.") ``` 如果打開類似 [http://127.0.0.1/example](http://127.0.0.1/example) 的網址,則會調用`do_GET()`方法。 我們通過這種方法手動發送網頁。 ![web server in python 3](https://img.kancloud.cn/cb/c7/cbc72d687466e4560c91ad907825578d_624x292.jpg) 變量`self.path`返回所請求的 Web 瀏覽器 URL。 在這種情況下,它將是`/example` [下載示例和練習](https://gum.co/dcsp)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看