Python SimpleHTTPServer 模块是一个非常有用的工具. 您可以使用 Python SimpleHTTPServer 将任何目录转换成一个简单的 HTTP Web 服务器。
Python 简单HTTPS服务器
Python SimpleHTTPServer仅支持两种 HTTP 方法 - GET 和 HEAD. 所以这是一个很好的工具来通过网络共享文件。 Python SimpleHTTPServer已经迁移到 python http.server 模块在 python 3,我们今天将了解这两个模块,看看它是多么容易与他们一起工作。假设你和你的朋友正在使用相同的本地网络。你有一些文件,你想与你的朋友分享。但是你俩都有便携式硬盘,所以你可以将这些电影复制到便携式硬盘,并把它交给你的朋友。然后 Python SimpleHTTPServer 可以帮助你在这种情况下。使用 SimpleHTTPServer,你可以很容易地分享你的文件给你的朋友谁在同一个网络上。在本教程中,我们将学习关于 Python SimpleHTTPServer
Python 简单 HTTP 服务器
如果你正在使用Windows操作系统,然后去你想要的文件夹或目录,你想分享。现在,使用‘shift+右键单击’。你会找到在该目录中打开命令提示的选项。只需点击它并打开命令提示在那里。然而,如果你正在使用Ubuntu,只需右键单击该目录和打开终端。
1$python -m SimpleHTTPServer 9000
您可以在任何端口上运行 Python http 服务器,默认端口为 8000。 尝试使用大于 1024 的端口号,以避免冲突。 然后打开您最喜欢的浏览器,输入localhost:9000
。 是的! 您已经完成了!!! 现在知道您的 ip 地址,然后用您的 ip 地址代替 localhost,然后与您的朋友分享。
Python SimpleHTTPServer 错误 - 没有名为 SimpleHTTPServer 的模块
如果你正在运行Python 3,你会收到错误,如没有名为SimpleHTTPServer
的模块。 这是因为在Python 3,SimpleHTTPServer已经合并到http.server模块中。
1$python3 -m http.server 9000
Python 简单HTTPServer 示例
Below images show the Python SimpleHTTPServer output in terminal and browser.
Note that if there is any
index.html
file then it will be served to the browser, otherwise directory listing will be shown as in above image.
Python HTTP 服务器
Below image shows the terminal output for python http server module in python 3. Browser output remains same as in above images. As you can see from terminal output that the python 3 http server module is more clean, provides clear messages. Python http server module doesn't show all the python modules details on quitting from keyboard, that is a more clean approach. That’s all about Python SimpleHTTPServer in python 2 and python http server in python 3. If you don't have python installed in your system and want to give it a try, please go through python tutorial for beginners to get started. Reference: Official Documentation