Python SimpleHTTPServer – Python HTTP服务器

Python SimpleHTTPServer module is a very handy tool. You can use Python SimpleHTTPServer to turn any directory into a simple HTTP web server.

Python SimpleHTTPServer模块是一个非常方便的工具。 您可以使用Python SimpleHTTPServer将任何目录转换为简单的HTTP Web服务器。

Python SimpleHTTPServer (Python SimpleHTTPServer)

Python SimpleHTTPServer supports only two HTTP methods – GET and HEAD. So it’s a good tool to share files over network. Python SimpleHTTPServer has been migrated to python http.server module in python 3, we will learn about both of these modules today and see how easy it is to work with them.

Python SimpleHTTPServer仅支持两种HTTP方法-GET和HEAD。 因此,它是通过网络共享文件的好工具。 Python SimpleHTTPServer已被迁移到python 3中的python http.server模块,我们今天将了解这两个模块,并了解使用它们的难易程度。

Suppose you and your friend are using same local network. You have some files that you want to share with your friend. But both of you have portable hard disks so that you can copy those movies to that portable hard disks and give it to your friend. Then Python SimpleHTTPServer can help you in this case.

假设您和您的朋友使用同一局域网。 您有一些要与朋友共享的文件。 但是你们两个都有便携式硬盘,因此您可以将这些电影复制到该便携式硬盘上,然后将其提供给您的朋友。 然后,在这种情况下,Python SimpleHTTPServer可以为您提供帮助。

By Using SimpleHTTPServer, you can easily share your files to your friends who are in the same network. In this tutorial we will learn about basics of Python SimpleHTTPServer so that you can use it your day to day life.

通过使用SimpleHTTPServer,您可以轻松地将文件共享给同一网络中的朋友。 在本教程中,我们将学习Python SimpleHTTPServer的基础知识,以便您可以在日常生活中使用它。

Python简单HTTP服务器 (Python Simple HTTP Server)

If you are using Windows operating system then go to your desired folder or directory that you want to share. Now, use shift+right click. Your will find option to open command prompt in that directory. Just click on that and open command prompt there. However, if you are using Ubuntu, just right click into that directory and open terminal. After that, execute the below command.

如果您使用的是Windows操作系统,请转到要共享的所需文件夹或目录。 现在,使用shift+right click 。 您将找到在该目录中打开命令提示符的选项。 只需单击它,然后在此处打开命令提示符。 但是,如果您使用的是Ubuntu,则只需右键单击该目录并打开终端。 之后,执行以下命令。

$python -m SimpleHTTPServer 9000

You can run python http server on any port, default port is 8000. Try to use port number greater than 1024 to avoid conflicts. Then open your favourite browser and type localhost:9000. Yeah! You’re done!!! Now know your ip address and then replace localhost with your ip address and then share it with your friend.

您可以在任何端口上运行python http服务器,默认端口为8000。请尝试使用大于1024的端口号以避免冲突。 然后打开您喜欢的浏览器,然后输入localhost:9000 。 是的 你完成了!!! 现在知道您的IP地址,然后将localhost替换为您的IP地址 ,然后与您的朋友共享。

Python SimpleHTTPServer错误–没有名为SimpleHTTPServer的模块 (Python SimpleHTTPServer Error – No module named SimpleHTTPServer)

If you are running Python 3, you will get error as No module named SimpleHTTPServer. It’s because in python 3, SimpleHTTPServer has been merged into http.server module. You can use below command to run python http server in Python 3.

如果您运行的是Python 3,则会收到错误消息,提示No module named SimpleHTTPServer 。 这是因为在python 3中,SimpleHTTPServer已合并到http.server模块中。 您可以使用以下命令在Python 3中运行python http服务器。

$python3 -m http.server 9000

Python SimpleHTTPServer示例 (Python SimpleHTTPServer Example)

Below images show the Python SimpleHTTPServer output in terminal and browser.

下图显示了终端和浏览器中的Python SimpleHTTPServer输出。

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.

请注意,如果有任何index.html文件,则它将被提供给浏览器,否则目录列表将如上图所示。

Python HTTP服务器 (Python HTTP Server)

Below image shows the terminal output for python http server module in python 3. Browser output remains same as in above images.

python http server example

下图显示了python 3中python http服务器模块的终端输出。浏览器输出与上图相同。

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.

从终端输出中可以看到python 3 http服务器模块更干净,提供了清晰的消息。 Python http服务器模块并未显示有关从键盘退出的所有python模块详细信息,这是一种更干净的方法。

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.

这就是有关python 2中的Python SimpleHTTPServer和python 3中的python http服务器的全部内容。如果您的系统中未安装python并想尝试一下,请通过python教程供初学者入门。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/15915/python-simplehttpserver-http-server

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值