第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 python 代碼中使用 http.server 命令行

在 python 代碼中使用 http.server 命令行

瀟瀟雨雨 2022-12-06 15:28:12
在命令行中,我們可以這樣做:$ python3 -m http.server 8674但在 Python 代碼中(在 .py 中),如何做到這一點?別用os.system!我打算在 exe 中使用它,但會失敗。PPS 不要建議這個。真正來自代碼,而不是命令行。
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經(jīng)驗 獲得超13個贊

您所要做的就是導入http.server默認模塊。


from http.server import HTTPServer, SimpleHTTPRequestHandler


def run(number=8080, server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):

    server_address = ('', number)

    httpd = server_class(server_address, handler_class)

    try:

        httpd.serve_forever()

    except KeyboardInterrupt:

        print("Exit")

有關詳細說明,請參閱Python 文檔。



查看完整回答
反對 回復 2022-12-06
?
撒科打諢

TA貢獻1934條經(jīng)驗 獲得超2個贊

通過使用這兩個模塊,可以通過 Python 程序輕松地為網(wǎng)站提供服務:


http.server(用于 http)

套接字服務器(用于 TCP 端口)

這是工作代碼的示例:


# File name:  web-server-demo.py


import http.server

import socketserver


PORT = 8080

Handler = http.server.SimpleHTTPRequestHandler


with socketserver.TCPServer(("", PORT), Handler) as httpd:

    print("serving the website at port # ", PORT)

    httpd.serve_forever()

示例 index.html 文件:


<!DOCTYPE html>

<html>

  <head>

    <title>Website served by Python</title>

  </head>

  <bod>

    <div>

      <h1>Website served by Python program</h2>

    </div>

  </body>

</html>

輸出:


> python web-server-demo.py

serving the website at port #  8080

127.0.0.1 - - [25/May/2020 14:19:27] "GET / HTTP/1.1" 304 -

http://img1.sycdn.imooc.com//638eef450001f53c05250294.jpg

查看完整回答
反對 回復 2022-12-06
  • 2 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號