[转载] SimpleHTTPServer解释:如何使用Python发送文件

参考链接: 使用Python的SimpleHTTPServer模块提供文件

As a web developer, there will be a point when you need to create your own local web server. 

  作为Web开发人员,您需要创建自己的本地Web服务器。  

 Maybe it's because you'll be on a flight and want to work on your project, far from internet service. Or perhaps you just want a quick way to access files from another computer on your home network. 

  也许是因为您将要飞行并且想要从事项目工作,而远离互联网服务。 或者,也许您只是想要一种快速的方法来从家庭网络中的另一台计算机访问文件。  

 Whenever and however the need arises, setting up a local HTTP server is a useful skill to have.

  无论何时,只要有需要,设置本地HTTP服务器都是一项有用的技能。  

  什么是HTTP服务器? (What is an HTTP server?) 

 Simply put, an HTTP server or web server is a process running on a machine that listens for incoming requests and serves web pages. 

  简而言之,HTTP服务器或Web服务器是在计算机上运行的进程,用于侦听传入的请求并提供网页。  

 For example, when you type in https://www.freecodecamp.org/news/ into your browser, there's a server somewhere listening for that request. In response, it sends back data so your browser can render the freeCodeCamp Developer News page.

  例如,当您在浏览器中输入https://www.freecodecamp.org/news/ ,某处有一个服务器在侦听该请求。 作为响应,它将发送回数据,以便您的浏览器可以呈现freeCodeCamp开发人员新闻页面。  

 Of course there's a lot more happening behind the scenes, but for the purposes of this tutorial, that's all you really need to know.

  当然,幕后还有很多事情要做,但是就本教程而言,这就是您真正需要知道的全部。  

  如何设置本地HTTP服务器 (How to set up a local HTTP server) 

 Install Python 安装Python Open your command prompt or terminal and run python -V 打开命令提示符或终端并运行python -V Go to your project's directory with cd on *nix or MacOS systems or CD for Windows 转到您的项目目录与cd在* nix或者MacOS系统或CD的Windows Run the following commands to start a local HTTP server: 运行以下命令以启动本地HTTP服务器: 

 # If python -V returned 2.X.X

python -m SimpleHTTPServer

 

# If python -V returned 3.X.X

python3 -m http.server

 

# Note that on Windows you may need to run python -m http.server instead of python3 -m http.server

 You'll notice that both commands look very different – one calls SimpleHTTPServer and the other http.server. This is just because the SimpleHTTPServer module was rolled into Python's http.server in Python 3. They both work the same way.

  您会注意到,这两个命令看起来非常不同–一个调用SimpleHTTPServer ,另一个调用http.server 。 这仅仅是因为SimpleHTTPServer模块已在Python 3中集成到Python的http.server中。它们都以相同的方式工作。  

 Now when you go to http://localhost:8000/ you should see a list of all the files in your directory. Then you can just click on the HTML file you want to view.

  现在,当您访问http://localhost:8000/ ,应该会看到目录中所有文件的列表。 然后,您只需单击要查看HTML文件。  

 Just keep in mind that SimpleHTTPServer and http.server are only for testing things locally. They only do very basic security checks and shouldn't be used in production.

  请记住, SimpleHTTPServer和http.server仅用于本地测试。 它们仅执行非常基本的安全检查,不应在生产中使用。  

  如何在本地发送文件 (How to send files locally) 

 To set up a sort of quick and dirty NAS (Network Attached Storage) system:

  要设置一种快速而肮脏的NAS(网络附加存储)系统,请执行以下操作:  

 Make sure both computers are connected through same network via LAN or WiFi 确保两台计算机都通过LAN或WiFi通过同一网络连接 Open your command prompt or terminal and run python -V to make sure Python is installed 打开命令提示符或终端并运行python -V以确保已安装Python Go to the directory whose file you want to share by using cd (change directory) command. 使用cd(更改目录)命令转到要共享其文件的目录。 Go to the directory with the file you want to share using cd on *nix or MacOS systems or CD for Windows 使用* nix或MacOS系统上的cd或Windows的CD转到要共享文件的目录 Start your HTTP server with either python -m SimpleHTTPServer or python3 -m http.server 使用python -m SimpleHTTPServer或python3 -m http.server启动HTTP服务器 Open new terminal and type ifconfig on *nix or MacOS or ipconfig on Windows to find your IP address 打开新终端,然后在* nix或MacOS上键入ifconfig或在Windows上键入ipconfig来找到您的IP地址 

 Now on the second computer or device:

  现在在第二台计算机或设备上:  

 Open browser and type in the IP address of the first machine, along with port 8000: http://[ip address]:8000 打开浏览器,输入第一台计算机的IP地址以及端口8000: http://[ip address]:8000 

 A page will open showing all the files in the directory being shared from the first computer. If the page is taking too long to load, you may need to adjust the firewall settings on the first computer. 

  将打开一个页面,显示从第一台计算机共享的目录中的所有文件。 如果页面加载时间太长,则可能需要在第一台计算机上调整防火墙设置。  

 

  翻译自: https://www.freecodecamp.org/news/simplehttpserver-explained-how-to-send-files-using-python/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值